64. ZigZag Conversion
ZigZag Conversion
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".
思路:
方法1: 刚开始正着方三个,然后反着放两个,正着放两个。直到结束。(优)
class Solution {
public:
string convert(string s, int nRows) {
if(nRows <= 1 || s == "") return s;
int len = s.length();
string s2[nRows];
int k = 0, k2 = 0;
while(k2 < len){
while(k < nRows && k2 < len) s2[k++].push_back(s[k2++]);
k--;
while(k > 0 && k2 < len) s2[--k].push_back(s[k2++]);
k++;
}
string s3;
for(int i = 0; i < nRows; ++i) {
s3 += s2[i];
}
return s3;
}
};
方法二: 类似方法一,不过存下每个位置的字符应该放的地方。然后依次读入。
class Solution {
public:
string convert(string s, int nRows) {
if(nRows <= 1 || s == "") return s;
int len = s.length();
vector<int> index(2*nRows-2);
int t = 0;
for(int i = 0; i < nRows; ++i) index[t++] = i;
for(int j = nRows-2; j > 0; --j) index[t++] = j;
string s2[nRows];
int k = 0, m = 2*(nRows-1);
while(k < len){
s2[index[k % m]].push_back(s[k]);
k++;
}
string s3;
for(int i = 0; i < nRows; ++i) {
s3 += s2[i];
}
return s3;
}
};
64. ZigZag Conversion的更多相关文章
- 【leetcode❤python】 6. ZigZag Conversion
#-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object): def convert(self, s, numRow ...
- No.006 ZigZag Conversion
6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...
- leetcode第六题 ZigZag Conversion (java)
ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...
- leetcode题解 6.ZigZag Conversion
6.ZigZag Conversion 题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a gi ...
- 6.[leetcode] ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 字符串按照Z旋转90度然后上下翻转的字形按行输出字符串--ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- LeetCode--No.006 ZigZag Conversion
6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...
- leetcode-algorithms-6 ZigZag Conversion
leetcode-algorithms-6 ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag ...
- LeetCode 6. ZigZag Conversion & 字符串
ZigZag Conversion 看了三遍题目才懂,都有点怀疑自己是不是够聪明... 就是排成这个样子啦,然后从左往右逐行读取返回. 这题看起来很简单,做起来,应该也很简单. 通过位置计算行数: P ...
随机推荐
- Android 4.2以上的手机USB调试设置
今天遇到一个问题,我手上有两部手机一部是红米.一部是中兴的青漾QY N986,两部手机的Android系统都是4.2.1的,连接到电脑测试,找了半天没有找到设置开发者选项,后来在网上找了半天,才发现g ...
- candence 知识积累3
1. PCB板型: 1.新建PCB:PCB design ,新建的类型为board ,输入名称和保存位置,设置图纸参数.网格参数. 2.建立PCB板外框:菜单Add下选择相应的工具.在Option选项 ...
- Ubuntu下部分文件操作的命令
(1)创建目录 mkdir filefoldname (2)删除空目录 rmdir filefoldname (3)删除非空目录及其中所有文件 rm -rf filefoldname (4)移动文件或 ...
- <Java中的继承和组合之间的联系和区别>
//Java中的继承和组合之间的联系和区别 //本例是继承 class Animal { private void beat() { System.out.println("心胀跳动...& ...
- Java Web项目 配置 ueditor心得
近期的JAVA项目,由于客户要求需要引入富文本编辑器. 参考了两款插件,一款是ckeditor,一款是ueditor. ckeditor在上传文件的时候必须配合ckfinder使用,而ckfinder ...
- C#常用操作类库三(XML操作类)
/// <summary> /// XmlHelper 的摘要说明. /// xml操作类 /// </summary> public class XmlHelper { pr ...
- unix exec族函数 关于参数的疑惑
问题不出在这几个函数,而在于看后文解释器的时候发现一个很奇妙的问题. #include <unistd.h> int execl(const char *pathname, const c ...
- HDU 2277 Change the ball
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2277 Change the ball Time Limit: 2000/1000 MS (Java/O ...
- ubuntu下mysql的常用命令,MySQL数据库的基本操作命令
一.mysql服务操作 0.查看数据库版本 sql-> status; 1.net start mysql //启动mysql服务 2.net stop mysql //停止mysql服务 3 ...
- UIControlEventTouch
在控件事件中,简单解释下下面几个事件. 说明:由于是在“iOS 模拟器”中测试的,所以不能用手指,只能用鼠标. 1)UIControlEventTouchDown 指鼠标左键按下(注:只是“按下”)的 ...