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. 数据库系统中事务的ACID原则

    事务的原子性.一致性.独立性及持久性 事务的原子性是指一个事务要么全部执行,要么不执行.也就是说一个事务不可能只执行了一半就停止了.比如你从取款机取钱,这个事务可以分成两个步骤:1划卡,2出钱.不可能 ...

  2. 安装cloudera

    1. 查看selinux状态 $ /usr/sbin/getenforce Enforcing $ /usr/sbin/sestatus SELinux status: enabled SELinux ...

  3. 深入理解Java对象的序列化与反序列化的应用

    当两个进程在进行远程通信时,彼此可以发送各种类型的数据.无论是何种类型的数据,都会以二进制序列的形式在网络上传送.发送方需要把这个Java对象转换为字节序列,才能在网络上传送:接收方则需要把字节序列再 ...

  4. 监控tomcat性能

    tomcat经常被用作中间件,也有直接作WEB的,自带的工具不是很给力,推荐以下的办法 工具/原料 javamelody 方法/步骤   下载 javamelody.jar和 jrobin-x.jar ...

  5. 宏ut_mem_block_t

    /** This struct is placed first in every allocated memory block */ typedef struct ut_mem_block_struc ...

  6. CSS3与页面布局学习总结(三)——BFC、定位、浮动、垂直居中

    一.BFC与IFC 1.1.BFC与IFC概要 BFC(Block Formatting Context)即“块级格式化上下文”, IFC(Inline Formatting Context)即行内格 ...

  7. MVC项目中应用富文本编辑器UEditor中的几个坑

    UEditor:百度出品 官网连接:http://ueditor.baidu.com/website/ 错误现象:在官网上复制到本地后,上传图片功能不能用, 控制台提示:“请求后台配置项http错误, ...

  8. HDU 2577 How to Type【DP】

    题意:给出一个字符串,有大写有小写,问最少的按键次数.然后打字的这个人有一个习惯,打完所有的字之后,指示灯要关闭. dp[i][j]表示打到第i个字母,j有0,1两个值表示指示灯开或者关的状态 然后就 ...

  9. acdream 1685 多民族王国(DFS,并查集)

    Problem Description 娜娜好不容易才回忆起自己是娜娜而不是什么Alice,也回忆起了自己要继续探索这个世界的目标,便偷偷溜出皇宫.娜娜发现这个王国有很多个民族组成,每个民族都有自己的 ...

  10. 多层感知机及其BP算法(Multi-Layer Perception)

    Deep Learning 近年来在各个领域都取得了 state-of-the-art 的效果,对于原始未加工且单独不可解释的特征尤为有效,传统的方法依赖手工选取特征,而 Neural Network ...