题目:

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日的更多相关文章

  1. 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 ...

  2. [分享] 从定制Win7母盘到封装详细教程 By BILL ( 10月23日补充说明 )

    [分享] 从定制Win7母盘到封装详细教程 By BILL ( 10月23日补充说明 ) billcheung 发表于 2011-10-23 00:07:49 https://www.itsk.com ...

  3. 11月23日《奥威Power-BI报表集成到其他系统》腾讯课堂开课啦

    听说明天全国各地区都要冷到爆了,要是天气冷到可以放假就好了.想象一下大冷天的一定要在被窝里度过才对嘛,索性明天晚上来个相约吧,相约在被窝里看奥威Power-BI公开课如何?        上周奥威公开 ...

  4. 2016年11月23日 星期三 --出埃及记 Exodus 20:14

    2016年11月23日 星期三 --出埃及记 Exodus 20:14 "You shall not commit adultery.不可奸淫.

  5. 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 ...

  6. 2016年6月23日 星期四 --出埃及记 Exodus 14:20

    2016年6月23日 星期四 --出埃及记 Exodus 14:20 coming between the armies of Egypt and Israel. Throughout the nig ...

  7. Week16(12月23日):复习

    Part I:提问 =========================== 1.声明强类型视图时,使用关键字(    ) A.ViewBag    B.model    C.Type    D.Tit ...

  8. 2017年3月23日 坚果性能测试Loadrunner 免费公开课

    2017-03-23  坚果性能测试1群 607937164  我昨天看了一下飞扬老师的讲义PPT,真的很棒,BAT的专业性能老师果然是有好几把刷子,十分受教,相信周四的公开课一定会让大家收益颇丰的. ...

  9. 成都Uber优步司机奖励政策(4月23日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

随机推荐

  1. (iOS)关于zbar扫描条形码,所搭载的设备

    四个月之前写的,现在发出来. 最近在开发一款程序的时候,功能要求扫描条形码. 现在最流行的扫描条形码的开源代码有zbar和zxing两种,可以支持多种一维和二维码. 之前了解过zbar,所以这次试用z ...

  2. mysql 分析5语句的优化--索引添加删除

    查看表的索引  show create table 表名; show index from 表名; show keys from表名; 添加索引 alter table 表名  add index 索 ...

  3. MySQL存储过程--带参数报错1064

    DELIMITER $$ USE `student`$$ DROP PROCEDURE IF EXISTS `sync_student`$$ CREATE DEFINER=`student`@`%` ...

  4. JS滚动加载

    var one = true;//设置一个全局变量 $(window).scroll(function () { var hight = document.body.scrollHeight - do ...

  5. ios ALAssetsLibrary简单的使用

    关于ALAssetsLibrary的简单使用有两个方面: 第一:存储图片/视频方法如下: // With a UIImage, the API user can use -[UIImage CGIma ...

  6. 优化Servlet:(利用反射的思想)

    1.创建BaseServlet (重写父类的service方法) package com.learning.web.servlet; import java.io.IOException; impor ...

  7. 如何用unity3d实现发送带附件的邮件

    以Gmail为例.点击屏幕的Capture按钮得到当前屏幕截图,点击Send按钮将之前的截图作为附件发送邮件. using UnityEngine; using System.Collections; ...

  8. Java Comparator的范型类型推导问题

    问题 在项目中,有一处地方需要对日期区间进行排序 我需要以日期区间的开始日为第一优先级,结束日为第二优先级进行排序 代码 我当时写的代码如下: List<Pair<LocalDate, L ...

  9. Android sdk配置 常见问题及处理方法

    1. 下载sdk压缩包,解压后显示 2.双击SDK Manager.exe 程序进入如下界面 注:有的童鞋可能遇到如下问题 一般将一和二两种操作都完成就OK了 一. 更新sdk,遇到了更新下载失败问题 ...

  10. Spring事务管理的实现方式之编程式事务与声明式事务详解

    原创说明:本博文为原创作品,绝非他处转载,转载请联系博主 1.上篇文章讲解了Spring事务的传播级别与隔离级别,以及分布式事务的简单配置,点击回看上篇文章 2.编程式事务:编码方式实现事务管理(代码 ...