leetcode ZigZag Conversion 006
来源:互联网 发布:php网站流量统计系统 编辑:程序博客网 时间:2024/06/04 18:02
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 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"
.Subscribe to see which companies asked this question
#include <iostream>#include <vector>#include <string>using namespace std;class Solution {public:string convert(string s, int numRows) {string ans="";if(s=="" || numRows==1) return s;int len = numRows+numRows-2;int num = s.length()/len, remain=s.length()-num*len;for(int i=0; i < num; i++) {ans+=s[i*len];}if(remain>0) ans+=s[num*len];for (int j = 1; j < numRows-1; ++j){for (int i = 0; i < num; ++i){ans += s[i*len+j];ans += s[i*len+j+2*(numRows-1-j)];}if(remain > 0) {if(num*len+j < s.length())ans += s[num*len+j];if(num*len+j+2*(numRows-1-j) < s.length())ans+= s[num*len+j+2*(numRows-1-j)];}}for (int i = 0; i < num; ++i){ans+=s[i*len+numRows-1];}if(remain > 0) {if(num*len+numRows-1 < s.length())ans+= s[num*len+numRows-1];}return ans;}};
0 0
- leetcode-006:ZigZag Conversion
- LeetCode 006 ZigZag Conversion
- LeetCode 006 ZigZag Conversion
- [LeetCode]-006-ZigZag Conversion
- 006 ZigZag Conversion [Leetcode]
- [LeetCode]006-ZigZag Conversion
- LeetCode 006 ZigZag Conversion
- leetcode ZigZag Conversion 006
- LeetCode 006 ZigZag Conversion
- leetcode-006 ZigZag Conversion
- leetcode 006 ZigZag Conversion
- LeetCode-006 ZigZag Conversion
- [LeetCode OJ][006]ZigZag Conversion
- leetcode 006 —— ZigZag Conversion
- leetcode之路006 ZigZag Conversion
- LeetCode: ZigZag Conversion
- LeetCode : ZigZag Conversion
- leetcode 13:zigzag conversion
- FastJson构造json串空的问题
- Window环境中开发Android之adb连接手机问题解决
- linux中如何快速在某目录下打开终端 及vim多文件调试
- uboot CMD 命令体系
- hdu2571 命运--DP
- leetcode ZigZag Conversion 006
- 排序——希尔排序(C++)
- gcc/g++编译
- android离线缓存技术
- HTML学习笔记
- Java经典设计模式详解
- 又是一个面试容易被问到的东西——C++内联
- iOS多线程比较、总结
- spi通信