LeetCode-ZigZag Conversion
来源:互联网 发布:传智播客2016java视频 编辑:程序博客网 时间:2024/04/28 08:45
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 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"
.Solution:
Code:
<span style="font-size:14px;">class Solution {public: string convert(string s, int nRows) { int length = s.size(); if (nRows == 1) return s; int group = 2+(nRows-2)*2; string result; int q = length/group; int r = length%group; for (int i = 0; i < nRows; ++i) { if (i == 0 || i == nRows-1) { for (int j = i; j < length; j += group) result += s[j]; } else { for (int j = i; j < length; j += group) { result += s[j]; if (j+2*nRows-2-2*i < length) result += s[j+2*nRows-2-2*i]; } } } return result; }};</span>
0 0
- LeetCode: ZigZag Conversion
- LeetCode : ZigZag Conversion
- leetcode 13:zigzag conversion
- [LeetCode] Zigzag Conversion
- LeetCode 6 - ZigZag Conversion
- Leetcode : ZigZag Conversion
- 【leetcode】Zigzag Conversion
- [LeetCode]ZigZag Conversion
- leetcode ZigZag Conversion
- LeetCode-ZigZag Conversion
- LeetCode ZigZag Conversion
- LeetCode 6: ZigZag Conversion
- leetcode-006:ZigZag Conversion
- Leetcode: ZigZag Conversion
- [LeetCode]ZigZag Conversion
- [LeetCode] ZigZag Conversion
- [LeetCode]ZigZag Conversion
- leetcode: ZigZag Conversion
- NT兼容系统——NT On Linux(NTOL)
- eclipse使用日记
- Ubuntu安装新内核
- The 3n + 1 problem
- 自己动手搭建Nginx+memcache+xdebug+php运行环境绿色版 For windows版
- LeetCode-ZigZag Conversion
- ExtJs4 笔记(12) Ext.toolbar.Toolbar 工具栏、Ext.toolbar.Paging 分页栏、Ext.ux.statusbar.StatusBar 状态栏
- JS权限验证
- android最新的adt adt-bundle-mac-x86_64-20140702
- jQuery EasyUI自定义validatebox
- 安卓获取TextView中字符串占屏幕宽度的方法
- 《自己动手写CPU》开始预售了!!
- STM32F107的时钟设置由25M修改成8M
- Linux系统之间拷贝文件的技巧总结