public class Solution {
public String convert(String s, int nRows) {
if (s == null || s.isEmpty() || s.length() <= nRows || nRows == 1) {
return s;
} int length = s.length(); StringBuilder sb = new StringBuilder(); int step = 2 * (nRows - 1);
int count = 0; for (int i = 0; i < nRows; i++){
int interval = step - 2 * i; for (int j = i; j < length; j += step){
sb.append(s.charAt(j));
count++;
if (interval > 0 && interval < step && j + interval < length && count < length) {
sb.append(s.charAt(j + interval));
count++;
}
}
}
return sb.toString();
}
}

[Leetcode]006. ZigZag Conversion的更多相关文章

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

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

  2. No.006 ZigZag Conversion

    6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...

  3. LeetCode--No.006 ZigZag Conversion

    6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...

  4. LeetCode 6. ZigZag Conversion & 字符串

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

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

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

  6. 【LeetCode】006. ZigZag Conversion

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

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

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

  8. LeetCode 6 ZigZag Conversion(规律)

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

  9. [LeetCode][Python]ZigZag Conversion

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

随机推荐

  1. Arc065_E Manhattan Compass

    平面上有$N$个点$(X_i\space, Y_i)$,定义$D(a,b)=|X_a-X_b|+|Y_a-Y_b|$. 如果你当前在$(p,q)$,这个无序二元组(即$(p,q)$和$(q,p)$被认 ...

  2. POJ1422 Air Raid 和 CH6902 Vani和Cl2捉迷藏

    Air Raid Language:Default Air Raid Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9547 A ...

  3. 判断iOS系统的Model

    获取iOS系统的Model   (参考网址:https://www.theiphonewiki.com/wiki/Models) + (NSString *)getModel{ struct utsn ...

  4. CF 914 G Sum the Fibonacci —— 子集卷积,FWT

    题目:http://codeforces.com/contest/914/problem/G 其实就是把各种都用子集卷积和FWT卷起来算即可: 注意乘 Fibonacci 数组的位置: 子集卷积时不能 ...

  5. QT4.8.5+qt-vs-addin-1.1.11+VS2010安装配置和QT工程的新建和加载

    1.下载windows下的QT库 QT4.8.5 for vs2010: http://download.qt-project.org/official_releases/qt/4.8/4.8.5/q ...

  6. 用户收到"无法显示页面"的错误消息和"Connections_refused"条目记录在运行 Windows Server 2003,Exchange 2003 和 IIS 6.0 的服务器上的 Httperr.log 文件

    症状 您会遇到下列症状在运行 Microsoft Windows Server 2003. Microsoft Exchange Server 2003年和 Microsoft Internet In ...

  7. 开源一个windows消息队列查看器

    windows消息简单易用,在异步消息发送场景的使用还是比较适合.为了方便查看队列中的消息和删除一些异常消息,开发了一个简单的小工具拿出来分享下. 源码地址:https://gitee.com/eab ...

  8. python变量、类型、运算、输出

    1.变量.类型.运算.输出等 # -*- coding: utf-8 -*- a=2 b=3 c=a+b print u'结果是=%i'%c #加u显示中文 str=unicode(s,"u ...

  9. lsnrctl start错误Linux Error: 29: Illegal seek (翻译:非法谋取)

    现在,想不起来为什么ORACLE的监听,怎么就突然无法起来了呢. 好吧,问题反正就是发生了. lsnrctl start 遇到如下错误, LSNRCTL for Linux: Version 10.2 ...

  10. matlab基本语法和运算基础

    转载自:http://blog.csdn.net/robertcalm/article/details/51582253 matlab语法比较随意,但正如其全名 matrix &laborat ...