leetcode 6. ZigZag Conversion [java]
自己写的:
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]的更多相关文章
- 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 ...
- 【JAVA、C++】LeetCode 006 ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- Java [leetcode 6] ZigZag Conversion
问题描述: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...
- 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]
题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- 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:ZigZag Conversion 曲线转换
Question: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of ...
- [LeetCode][Python]ZigZag Conversion
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/zigzag- ...
随机推荐
- Silverlight 在ie8 下 报2152 错误
前几天改别人的一个silverlight程序,在项目属性上 选中了 “通过使用应用程序库缓存减小XAP 大小”,编译无错,发布无错误. 放服务器上测试: 站点绑定域名,使用ie9.ie10 都没有问题 ...
- Redis-发布与订阅
发布与订阅(pub/sub)的特点是订阅者负责订阅频道发送者负责向频道发送二进制字符串消息.每当有消息发送至给定频道时,频道的所有订阅者都会收到消息 (发布和订阅命令) SUBSCRIBE : SUB ...
- js 中格式化时间
在js中常常要求对时间的输出格式进行格式化,比如 2017-01-01 10:10,比较麻烦的是月,日,小时,分.它们的格式一般要求两位,如果小于10的话需要在前边补0,当然这算不上什么问题,可以通过 ...
- $.each()和$(selector).each()
转载:http://www.jb51.net/article/65215.htm $.each()与$(selector).each()不同, 后者专用于jquery对象的遍历, 前者可用于遍历任何的 ...
- MYSQL查询优化(Ⅰ)
一. 通过查询缓冲提高查询速度 一般我们使用SQL语句进行查询时,数据库服务器每次在收到客户端 发来SQL后,都会执行这条SQL语句.但当在一定间隔内(如1分钟内),接到完全一样的SQL语句,也同样执 ...
- Spring IOC 容器源码分析
声明!非原创,本文出处 Spring 最重要的概念是 IOC 和 AOP,本篇文章其实就是要带领大家来分析下 Spring 的 IOC 容器.既然大家平时都要用到 Spring,怎么可以不好好了解 S ...
- oracle 中如何查询当前用户可以看到的表名、表对应的所有字段
前言:利用 oracle 的视图来查询表的相关信息. oracle 查询当前用户下的表名 + 表注释 select t.table_name tableName, f.comments comment ...
- Django中连接redis
1. 安装 pip install django-redis 2. settings中配置 CACHES = { "default": { "BACKEND": ...
- webstorm激活
选择 License server http://idea.imsxm.com/ http://idea.iteblog.com/key.php (2016.11.16) http://v2mc.ne ...
- linux学习笔记-目录结构(1)
每个linux系统的目录结构差不多,因为有FHS(Filesystem Hierarchy Standard)标准的规范. FHS的重点在于规范每个特定的目录下应该要放什么样的数据. FHS依据文件系 ...