6. ZigZag Conversion
来源:互联网 发布:按键精灵淘宝秒杀脚本 编辑:程序博客网 时间:2024/06/05 21:41
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 NA P L S I I GY I RAnd 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"
.
/*n=numRowsΔ=2n-2 1 2n-1 4n-3Δ= 2 2n-2 2n 4n-4 4n-2Δ= 3 2n-3 2n+1 4n-5 .Δ= . . . . .Δ= . n+2 . 3n .Δ= n-1 n+1 3n-3 3n-1 5n-5Δ=2n-2 n 3n-2 5n-4*/
that's the zigzag pattern the question asked! Be careful with nR=1 && nR=2
class Solution {public: string convert(string s, int numRows) { string result=""; if(numRows==1) return s; int step1,step2; int len=s.size(); for(int i=0;i<numRows;++i){ step1=(numRows-i-1)*2; step2=(i)*2; int pos=i; if(pos<len) result+=s.at(pos); while(1){ pos+=step1; if(pos>=len) break; if(step1) result+=s.at(pos); pos+=step2; if(pos>=len) break; if(step2) result+=s.at(pos); } } return result; }};
use numRows space and easy to understand ,while my answer search the math solution,not easy to understand ,but is interesting
string convert(string s, int numRows){ int length = s.size(); if(numRows <= 1 || length <= numRows) return s; string sZigzag; for(int iLineIndex = 0; iLineIndex < numRows; iLineIndex++) { int iRowIndex = iLineIndex; for(; iRowIndex < length; iRowIndex += 2 * (numRows - 1)) { if(0 == iLineIndex || numRows - 1 == iLineIndex) sZigzag = sZigzag + s[iRowIndex]; else { if(iRowIndex == iLineIndex) sZigzag = sZigzag + s[iRowIndex]; else { sZigzag = sZigzag + s[iRowIndex - 2 * iLineIndex]; sZigzag = sZigzag + s[iRowIndex]; } } } if((iLineIndex > 0) && (iLineIndex < numRows - 1) && (iRowIndex - 2 * iLineIndex < length)) sZigzag = sZigzag + s[iRowIndex - 2 * iLineIndex]; } return sZigzag;}
The problem statement itself is unclear for many. Especially for 2-row case. "ABCD", 2 --> "ACBD". The confusion most likely is from the character placement. I would like to extend it a little bit to make ZigZag easy understood.
The example can be written as follow:
- P.......A........H.......N
- ..A..P....L..S....I...I....G
- ....Y.........I........R
Therefore, <ABCD, 2> can be arranged as:
- A....C
- ...B....D
My simple accepted code:
class Solution {public: string convert(string s, int numRows) { int len=s.size(); if(numRows<=1||len<=numRows) return s; vector<string> vs(numRows); string ss; int step,row=0; for(int i=0;i<len;i++) { vs[row].push_back(s[i]); if(row==0) step=1; else if(row==numRows-1) step=-1; row+=step; } for(int j=0;j<numRows;j++) ss.append(vs[j]); return ss; }};
- LeetCode 6. ZigZag Conversion
- LeetCode --- 6. ZigZag Conversion
- LeetCode 6.ZigZag Conversion
- [Leetcode] 6. ZigZag Conversion
- 6.ZigZag Conversion
- 6. ZigZag Conversion
- 6. ZigZag Conversion
- 【leetcode】6. ZigZag Conversion
- 6.ZigZag Conversion
- 6. ZigZag Conversion
- 6.ZigZag Conversion
- [LeetCode] 6.ZigZag Conversion
- LeetCode - 6. ZigZag Conversion
- 6. ZigZag Conversion
- 6. ZigZag Conversion
- 6.ZigZag Conversion(93.10%)
- 6. ZigZag Conversion
- [leetcode] 6. ZigZag Conversion
- 用好PowerPoint的“重用幻灯片”
- Thrift使用指南
- CodeForces - 366A Dima and Guards (模拟)
- android studio 的 Butter Knife:8.0.1的完整正确导入方法
- 浅谈Thrift内部实现原理
- 6. ZigZag Conversion
- android view 原理 -- measure 分析与应用
- c++实验6
- C++的一些简单示例
- C++学习笔记:转换构造函数与类型转换函数
- C# 压缩和解压文件
- 代码混淆 proguard相关配置过程记录
- 面试
- BP神经网络