[leetcode] ZigZag Conversion *
2013-03-09 00:21
357 查看
/* * Just find the rule; Do not forget to check * special cases (e.g. nRows = 1 ...) * * @author: HZT * @date: 2013-3-9 */ #include <iostream> #include <string> using namespace std; class Solution { public: string convert(string s, int nRows) { // Start typing your C/C++ solution below // DO NOT write int main() function // DO NOT forget this!! if(nRows == 1) return s; if(s.empty()) return s; int len = s.length(); char* ans = new char[len+1]; int idx, ansIdx=0; int delta = 2 * nRows - 2; // each row for(int r=1; r<=nRows; r++){ idx = r; while(idx <= len){ ans[ansIdx++] = s[idx-1]; if(r!=1 && r!=nRows && 2*nRows-2*r+idx <= len){ ans[ansIdx++] = s[2*nRows-2*r+idx-1]; } idx += delta; } } ans[len] = '\0'; return ans; } }; int main(){ string s = "PAYPALISHIRING"; int nRows = 3; //string s = "A"; //int nRows = 1; cout << (new Solution)->convert(s, 3) << endl; return 0; }
相关文章推荐
- LeetCode_ZigZag Conversion_006
- LeetCode OJ ZigZag Conversion
- LeetCode - ZigZag Conversion
- LeetCode-6-ZigZag-Conversion 找规律水题
- LeetCode-6(ZigZag Conversion)
- leetcode 6. ZigZag Conversion golang实现
- LeetCode6—ZigZag Conversion
- Leetcode 6. ZigZag Conversion The Solution of Python
- leetcode之ZigZag Conversion
- Leetcode ZigZag conversion
- LeetCode ZigZag Conversion
- Leetcode ZigZag Conversion
- leetcode ZigZag Conversion
- leetcode题解||ZigZag Conversion问题
- LeetCode 6 ZigZag Conversion(Z型转换)
- LeetCode 6 ZigZag Conversion (C,C++,Java,Python)
- [Leetcode]6. ZigZag Conversion @python
- [leetcode] 6. ZigZag Conversion
- leetcode 006 ZigZag Conversion
- leetcode ZigZag Conversion 006