题目:

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. CSS3学习笔记(3)-CSS3边框

    p{ font-size: 15px; } .alexrootdiv>div{ background: #eeeeee; border: 1px solid #aaa; width: 99%; ...

  2. Best Time to Buy and Sell Stock系列

    I题 Say you have an array for which the ith element is the price of a given stock on day i. If you we ...

  3. 1094:零起点学算法01——第一个程序Hello World!

    Description 题目很简单 输出"Hello World!"(不含引号),并换行. Input 没有输入 Output 输出"Hello World!" ...

  4. js alert(“”)弹框 自定义样式

    首先用css渲染一个样式 #msg{ height: 2rem; text-align: center; position: fixed; top: 50%; margin-top: -1rem; l ...

  5. Java环境变量详解

    自己总结些再加抄点: 安装JDK后要配置环境变量,主要有三个: 1 JAVA_HOME ->为JDK的安装目录,如:F:\JAVA\jdk1.6.0_04 2 CLASSPATH ->到哪 ...

  6. linux常用操作命令

    cd: cd /data 进入目录 cd .. 返回上级菜单tar: tar -cvf jcms20170411.tar.gz jcms/ 将jcms文件夹打包为 jcms20170411.tar.g ...

  7. Centos下装eclipse测试Hadoop

    (一),安装eclipse 1,下载eclipse,点这里 2,将文件上传到Centos7,可以用WinSCP 3,解压并安装eclipse [root@Master opt]# tar zxvf ' ...

  8. 设备offline时如何自动重置

    在linux底层 Linux/include/uapi/linux/usbdevice_fs.h中,重置_IO('U', 20)可以重置usb设备. 因此,我们可以在脚本中利用这个方法去重置USB 代 ...

  9. 动画制作 手机APP制作以及响应式的实现

    babber 宽度:浏览器的100% 原则上:高度-=图片高度 banner img 宽度:图片的实际宽度 高度:充满父容器 使用定位:让图片在父容器中绝对居中.     <!DOCTYPE h ...

  10. css常用的属性方法 上篇

    自己是从java后台自学转前端的,所以平时一些简单的css+html就不写了,列出的都是新手常用的一些属性,会持续更新,大神勿喷,留给新手做个参考! 尤其是跟我一样自学前端的.     背景关联 ba ...