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 ...
随机推荐
- JavaWeb总结(十)—文件上传和下载
一.文件的上传 1.文件的基本上传 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用Servlet获取上传文件的输入流然后再解析里面的请求参数是比较麻烦,所以一般选择 ...
- C++(浅析枚举类型-enum)
枚举类型 枚举类型(enumeration)是C++中的一种派生数据类型,它是由用户定义的若干枚举常量的集合. 如果一个变量只有几种可能的值,可以定义为枚举(enumeration)类型.所谓&quo ...
- JavaScript 格式化时间
//格式化 yyyy-MM-dd hh:mm:ss function renderTime(date) { if (date == '' || date == null) { return ''; } ...
- var的一些理解
var 是 variable(变量,可变物)的简写.在多种编程语言中,var 被用作定义变量的关键字,在一些操作系统中也能见到它的身影.类似object,但是效率比object高一点. var是一个局 ...
- 从零到实现Shiro中Authorization和Authentication的缓存
本文大纲 一.简介 二.缓存的概念 三.自定义实现缓存机制 四.什么是Ehcache 五.Ehcache怎么用 六.Spring对缓存的支持 七.Spring+Ehcache实现 八.Spring+S ...
- Linux常用命令快查
一.读取配置文件中某一个变量的值 假如有一个配置文件dubbo.properties,需要读取dubbo.application.name的值: dubbo.application.name=book ...
- php 启动过程 - sapi MSHUTDOWN 过程
php 启动过程 - sapi MSHUTDOWN 过程 概述 当服务器关闭时, 会走到 sapi MSHUTDOWN 过程 注册过程 本次内容是在 php 启动过程 - sapi MINIT 过程 ...
- (知识点)JavaScript继承
1)原型链 ①原型链示例 function Shape() { this.name = 'shape'; this.toString = function(){ return this.name; } ...
- ABP官方文档翻译 2.5 设置管理
设置管理 介绍 关于 ISettingStore 定义设置 设置范围 重写设置定义 获取设置值 服务端 客户端 更改设置 关于缓存 介绍 每个应用都需要存储设置,并且在应用的某些地方需要使用这些设置. ...
- 结构体的vector resize()与初始化
序: 我们在使用vector的时候可以自定义里面的数据类型.例如这样: struct Edge{ int from; int to; int weight; }; vector<Edge> ...