LeetCode OJ ZigZag Conversion ”之“字符串变化 string操作
2015-03-24 13:05
169 查看
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".
class Solution { public: /* 向下循环:nRows 斜角线循环:nRows-2(减去首尾两个端点) s PAYPALISHIRING i 0 j 0 res PAHN APLSIIG YIR */ string convert(string s, int nRows) { if(nRows == 1) return s; string res[nRows]; int i,j; i=0; while(i<s.size()) { for(j=0;j<nRows&&i<s.size();j++) res[j]+=s[i++]; for(j=nRows-2;j>0&&i<s.size();j--) res[j]+=s[i++]; } string str=""; for(i=0;i<nRows;i++) str+=res[i]; return str; } };
相关文章推荐
- LeetCode-6 ZigZag Conversion(字符串锯齿形输出)
- LeetCode 345 Reverse Vowels of a String(字符串操作)
- 九度oj 题目1105:字符串的反码(string的操作)
- LeetCode OJ ZigZag Conversion
- LeetCode OJ ZigZag Conversion
- (LeetCode)ZigZag Conversion --- Z型字符串
- leetcode---zigzag-conversion---字符串
- LeetCode系列字符串操作(一)ZigZag输出,寻找最大不重复字串长度。
- Leetcode 6 ZigZag Conversion 字符串处理
- LeetCode OJ String to Integer (atoi) 字符串转数字
- 有关String 字符串的各种操作大全
- C++ 字符(char)与字符串(string)操作
- C#(asp.net)对字符串的操作公共类(StringUtil.cs)
- C++_String_类字符串操作
- Python学习笔记二(String字符串操作)
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- 字符串(String)的一系列操作
- 字符串(String)的一系列操作
- String字符串的操作
- tcl/tk参考——字符串操作string