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- ...
随机推荐
- GCD之后台程序运行
点击Home键进入后台时进行计时,直到从新启动,超过三分钟启动手势 // // AppDelegate.m // GCDDown // // Created by City--Online on 15 ...
- js获取指定格式的时间字符串
如下: // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1- ...
- HDU3592(差分约束)
World Exhibition Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU1698(KB7-E 线段树)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- thinkphp5+qrcode生成二维码
1.下载二维码插件Phpqrcode,地址 https://sourceforge.net/projects/phpqrcode/files/,把下载的文件夹放到\thinkphp\vendor下 2 ...
- 【代码笔记】iOS-performSelectorOnMainThread
代码: RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController : UIViewControl ...
- 高性能JavaScript(加载和执行)
当浏览器遇到 <script> 标签时,它是没办法知道 JavaScript 是否会向DOM中添加内容或引入其他元素,甚至关闭某一个标签.因此这个时候浏览器就会停止处理页面,先执行Java ...
- mongose + express 写REST API
一.准备工具 先确保电脑已经安装好nodejs 1.mongoose:安装非常简单: npm install mongoose --save [mongoose封装了mongodb的方法,调用mo ...
- JS--我发现,原来你是这样的JS(引用类型不简单[上篇],且听我娓娓道来)
一.介绍 没错,这是第五篇,到了引用类型,这次要分成两次博文了,太多内容了,这是前篇,篇幅很长也很多代码,主要讲引用类型和常用的引用类型,代码试验过的,老铁没毛病. 坚持看坚持写,不容易不容易,希望大 ...
- url override and HttpSession implements session for real
无论cookie有没有禁用,HttpSession都有效 package com.test; import javax.servlet.ServletException; import javax.s ...