自己写的:

if(numRows == 1)   return s;
int ll = s.length() / 2 + 1;
Character tc[] = new Character[numRows* ll];
int i = 0, j = 0;
boolean down = true;
for(int k = 0; k < s.length(); k++){
tc[i* ll + j] = s.charAt(k);
if(down){
i++;
if(i == numRows){
i -= 2;
j ++;
down = false;
}
}else{
i--;
j++;
if(i == -1){
i = 1;
j --;
down = true;
}
}
}
String res = "";
for(i=0;i < tc.length; i ++){
if(tc[i] != null)
res += tc[i];
}
return res;

参考于discuss:

char[] c = s.toCharArray();
int len = c.length;
StringBuffer[] sb = new StringBuffer[numRows];
for (int i = 0; i < sb.length; i++) sb[i] = new StringBuffer(); int i = 0;
while(i < len){
for (int idx = 0; idx < numRows&&i < len; idx ++)
sb[idx].append(c[i++]);
for (int idx = numRows -2; idx >= 1 && i < len; idx --)
sb[idx].append(c[i++]);
}
for(int idx = 1;idx < sb.length; idx ++)
sb[0].append(sb[idx]);
return sb[0].toString();

leetcode 6. ZigZag Conversion [java]的更多相关文章

  1. LeetCode 6. ZigZag Conversion & 字符串

    ZigZag Conversion 看了三遍题目才懂,都有点怀疑自己是不是够聪明... 就是排成这个样子啦,然后从左往右逐行读取返回. 这题看起来很简单,做起来,应该也很简单. 通过位置计算行数: P ...

  2. Leetcode 6. ZigZag Conversion(找规律,水题)

    6. ZigZag Conversion Medium The string "PAYPALISHIRING" is written in a zigzag pattern on ...

  3. 【JAVA、C++】LeetCode 006 ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  4. Java [leetcode 6] ZigZag Conversion

    问题描述: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...

  5. 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]

    题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...

  6. LeetCode 6 ZigZag Conversion 模拟 难度:0

    https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in ...

  7. LeetCode 6 ZigZag Conversion(规律)

    题目来源:https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is writt ...

  8. leetcode:ZigZag Conversion 曲线转换

    Question: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of ...

  9. [LeetCode][Python]ZigZag Conversion

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/zigzag- ...

随机推荐

  1. javascript 创建对象方式

    本文主要是对<JavaScript高级程序设计>第六章(面向对象的程序设计)的总结,书上的这章至少看了4遍是有的.该章主要讲对象的创建与继承.其中创建对象和继承方式至少6种,再加上一些方法 ...

  2. Navicat---使用SSH远程连接到MySql,报错80070007: SSH Tunnel: Server does not support diffie-hellman-group1-sha1 for keyexchange

    尝试使用Navicat远程连接到我在阿里云服务器上的MySql,通过SSH. 但是报错: 80070007: SSH Tunnel: Server does not support diffie-he ...

  3. 分布式理论(五)—— 一致性算法 Paxos

    前言 Paxos 算法如同我们标题大图:世界上只有一种一致性算法,就是 Paxos.出自一位 google 大神之口. 同时,Paxos 也是出名的晦涩难懂,推理过程极其复杂.楼主在尝试理解 Paxo ...

  4. karma + jasmine 构建前端自动化测试

    http://blog.fens.me/nodejs-karma-jasmine/   很全的文档 执行karma init时报错如下: $ karma init > readline.js:5 ...

  5. [android] 看博客学习Android常见的几种RuntimeException

    异常分为两种: 1.编译时异常 当编译时异常抛出时,需要对其进行处理声明,否则编译不通过 2.运行时异常 编译时不检测,运行时 如果抛出,程序会立刻停止 NullPointerException 空指 ...

  6. Java基础——网络编程(三)

    TCP 网络编程 -- tcp 分为客户端和服务端 -- 客户端对应的对象是 Socket -- 服务端对应的对象是 ServerSocket -- 如果客户端先启动,则出现 connection r ...

  7. HDU1083(KB10-C 二分图最大匹配)

    Courses Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  8. Code Signal_练习题_stringsRearrangement

    Given an array of equal-length strings, check if it is possible to rearrange the strings in such a w ...

  9. Wampserver配置与本地建站

    ☆根目录修改问题 /.修改运行根目录 1.修改apache配置,将服务请求定位到新目录下 →左击wampserver,点击Apache打开httpd.conf文件,Ctrl+f搜索documentro ...

  10. web api 权限控制

    https://www.cnblogs.com/landeanfen/p/5287064.html 我只是个搬运工, 我只想存个档