leetcode:ZigZag Conversion 曲线转换
Question:
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line:"PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".
字符串"PAYPALISHIRING"就像以下“Z”形排列
P A H N
A P L S I I G
Y I R 所要得到的字符串按横行的顺序依次排列,这个字符串要求得到"PAHNAPLSIIGYIR"
给定string convert(string text, int nRows)这个函数,你需要写代码完成这个转换,nRows可以为任意正整数。
设计思想:1、对Rows的情况进行讨论,当Rows=1或者字符串text的长度<nRows,那么可以确定字符串还是顺序输出;
2、如果nRows=2,那么首先要获得text字符串偶数位置上的字母,然后获得奇数位置上的字母;
3、如果nRows>2且字符串长度>nRows,设计一个mark数组用来标记在字符串会在第几行,比如mark[k]=i,意思是第K个字母会在第i行。通过设置一boolean变量bool,来控制字符串移动方向,通过设置i来控制换行。
4、最后根据mark标记数组中的内容,分别添加第0行、1行一直到nRows行的字母到str中,并返回。
代码实现(java):
class Solution6 {
public String convert(String s, int nRows) {
String str="";
if(0==nRows||1==nRows||s.length()<=nRows)
return s;
if(2==nRows){
for(int i=0;i<s.length();i+=2)
str+=s.charAt(i);
for(int i=1;i<s.length();i+=2)
str+=s.charAt(i);
return str;
}
int i=0,k=0;
boolean bool=false;
Integer []mark=new Integer[s.length()];
while(k<s.length()){
mark[k++]=i;
if(i==nRows-1){
bool=true;
}
else if(i==0){
bool=false;
}
if(!bool)
i++;
else if(bool)
i--;
}
k=0;i=0;
while(i<nRows){
while(k<s.length()){
if(i==mark[k]){
mark[k]=-1;
str+=s.charAt(k);
}
k++;
}
k=0;
i++;
}
return str;
}
}
2013-10-04
leetcode:ZigZag Conversion 曲线转换的更多相关文章
- 6.[leetcode] ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- LeetCode ZigZag Conversion(将字符串排成z字型)
class Solution { public: string convert(string s, int nRows) { string a=""; int len=s.leng ...
- LeetCode: ZigZag Conversion 解题报告
ZigZag ConversionThe string "PAYPALISHIRING" is written in a zigzag pattern on a given num ...
- LeetCode——ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- [leetcode]ZigZag Conversion @ Python
原题地址:https://oj.leetcode.com/problems/zigzag-conversion/ 题意: The string "PAYPALISHIRING" i ...
- [LeetCode]ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- [LeetCode] ZigZag Conversion [9]
称号 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- C++ leetcode::ZigZag Conversion
mmp,写完没保存,又得重新写.晚上写了简历,感觉身体被掏空,大学两年半所经历的事,一张A4纸都写不满,真是一事无成呢.这操蛋的生活到底想对我这个小猫咪做什么. 今后要做一个早起的好宝宝~晚起就诅咒自 ...
- Leetcode:ZigZag Conversion分析和实现
问题的大意就是将字符串中的字符按锯齿状(倒N形)垂直由上向下放置,最后水平从左向右读取.比如 ABCDEFGHIJKLMN,4表示 A G M B F H ...
随机推荐
- /dev/tty /dev/ttyS0 /dev/tty0,/dev/null区别
1./dev/tty表示控制终端如果当前进程有控制终端(Controlling Terminal)的话,那么/dev/tty就是当前进程的控制终端的设备特殊文件.可以使用命令”ps –ax”来查看进程 ...
- 安卓自动化测试之MonkeyRunner环境的搭建
最近在抽时间学习一些安卓自动化的知识,把学到东西都就记录下来,避免以后记性不好忘记,也方便对自己积累知识的查阅 MonkeyRunner是基于坐标点来操作控件的,你可以通过写python脚本来调用mo ...
- Case 架构的实际应用-1
We use testlink to manage cases, and the frame is below: Project Name -All Features(Modules) -Featur ...
- 同一Session中的aspx页面的并发限制
项目中客户端采用WebBrowser展示aspx页面,用户有可能打开带多个带WebBrowser的winform窗体.此时,如果其中一个的WebBrowser的aspx页面响应较长的话,其他窗体中的W ...
- eclipse中maven项目部署到tomcat
其实maven项目部署到tomcat的方式很多,我从一开始的打war包到tomcat/webapps目录,到使用tomcat-maven插件,到直接使用servers部署,一路来走过很多弯路. 下面就 ...
- 真正理解 git fetch, git pull 以及 FETCH_HEAD【转】
转自:http://www.cnblogs.com/ToDoToTry/p/4095626.html 真正理解 git fetch, git pull 要讲清楚git fetch,git pull,必 ...
- 解决docker中DNS查询的问题
I got a dns error that i can not access dns server, that caused by : /etc/resolv.conf you can find t ...
- 《c程序设计语言》读书笔记--统计字符数
#include <stdio.h> #define MAXLINE 1000 int getline(char line[],int maxline); void copy(char t ...
- pyhton与json,Xml
对简单数据类型的encoding 和 decoding: 使用简单的json.dumps方法对简单数据类型进行编码,例如: 1 2 3 4 5 6 import json obj = [[1,2, ...
- MV、MVC、MVP、MVVM简介,对MVC不确定了。
参考: http://www.cnblogs.com/changxiangyi/archive/2012/07/16/2594297.html http://www.jcodecraeer.com/a ...