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. 二维码(2)二维码登录原理及Android客户端示例

    1,原理 服务器: 数据库: 建立一个2维码登录的数据表,产生一个登录页时,插入一条记录X,X含将要登录的用户名字段(初始为空),2维码中的数据字段(唯一) 登录页面: 在产生的2维码中包含关键数据Y ...

  2. 选错实施顾问公司 ERP项目九死一生

    今天接到一个朋友的电话,他是一家企业老总.这位老总感到非常头疼的是他的企业选择了一款国际上名气很大的ERP软件,但实施效果却强差人意.他的疑问是"不是说只要选对了ERP产品,谁实施都能成功吗 ...

  3. mongoDB使用复制还原数据库节省空间

    用db.copyDatabase可以备份复制数据的方法. 1.db.copyDatabase("from","to","127.0.0.1:16161 ...

  4. URAL1900 Brainwashing Device(dp)

    1900 二维dp挺好推 dp[i][j] = max(dp[i][j],dp[g][j-1]+o[i][i+1]-o[g][i+1])(i>g>=j-1) dp[i][j]表示第i个站台 ...

  5. laravel named route

    laravel中一般对于路由的使用方法是在routes.php中定义一个路由,在view中如果要引用一个url则直接通过<a href="url/">来使用. 但是随着 ...

  6. 【笨嘴拙舌WINDOWS】GDI绘制区域

    在默认情况下,Gdi绘画操作的使用白纸(窗口的客户区)黑字(Pen的颜色)!前面我们已经讲过如何改笔,现在来学习改变白纸(GDI的绘制区域) 正常的纸为一个矩形形状!有时候小孩不小心撕掉纸的一角,不小 ...

  7. UVa 11988 (数组模拟链表) Broken Keyboard (a.k.a. Beiju Text)

    题意: 模拟一个文本编辑器,可以输入字母数字下划线,如果遇到'['则认为是Home键,如果是']'则认作End键. 问最终屏幕上显示的结果是什么字符串. 分析: 如果在数组用大量的移动字符必然很耗时. ...

  8. codeforces 510 C Fox And Names【拓扑排序】

    题意:给出n串名字,表示字典序从小到大,求符合这样的字符串排列的字典序 先挨个地遍历字符串,遇到不相同的时候,加边,记录相应的入度 然后就是bfs的过程,如果某一点没有被访问过,且入度为0,则把它加入 ...

  9. 长期演进技术(LTE,Long Term Evolution)

    /********************************************************************************* * 长期演进技术(LTE,Long ...

  10. POJ2236 Wireless Network

    解题思路:简单并查集,注意时间限制是10000MS,每次进行O操作之后,   进行一次for循环,进行相关调整.同时注意输入输出格式,见代码: #include<cstdio> #incl ...