15.12—细节实现题—ZigZag Conversion
来源:互联网 发布:linux mount sd卡 编辑:程序博客网 时间:2024/06/02 07:05
描述
e string ”PAYPALISHIRING” is wrien in a zigzag paern on a given number of rows like this: (you
may want to display this paern in a fixed font for beer 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”.
e string ”PAYPALISHIRING” is wrien in a zigzag paern on a given number of rows like this: (you
may want to display this paern in a fixed font for beer 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”.
#include<iostream>#include<string>using namespace std;string ZigZagCon(string str, int rows){if (rows == 1){string res = str;return res;}int len = str.size();string res;for (int i = 0; i<rows; i++){if (i == 0 || i == rows - 1){int j = i;while (j<len){res.push_back(str[j]);j = j + 2 * rows - 2;}}else{int j = i;int tmp = (rows - i - 1) * 2;while (j<len){res.push_back(str[j]);if (j + tmp<len)res.push_back(str[j + tmp]);j = 2 * rows + j - 2;}}}return res;}int main(){string str = "PAYPALISHIRING";int rows = 5;string res = ZigZagCon(str, rows);cout << res << endl;}
阅读全文
0 0
- 15.12—细节实现题—ZigZag Conversion
- LeetCode 之 ZigZag Conversion — C 实现
- LeetCode6—ZigZag Conversion
- leetcode—zigzag conversion
- LeetCode算法题——ZigZag Conversion
- LeetCode——ZigZag Conversion
- leetcode——ZigZag Conversion
- LeetCode6——ZigZag Conversion
- String——zigzag-conversion
- ZigZag Conversion —— leetcode
- Leetcode6——ZigZag Conversion
- <6>——ZigZag Conversion
- leetcode第6题——*ZigZag Conversion
- 【LeetCode】LeetCode——第6题:ZigZag Conversion
- LeetCode OJ 刷题日记——ZigZag Conversion
- 15.12 ZigZag Conversion
- leetcode 006 —— ZigZag Conversion
- LeetCode题解——6ZigZag Conversion
- move和forward源码分析[转]
- 15.11—细节实现题—Spiral Matrix II
- TreeListView树装列表节点选择设置为空
- yii2框架中使用自动搜索yii-widget-select2
- POJ3259 Wormholes 洛谷P3385 【模板】负环
- 15.12—细节实现题—ZigZag Conversion
- 15.13—细节实现题—Divide Two Integers
- 总价
- STM32初学篇-推挽输出、开漏输出、复用开漏输出、复用推挽输出以及上拉输入、下拉输入、浮空输入、模拟输入区别
- 15.14—细节实现题—Text Justification
- zookeeper-命令
- Spring-MyBatis(c3p0-alibaba-dbcp-springframework)识别jbdc。propertties文件
- 15.15—细节实现题—Max Points on a Line
- Java,AXIS,webService 调用 完整实例 xml 入参出参