Leetcode_Zig Zag Conversion
来源:互联网 发布:ubuntu安装office2013 编辑:程序博客网 时间:2024/05/05 15:15
<p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 13.63636302947998px; line-height: 30px;">The string <code style="box-sizing: border-box; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 12.727272033691406px; padding: 2px 4px; color: rgb(199, 37, 78); white-space: nowrap; background-color: rgb(249, 242, 244); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px;">"PAYPALISHIRING"</code> 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><pre style="box-sizing: border-box; font-family: Monaco, Menlo, Consolas, 'Courier New', monospace; font-size: 13px; white-space: pre-wrap; padding: 9.5px; margin-top: 0px; margin-bottom: 10px; line-height: 1.428571429; color: rgb(51, 51, 51); word-break: break-all; word-wrap: break-word; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px;">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"
.<span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 13.63636302947998px; line-height: 30px;">解题方法:</span>
<span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 13.63636302947998px; line-height: 30px;">该题没有什么特别之处,纯粹提取规律+跳出循环条件考虑</span>
<span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 13.63636302947998px; line-height: 30px;"></span>
string convert(string s, int nRows) {if(nRows == 1)return s;string ans = s;int k = 2*(nRows-1);int index = 0;for(int i = 0; i < nRows; ++i){int increment = k - 2*i;int currentindex = i;while(currentindex < s.size()){if(increment != 0){ans[index] = s[currentindex];++index;}currentindex += increment;increment = k - increment;}}return ans;}
0 0
- Leetcode_Zig Zag Conversion
- 4 - zig-zag conversion
- FOJ1091 Zig-Zag Lines
- SOJ-1640 Zig-Zag
- Zig-zag扫描
- zig-zag旋转
- SOJ--Zig-Zag
- MQL5:zig-zag 指标
- Conversion
- 生成 ZIG-ZAG 扫描顺序MATLAB代码
- FOJ Problem 1091 Zig-Zag Lines
- Zig-Zag(Z型)填数
- 对二维数组进行Zig-Zag扫描(C++)
- 对二维数组进行Zig-Zag扫描(C++)
- Audio Conversion
- About Conversion
- Conversion Functions
- conversion/lexical_cast
- 跟着辛星解读PHP中json的使用
- progressDialog not attached to window manager error!
- GeeksforGeeks: Given a sorted dictionary of an alien language, find order of characters
- Leetcode_String To Integer
- 辛星与您解读PHP页面跳转的几种实现方式
- Leetcode_Zig Zag Conversion
- 辛星和您彻底解读PHP中的引用
- android debug, print class name, line number
- Cache Operation in AngularJS
- 百度编辑器换皮肤,价格好商量
- LPCXpresso的下载
- 2014年7月24日 5点57分
- WEB开发中常用的子页面调用父页面方式
- C# Lua5.2.3 SandBox 结合