LeetCode-ZigZag Conversion
来源:互联网 发布:c语言编程器 编辑:程序博客网 时间:2024/06/03 20:40
原题:
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)
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”.
理解zigzag的含义:
从上至下,再从下至上走之字形状,分析规律:numRows=5,第一行间隔8=5*2-2,第二行间隔:8-2,2
第三行:8-4,4,以此类推。
class Solution {public: string convert(string s, int numRows) { if(numRows==1)return s; int n=s.size(); int interval=numRows*2-2; int i,k=0; string res(n,' '); for(i=0;i<n;i+=interval){ res[k++]=s[i]; } for(i=1;i<numRows-1;i++){ int inter=2*i; for(int j=i;j<n;j+=inter){ res[k++]=s[j]; inter=interval-inter; } } for(i=numRows-1;i<n;i+=interval){ res[k++]=s[i]; } return res; }};
阅读全文
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
- 经验 | 如何高效学Python?
- utuntu unicode打印中文
- 挑战程序设计竞赛 算法和数据结构 第12章 图
- STL容器Set和Multiset
- Pandas学习笔记:pandas基础
- LeetCode-ZigZag Conversion
- android-studio安装过程详解
- 开源地图MapBox自定义(二):基本概念
- Android文件上传带参数
- 重磅 | 周志华最新论文:首个基于决策树集成的自动编码器,表现优于DNN
- 笔记 | 吴恩达Coursera Deep Learning学习笔记
- keep-alive的activated,deactivated钩子,及列表scrollTop值存储,返回时还在该位置
- 资源 | AI、神经网络、机器学习、深度学习以及大数据学习备忘单
- 观点 | 李开复谈AI创业的“风口”和“泡沫”