public class Solution {

     public static void main(String[] args) {
String s = "PAYPALISHIRING";
String res = convert(s, 4);
System.out.println(res);
} /**
* numRows=1和numRows=2为特殊情况
*/
public static String convert(String s, int numRows) {
String res = "";
int l = s.length();
if (l == 0) {
return "";
} if (l > 0 && l <= numRows) {
return s;
} if (numRows == 1) {
return s;
} // col为列数
int col = l / (2 * numRows - 2);
int remainder = l % (2 * numRows - 2);
if (remainder >= 0 && remainder <= numRows) {
col = 2 * col + 1;
}
if (remainder > numRows) {
col = 2 * col + 2;
} // temp为辅助数组
int[] temp = new int[col];
temp[0] = 1;
for (int i = 1; i < col; i++) {
temp[i] = 2 * i * (numRows - 1) - temp[i - 1];
}
for (int i = 0; i < numRows; i++) {
if (i == 0) {
int j = 0;
while (2 * j * (numRows - 1) < l) {
res += s.charAt(2 * j * (numRows - 1));
j++;
}
continue;
}
if (i == numRows - 1) {
int j = 0;
while ((2 * j + 1) * (numRows - 1) < l) {
res += s.charAt((2 * j + 1) * (numRows - 1));
j++;
}
continue;
}
for (int k = 0; k < col; k++) {
if (k == 0 && i < l) {
res += s.charAt(i);
continue;
} if(k%2==0){
if(temp[k]+i-1<l){
res += s.charAt(temp[k]+i-1);
continue;
}
} if (k % 2 == 1) {
if (temp[k] - i + 1 < l) {
res += s.charAt(temp[k] - i + 1);
continue;
}
}
break;
} } return res;
}
}

思路:

另一种解法:

 /**
* 时间复杂度也为O(n) 但更快
*/
public static String convert1(String s, int numRows) {
if(numRows==1) return s;
int x = 2 * (numRows-1); // distance between pipes |/|/|...
int len = s.length();
char[] c = new char[len];
int k =0;
for(int i=0; i < numRows; i++)
{
for(int j=i;j<len;j=j+x)
{
c[k++] = s.charAt(j);
if(i>0 && i<numRows-1 && j+x-2*i < len)
{
c[k++] = s.charAt(j+x-2*i); // extra character between pipes
}
}
}
return new String(c);
}

思路比我的更加清晰。通过加断点debug可以理解算法思想。

leetcode oj s_06的更多相关文章

  1. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  2. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  3. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  4. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

  5. LeetCode OJ 297. Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  6. 备份LeetCode OJ自己编写的代码

    常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...

  7. LeetCode OJ 之 Maximal Square (最大的正方形)

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

  8. LeetCode OJ:Integer to Roman(转换整数到罗马字符)

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  9. LeetCode OJ:Serialize and Deserialize Binary Tree(对树序列化以及解序列化)

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

随机推荐

  1. CF-358D-Dima and Hares【T^T+*^*】

    [文章标题打着转载,是因为不是自己想出来的解题,但下面的文字是自己敲的[~捂脸*>_<*~]] 题目就不贴了~~~DP+greedy的题.弱爆了看别人的代码思路过的.T^T但还是自己复述一 ...

  2. AOJ 2170 Marked Ancestor (基础并查集)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=45522 给定一棵树的n个节点,每个节点标号在1到n之间,1是树的根节点,有如 ...

  3. MTK平台缩写

    HSPA:High Speed Packet Access smartphone application processor,高速分组接入的智能手机应用程序处理器 META mode:Mobile E ...

  4. Codeforces Round #272 (Div. 2) C. Dreamoon and Sums (数学 思维)

    题目链接 这个题取模的时候挺坑的!!! 题意:div(x , b) / mod(x , b) = k( 1 <= k <= a).求x的和 分析: 我们知道mod(x % b)的取值范围为 ...

  5. 日期选择插件clndr的使用

    需求是:在HTML中绘制日历直接供用户选择 而不是使用datepicker之类的表单插件让用户点击input后弹出datepicker让用户选择 浏览了一些解决方案后,发现  CLNDR 这个jQue ...

  6. R语言缺失值信息处理

    mean(!is.na(mat))可以计算数据完整度(没有缺失值的) mean(!is.na(mat))>0.9,90%完整可以使用 # 缺失值的位置研究as.vector(attributes ...

  7. MornUI 源码阅读笔记

    1. label的mouseChildren属性为true,但label本身是不需要监听textfield的任何事件的, 个人猜测是为了给TextInput, TextArea用的,因为后两者需要监听 ...

  8. php 格式化数字 位数不足前面加0补足

    本文引用自 http://www.fengfly.com/plus/view-62827-1.html 补0: <?php $var = sprintf("%03d", 12 ...

  9. JVM内存结构——运行时数据区

    在Java虚拟机规范中将Java运行时数据划分为6种,分别为: PC寄存器(程序计数器) Java栈 堆 方法区 运行时常量池 本地方法栈 一.PC寄存器(程序计数器) PC寄存器(Program C ...

  10. 聊聊Dataguard的三种保护模式实验(上)

    Data Guard是Oracle高可用性HA的重要解决方案.针对不同的系统保护需求,DG提供了三种不同类型的保护模式(Protection Mode),分别为:最大保护(Maximum Protec ...