leetcode oj s_06
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的更多相关文章
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- LeetCode OJ学习
一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...
- 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 ...
- 备份LeetCode OJ自己编写的代码
常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 加密解密(7)*PKI基础知识(完整)
PKI 基础知识 摘要 本白皮书介绍了加密和公钥基本结构(PKI)的概念和使用 Microsoft Windows 2000 Server 操作系统中的证书服务的基础知识.如果您还不熟悉加密和公钥技术 ...
- JS里面匿名函数的调用 & 变量作用域的实验
参考 http://www.educity.cn/wenda/54753.html 已实验验证结果正确. 1.下列哪些正确?(B.C) A.function(){ alert("Here!& ...
- Cocos2d-x 开发手记
1.所有的源文件统一新建到Classes里,否则无法找到源文件,这样也便于跨平台编译 2.绘图坐标系,与opengl采用相同坐标系,左下角为原点 纹理坐标系,以左上角为原点 3.最近有在学习C ...
- Spring事务报Connection is read-only
昨天做项目时,写了个方法,程序突然报了Connection is readonly. Queries leading to data modification are not allowed调了程序半 ...
- 使用public key来做SSH authentication
public key authentication(公钥认证)是对通过敲用户名.密码方式登录服务器的一种替代办法.这种方法更加安全更具有适应性,但是更难以配置. 传统的密码认证方式中,你通过证明你你知 ...
- Mybatis 插入与批量插入以及多参数批量删除
实体类: import java.io.Serializable; public class AttachmentTable implements Serializable { private sta ...
- UVa 11468 (AC自动机 概率DP) Substring
将K个模板串构成一个AC自动机,那些能匹配到的单词节点都称之为禁止节点. 然后问题就变成了在Tire树上走L步且不经过禁止节点的概率. 根据全概率公式用记忆化搜索求解. #include <cs ...
- How to begin with the webpage making
1.网页制作三剑客必须要会使用.(dreamweaver /fireworks/flash)2.学习些最基层的html语言的知识,3.在学习一些基本的html标签(要多加练习哦)4.先试着用表格进行 ...
- BZOJ 1202 狡猾的商人
前缀和+带权并查集. #include<iostream> #include<cstdio> #include<cstring> #include<algor ...
- libserialport: cross-platform library for accessing serial ports
/*********************************************************************************** * libserialport ...