6.[leetcode] ZigZag Conversion
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".
原题链接:https://leetcode.com/problems/zigzag-conversion/
Analysis & Solution
There exits pattern in the ZigZag Conversion. The indexs at i row follows patterns:
Let DiffConstant = 2*row - 2
i = 0: the difference = DiffConstant
i = 1: the differences = 2(row-1) - 2, DiffConstant - (2(row-1) - 2).
i = 2: the differences = 2(row-2) - 2, DiffConstant - (2(row-2) - 2).
i = k: the differences = 2(row-k) - 2, DiffConstant - (2(row-k) - 2) = 2k
public String convert(String s, int numRows) {
if(numRows == 1){
return s;
}
int diffConstant = 2*numRows - 2, i, index;
StringBuffer res = new StringBuffer();
for(i = 0; i < numRows; i++){
index = i;
boolean isEven = true;
while(index < s.length()){
res.append(s.charAt(index));
if(i == 0 || i == numRows -1){
index += diffConstant;
}else{
if(isEven){
index += 2*(numRows-i) - 2;
}else{
index += 2*i;
}
isEven = !isEven;
}
}
}
return res.toString();
}
6.[leetcode] ZigZag Conversion的更多相关文章
- LeetCode ZigZag Conversion(将字符串排成z字型)
class Solution { public: string convert(string s, int nRows) { string a=""; int len=s.leng ...
- LeetCode: ZigZag Conversion 解题报告
ZigZag ConversionThe string "PAYPALISHIRING" is written in a zigzag pattern on a given num ...
- [leetcode]ZigZag Conversion @ Python
原题地址:https://oj.leetcode.com/problems/zigzag-conversion/ 题意: The string "PAYPALISHIRING" i ...
- [LeetCode]ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- LeetCode——ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- [LeetCode] ZigZag Conversion [9]
称号 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- C++ leetcode::ZigZag Conversion
mmp,写完没保存,又得重新写.晚上写了简历,感觉身体被掏空,大学两年半所经历的事,一张A4纸都写不满,真是一事无成呢.这操蛋的生活到底想对我这个小猫咪做什么. 今后要做一个早起的好宝宝~晚起就诅咒自 ...
- Leetcode:ZigZag Conversion分析和实现
问题的大意就是将字符串中的字符按锯齿状(倒N形)垂直由上向下放置,最后水平从左向右读取.比如 ABCDEFGHIJKLMN,4表示 A G M B F H ...
- LeetCode解题报告—— 2 Keys Keyboard & Longest Palindromic Substring & ZigZag Conversion
1. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...
随机推荐
- 执行指定iframe页面的脚本
mark一下,通过jQuery执行指定iframe页面里面的脚本,当前仅知道页面名称. $(window.top.document).find('iframe[src="pagesrc&qu ...
- 解决fatal error LNK1168的终极方法
很多人的VC++或Visual studio 会出现fatal error LNK1168错误很是头疼,MS也说不清, 什么改权限.用户名.注册表.CMD,卸载杀毒软件...一切都瞎扯,除非reins ...
- elasticsearch 的安装配置与spring boot的整合应用
linux上的elasticsearch安装 一.下载elasticsearch 直接进入elasticsearch的官网,下载最新的安装包:https://www.elastic.co/downlo ...
- mvn -DskipTests和-Dmaven.test.skip=true区别
在使用mvn package进行编译.打包时,Maven会执行src/test/java中的JUnit测试用例,有时为了跳过测试,会使用参数-DskipTests和-Dmaven.test.skip= ...
- 写给小前端er的nodejs,mongodb后端小攻略~ (windows系统~)
一.写在前面 迫于学校的压力,研二上准备回学校做实验发论文了,感觉真的没意思,这几天学着搞搞后端,踩了很多坑,整理一下这几天的坑以免以后再犯! 二.本文主要内容(由于是面向前端同学的,所以前端的内容就 ...
- Python3.6下scrapy框架的安装
首先考虑使用最简单的方法安装 pip install scrapy 命令安装,提示 Failed building wheel for Twisted Microsoft Visual C++ 14. ...
- ESXI的安装和部署
1. 实验拓扑图: 2. 实验要求 (1) 新建一台exsi主机,安装exsi5.5系统. 步骤: 1)新建虚拟机,导入光盘. 2)安装esxi系统 (2)在exsi主机中,配置IP地址为1 ...
- WAMP下配置FCGID+ZendGuardLoader
公司的项目里,有几个文件是被加密的,经过一翻折腾,终于配置成功 文件加密技术用的是ZendGuard,所以必须安装的PHP必须得是nts的 一.下载并配置PHP 先下载安装php,注意VC版本和是否n ...
- linux yum命令 使用
yum -y install 包名(支持*) :自动选择y,全自动 yum install 包名(支持*) :手动选择y or n yum remove 包名(不支持*) rpm -ivh 包名(支持 ...
- SM干货篇:你应该具备的提问技巧!
在成为Scrum Master(SM)之前,我曾担任过许多团队的技术负责人.工作内容之一就是做决定,而且我认为自己做得挺好:坚定果断是我性格的一部分. 然而,当我成为Scrum Master之后,这样 ...