[Leetcode]006. ZigZag Conversion
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的更多相关文章
- 【JAVA、C++】LeetCode 006 ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- No.006 ZigZag Conversion
6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...
- LeetCode--No.006 ZigZag Conversion
6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...
- LeetCode 6. ZigZag Conversion & 字符串
ZigZag Conversion 看了三遍题目才懂,都有点怀疑自己是不是够聪明... 就是排成这个样子啦,然后从左往右逐行读取返回. 这题看起来很简单,做起来,应该也很简单. 通过位置计算行数: P ...
- Leetcode 6. ZigZag Conversion(找规律,水题)
6. ZigZag Conversion Medium The string "PAYPALISHIRING" is written in a zigzag pattern on ...
- 【LeetCode】006. ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- LeetCode 6 ZigZag Conversion 模拟 难度:0
https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in ...
- LeetCode 6 ZigZag Conversion(规律)
题目来源:https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is writt ...
- [LeetCode][Python]ZigZag Conversion
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/zigzag- ...
随机推荐
- 【转】Lucas定理 & 逆元学习小结
(From:离殇灬孤狼) 这个Lucas定理是解决组合数的时候用的,当然是比较大的组合数了.比如C(1000000,50000)% mod,这个mod肯定是要取的,要不算出来真的是天文数字了. 对于一 ...
- NYOJ-小猴子下落
描述 有一颗二叉树,最大深度为D,且所有叶子的深度都相同.所有结点从左到右从上到下的编号为1,2,3,·····,2的D次方减1.在结点1处放一个小猴子,它会往下跑.每个内结点上都有一个开关,初始全部 ...
- LOJ2303 「NOI2017」蚯蚓排队
「NOI2017」蚯蚓排队 题目描述 蚯蚓幼儿园有$n$只蚯蚓.幼儿园园长神刀手为了管理方便,时常让这些蚯蚓们列队表演. 所有蚯蚓用从$1$到$n$的连续正整数编号.每只蚯蚓的长度可以用一个正整数表示 ...
- bzoj 3926: 诸神眷顾的幻想乡 广义后缀自动机
题目: Description 幽香是全幻想乡里最受人欢迎的萌妹子,这天,是幽香的2600岁生日,无数幽香的粉丝到了幽香家门前的太阳花田上来为幽香庆祝生日. 粉丝们非常热情,自发组织表演了一系列节目给 ...
- Ajax知识点整理
一.javascript原生Ajax 1.简介 Ajax是Asynchronous JavaScript+XML(异步JavaScript和XML)的缩写. 该名称诞生于XML还是数据传输首选格式的时 ...
- Poj 1458 Common Subsequence(LCS)
一.Description A subsequence of a given sequence is the given sequence with some elements (possible n ...
- windows服务和进程的区别和联系
Windows Service 是主要用于服务器环境而长期运行的应用程序, 这类程序不需要有用户界面或者任何模拟输出. 任何的用户消息通常都是记录在Windows 事件日志里.Windows Serv ...
- sql 根据年份、月份查询数据
CREATE TABLE [dbo].[T_UserAccess]( ,) NOT NULL, [UserId] [int] NULL, [UserType] [int] NULL, ) NULL, ...
- 串口发送Hex数组
void MainWindow::String2Hex(QString str, QByteArray &senddata) { int hexdata,lowhexdata; ; int l ...
- Activity--弹出底部窗口
第一步 : 退出时候的布局文件exit_dialog_from_settings.xml <?xml version="1.0" encoding="UTF-8&q ...