ZigZag Conversion2015年6月23日
题目:
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".
思路:
1、通过字符串的长度和numRows大小算出商和余数
2、通过商和余数判断每一行元素的个数,各行元素个数存放在int[] row
3、逐行对应出每一行元素在源字符串中的位置,并根据位置取出相应字符添加到返回字符串的末尾
解答:
/ test cases passed.
Status: Accepted
Runtime: ms
Submitted: minutes ago
public class Solution {
public String convert(String s, int numRows) {
StringBuilder result = new StringBuilder("");
int len = s.length();
if(numRows == 1 || len<=numRows){
return s;
}
int quotient = len / (2*numRows -2);
int remainder = len % (2*numRows -2);
int[] row = new int[numRows];
//求数组row
for(int i=0; i<numRows; i++){
if(i==0){
if(remainder > i) {
row[i] = quotient + 1;
}else{
row[i] = quotient;
}
}else if(i == numRows -1){
if(remainder > i) {
row[i] = quotient + 1;
}else{
row[i] = quotient;
}
}else{
if(remainder > i && remainder <= numRows) {
row[i] = 2*quotient + 1;
}else if(remainder > numRows){
if((remainder - numRows) >= (numRows-1-i)){
row[i] = 2*quotient + 2;
}else{
row[i] = 2*quotient + 1;
}
}else{
row[i] = 2*quotient;
}
}
}
//逐行取出源字符串对应位置的字符添加到result
for(int i=0; i<numRows; i++) {
if(i==0 || i==numRows-1){
for(int j=0; j<row[i]; j++ ){
result.append(s.charAt(i+j*(2*numRows-2)));
}
}else{
for(int j=0; j<row[i]; j++ ){
if(j%2==0){
result.append(s.charAt(i+j/2*(2*numRows-2)));
}else{
result.append(s.charAt(i+(j-1)/2*(2*numRows-2)+2*numRows-2-2*i));
}
}
}
}
return result.toString();
}
}
ZigZag Conversion2015年6月23日的更多相关文章
- 2016年12月23日 星期五 --出埃及记 Exodus 21:18
2016年12月23日 星期五 --出埃及记 Exodus 21:18 "If men quarrel and one hits the other with a stone or with ...
- [分享] 从定制Win7母盘到封装详细教程 By BILL ( 10月23日补充说明 )
[分享] 从定制Win7母盘到封装详细教程 By BILL ( 10月23日补充说明 ) billcheung 发表于 2011-10-23 00:07:49 https://www.itsk.com ...
- 11月23日《奥威Power-BI报表集成到其他系统》腾讯课堂开课啦
听说明天全国各地区都要冷到爆了,要是天气冷到可以放假就好了.想象一下大冷天的一定要在被窝里度过才对嘛,索性明天晚上来个相约吧,相约在被窝里看奥威Power-BI公开课如何? 上周奥威公开 ...
- 2016年11月23日 星期三 --出埃及记 Exodus 20:14
2016年11月23日 星期三 --出埃及记 Exodus 20:14 "You shall not commit adultery.不可奸淫.
- 2016年10月23日 星期日 --出埃及记 Exodus 19:7
2016年10月23日 星期日 --出埃及记 Exodus 19:7 So Moses went back and summoned the elders of the people and set ...
- 2016年6月23日 星期四 --出埃及记 Exodus 14:20
2016年6月23日 星期四 --出埃及记 Exodus 14:20 coming between the armies of Egypt and Israel. Throughout the nig ...
- Week16(12月23日):复习
Part I:提问 =========================== 1.声明强类型视图时,使用关键字( ) A.ViewBag B.model C.Type D.Tit ...
- 2017年3月23日 坚果性能测试Loadrunner 免费公开课
2017-03-23 坚果性能测试1群 607937164 我昨天看了一下飞扬老师的讲义PPT,真的很棒,BAT的专业性能老师果然是有好几把刷子,十分受教,相信周四的公开课一定会让大家收益颇丰的. ...
- 成都Uber优步司机奖励政策(4月23日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
随机推荐
- Omi官方插件系列 - omi-transform介绍
原文链接 - https://github.com/AlloyTeam/omi/tree/master/tutorial 写在前面 Omi框架正式发布了omi-transform作为Omi组件化开发特 ...
- AngularJS1.X学习笔记6-控制器和作用域
经过一番艰苦卓绝的鏖战,我终于来到了控制器和作用域部分.控制器作为MVC的C,其重要性不可谓不重要:作用域决定了你可以拿到哪些东西,亦是分外重要.现在就来学习一下两个东西.去看看$apply,$wat ...
- 神奇的marquee--滚动的文字
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 【Direct2D开发】 通过操作像素实现纹理混合
转载请注明出处:http://www.cnblogs.com/Ray1024 一.概述 我们都知道Direct2D可以加载并显示图片,但是不知道你有没有想过,这个2D的图形引擎可以进行纹理混合吗?如果 ...
- 进入html+css世界的正确姿势
今天,我带大家一起走进html+css的世界. HTML其实是HyperText Markup Language的缩写, 超文本标记语言.他是用于告诉浏览器这是一个网页, 也就是说告诉浏览器我是一个H ...
- android通过代码获取华为手机的EMUI系统版本号
因为app中用到华为推送,但是华为推送在不同版本上是存在不同问题的,需要单独来处理. 那么最基本的问题是要获取EMUI系统的版本号. 上网翻了很多博客帖子,基本上是在获取root权限下去读取/syst ...
- angular的$http.post()提交数据到Java后台接收不到参数值问题的解决方法
本文地址:http://www.cnblogs.com/jying/p/6733408.html 转载请注明出处: 写此文的背景:在工作学习使用angular的$http.post()提交数据时, ...
- angular中路由的实现(针对新手)
最近搜了一下网上的教程,看完总感觉有点糊涂,对于新手来说可能云里雾里,所以写一个最简单的路由来给大家做个指引. 首先当然需要准备angular,下载地址:https://angular.io/ 现在a ...
- Java 中的接口有什么作用?好处?
接口的作用就是把使用接口的人和实现接口的人分开,实现接口的人不必要关心谁去使用,而使用接口的人也不用关心谁实现的接口,由接口将他们联系在一起. 很多JAVA初级程序员对于接口存在的意义很疑惑.不知道接 ...
- Mac os下安装brew
1.首先没下载xcode,请先安装xcode,安装的继续往下面看 2.安装brew /usr/bin/ruby -e "$(curl -fsSL https://raw.githubuser ...