LeetCodeZigZag Conversion
Posted 你瞅啥
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCodeZigZag Conversion相关的知识,希望对你有一定的参考价值。
题目如下:
C++代码:
#include <iostream> #include <string> using namespace std; class Solution { public: string convert(string s, int numRows) { if (numRows <= 1 || s.length() <= numRows) return s; string result; int ZigSpan = 2 * numRows - 2; for (int i = 0; i < numRows; i++){ for (int j = i; j < s.length(); j += ZigSpan){ result.push_back(s[j]); if (i != 0 && i != numRows - 1){ int t = j + ZigSpan - 2 * i; if (t < s.length()) result.push_back(s[t]); } } } return result; } }; void ZigZag(){ string s = "abcdefghijklmn"; Solution *ss = new Solution(); cout << ss->convert(s, 3) << endl; }
以上是关于LeetCodeZigZag Conversion的主要内容,如果未能解决你的问题,请参考以下文章