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. IronPython+Anthem.Net也玩 Ajax!

    在 IronPython 搭建的项目中(也可以是和 C# 的混合项目,详见我前一篇 post),可以使用 Anthem.NET 来轻松实现 Ajax 功能. 下面我简单的演示一个例子:在页面上我们分别 ...

  2. python mysql 查询返回字典结构

    cur = self.conn.cursor(MySQLdb.cursors.DictCursor)加上MySQLdb.cursors.DictCursor可以返回字典结构 {列名:值} class ...

  3. 【转】 Pro Android学习笔记(六六):安全和权限(3):Provider权限

    目录(?)[-] 访问其他应用的content provider Provider的读写权限 Provider的URI权限 Provider的granting 全局granting 部分URI的gra ...

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

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

  5. oracle--分页过程demo1

    oracle分页过程demo1: --ROWNUM用法 select o.*,rownum rn from (select * from emp) o where rownum<=10; sel ...

  6. stm32之UCOS-III

    一.UCOS-III 学习UCOS-III,一般会学习以下内容: 任务创建.删除.挂起.恢复等: 临界区:独占CPU,尽量少用,否则会降低效率: 时间管理:时钟节拍(基于硬件定时器).软件定时器: 互 ...

  7. Mac 远程连接Linux服务器及上传、下载命令

    1.使用ssh命令连接远程服务器主机 1.不设置端口,默认就是22 ssh root@192.168.18.129 1.1.设置端口例: ssh -p 22 root@192.168.18.1292. ...

  8. CURL访问举例

    <?php function request($url, $params = [], $requestMethod = 'GET', $jsonDecode = true, $headers = ...

  9. error C2512: “HelloWorld”: 没有合适的默认构造函数可用

    error C2512: "HelloWorld": 没有合适的默认构造函数可用 c++ newbie error C2512: no appropriate default co ...

  10. JavaScript正则表达式应用---replace()

    replace()方法使用一个替换值(replacement)替换掉一个匹配模式(pattern)在原字符串中某些或所有的匹配项,并返回替换后的字符串.这个替换模式可以是字符串或者RegExp(正则表 ...