LeetCode ZigZag Conversion
来源:互联网 发布:大数据开发入门的好书 编辑:程序博客网 时间:2024/06/03 18:42
一、题目
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 N
A P L S I I G
Y 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”.
二、代码
class Solution {public: string convert(string s, int numRows) { string str=""; if(numRows==1){ str = s; }else{ for(int line=0; line<numRows; line++){ for(int i=line; i<s.size(); i+=2*numRows-2){ str += s[i]; if(line>0 && line<numRows-1 && i+2*(numRows-1-line)<s.size()){ str += s[i+2*(numRows-1-line)]; } } } } return str; }};
阅读全文
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
- 微信个人网页版API的微信机器人
- centos 安装 svn 完整版
- 对 Jenkins+ANT+Jmeter 接口测试的实践
- 基于Haproxy+Keepalived负载均衡高可用架构
- HDU 5992 Finding Hotels
- LeetCode ZigZag Conversion
- Kaggle机器学习之模型融合(stacking)心得
- 转:linux as4下安装oracle10g终结版
- String对象常用操作
- centos7 安装composer
- 总结thinkphp快捷查询getBy、getField、getFieldBy用法及场景
- 解决虚拟机CentOSmini没有网络连接
- python_random.uinform
- eclipse maven项目pom报错web.xml is missing and <failOnMissingWebXml> is set to true