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 ...
随机推荐
- 老李分享:robotium常用API 2
断言: 具体请查看官网 断言方法assert(robotium特有的断言方式,实际项目中和Junit的assert方法配合使用) void assertCurrentActivity (String ...
- 使用JS实现鼠标悬浮切换显示
实现的是在鼠标悬停在不同链接上,在同一位置切换显示想要显示的内容 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional// ...
- java nio(一)buffer
概述 常见的网络io分为两种情况,BIO(block-io)和NIO(non-block-io),分别位于java.io和java.nio. BIO,是阻塞的io,采用一个线程处理一个连接的方式,就算 ...
- Angular.js学习笔记 (二)
用A链接对象解析url的组成 var url = 'https://www.baidu.com:8080/aaa/1.html?id=10#name'; var aLink = document.cr ...
- 跟着刚哥梳理java知识点——深入理解String类(九)
一.String类 想要了解一个类,最好的办法就是看这个类的实现源代码,来看一下String类的源码: public final class String implements java.io.Ser ...
- java复习(9)---数据库JDBC
java写工程当然需要连接数据库.JDBC技术是连接数据库和应用程序的纽带,本节主要说明如何连接数据库. java中提供sql类. package re09; import java.sql.*; p ...
- Java设置Excel有效性
XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = wb.createSheet("Excel"); String[] ...
- SSH框架搭建 详细图文教程
转载请标明原文地址 一.什么是SSH? SSH是JavaEE中三种框架(Struts+Spring+Hibernate)的集成框架,是目前比较流行的一种Java Web开源框架. SSH主要用于Jav ...
- 转Fiddler 构造http请求
今天使用Fiddler构造一个POST请求,server端的PHP脚本的 $_POST数组中怎么也获取不到值,后来偶然发现是因为缺少了一个http头:Content-Type: application ...
- jade模板引擎简明用法
①.特性 首个单词为标签,有一些不能识别的标签可作为code,如each for case if else if unless zen coding风格添加标签,如 .nb#hello 生成 & ...