Leetcode 6. Z字形转换
2018-03-02 13:26
381 查看
n=2*(numRows-1);
第一行 +n (+0) +n (+0)…
第二行 +n-2 +2 +n-2 +2 …
第三行 +n-4 +4 +n-4 +4 …
….
第numRows行 (+0) +n (+0) +n …
第一行 +n (+0) +n (+0)…
第二行 +n-2 +2 +n-2 +2 …
第三行 +n-4 +4 +n-4 +4 …
….
第numRows行 (+0) +n (+0) +n …
class Solution { public: string convert(string s, int numRows) { if (numRows == 1) return s; string ans; int diff = 2 * (numRows - 1); for (int k = 0; k < numRows; ++k) { int l = k, d = k << 1; d = d ? d : diff; while (l < s.size()) { ans += s[l]; if (d != diff) d = diff - d; l += d; } } return ans; } };
相关文章推荐
- LeetCode6Z字形转换
- LeetCode-6. ZigZag Conversion (JAVA)(之字形转换)
- LeetCode-Z字形转换
- leetcode:ZigZag Conversion 曲线转换
- 【LeetCode】将罗马数字转换成10进制数
- 【LeetCode笔记】Binary Tree Zigzag Level Order Traversal 二叉树Z字形遍历
- leetcode 583. Delete Operation for Two Strings 问题转换+最长公共子串 + DP动态规划
- 【LeetCode】之字形变换
- leetcode 85. Maximal Rectangle 最大子矩阵 + DP + 转换为最大直方图
- leetcode解题之 12&13. Integer to Roman Java版(罗马数字和数字的转换)
- [LeetCode] 281. Zigzag Iterator 之字形迭代器
- [LeetCode] Zigzag Iterator 之字形迭代器
- Leetcode (没有转换long long类型) Fraction to Recurring Decimal
- leetcode 罗马数字与整数的转换算法
- LeetCode Water and Jug Problem(巧妙转换为gcd问题)
- 【leetcode】第12题:将数字转换成罗马数字
- LeetCode | Integer to Roman(整数转换成罗马数字)
- [LeetCode系列]有序链表转换为平衡BST的递归解法
- LeetCode-13-Roman to Integer(罗马数字转换为整型数字)
- LeetCode Excel Sheet Column Title(表格列数数字到英文转换)