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. 1141. RSA Attack(RSA)

    1141 越来越喜欢数论了 很有意思 先看个RSA的介绍 RSA算法是一种非对称密码算法,所谓非对称,就是指该算法需要一对密钥,使用其中一个加密,则需要用另一个才能解密. RSA的算法涉及三个参数,n ...

  2. String.indexOf()

    int indexOf(int ch) 返回指定字符在此字符串中第一次出现处的索引. int indexOf(int ch, int fromIndex) 从指定的索引开始搜索,返回在此字符串中第一次 ...

  3. 在blade中定义一个可以被模版使用的变量

    laravel的blade中的数据一般由控制器传入,但是有没有什么办法临时在blade模版中创建并且被blade所使用吗? 答案是肯定的,不过语法稍微复杂一点 {{-- */$variableAvai ...

  4. HDU 5348 MZL's endless loop 给边定向(欧拉回路,最大流)

    题意: 给一个所有你可能想得到的奇葩无向图,要求给每条边定向,使得每个点的入度与出度之差不超过1.输出1表示定向往右,输出0表示定向往左. 思路: 网络流也是可以解决的!!应该挺简单理解的.但是由于复 ...

  5. 【英语】Bingo口语笔记(33) - 面部器官系列

    to play by ear Play就是玩的意思.可是,play by ear的意思并不是“玩耳朵”.这个词汇的来源和音乐有关系.它原来指的是那些会弹钢琴或某种乐器,但是却不会看五线谱的人.每当他们 ...

  6. DESCryptoServiceProvider加密、解密

    .net名称空间System.Security.Cryptography下DESCryptoServiceProvider类为我们提供了加密和解密方法,我们只需少许代码便可实现加密和解密. 稍感不托的 ...

  7. lseek()函数

    lseek()有个特殊的用途,确定文件是常规文件还是设备.<pre lang="c" escaped="true">off_t currpos;ou ...

  8. Android使用绘图Path总结

    Path作为Android中一种相对复杂的绘图方式,官方文档中的有些解释并不是很好理解,这里作一个相对全面一些的总结,供日后查看,也分享给大家,共同进步. 1.基本绘图方法 addArc(RectF ...

  9. NPOI 2.0导出word(docx格式)

    大名鼎鼎的NPOI用来导出EXCEL的文章园子里面有很多,可是用来导出WORD文档的文章大都含糊不清,最近刚好完成一个导出WORD文档的需求,在此分享下. NPOI里面认为word文档的最基本的结构是 ...

  10. C# 对Excel 单元格格式, 及行高、 列宽、 单元格边框线、 冻结设置

    一.对行高,列宽.单元格边框等的设置 这篇简短的文字对单元格的操作总结的比较全面,特此转载过来. private _Workbook _workBook = null; private Workshe ...