LeetCode第六题—— ZigZag Conversion(字符串的“之”字形转换)
题目描述:
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string s, int numRows);
Example 1:
Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"
Explanation:
P A H N
A P L S I I G
Y I R
Example 2:
Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:
P I N
A L S I G
Y A H R
P I
My Solution(19ms,38.4MB)
首先想到的办法,有点绕,用一个二维数组按照之字形储存起来,然后逐行逐列地去读取,然后拼接成一个字符串。
class Solution {
public String convert(String s, int numRows) {
if(numRows<2){return s;}//防止除数为零
int index = 0,row=numRows-2;
//columns实际上是指二维数组列下表的最大值
int columns = s.length()/(2*numRows-2)*(numRows-1);
int remainder = s.length()%(2*numRows-2);
if(remainder>numRows){columns = remainder - numRows + columns;}
String[][] zigzag = new String[numRows][columns+1];
//开始逐行填充数组
for(int j = 0;j<columns+1;j++){
if(j%(numRows-1)==0){//竖笔
for(int i = 0;i<numRows;i++){
if(index<s.length()){
zigzag[i][j] = s.substring(index,index+1);index++;
row = numRows-2;
}
}
}else{
if(index<s.length()){
zigzag[row][j] = s.substring(index,index+1);index++;
row--;
}
}
}
//开始拼接结果字符串
String result = "";
for(int i =0 ; i<numRows ;i++){
for(int j=0;j<columns+1;j++){
if(zigzag[i][j]!=null){
result = result + zigzag[i][j];
}
}
}
return result;
}
}
Visit By Row(3ms,36MB)
发现数字间的规律,直接逐行去读取(效率最高)
class Solution {
public String convert(String s, int numRows) {
if (numRows == 1) return s;
StringBuilder ret = new StringBuilder();
int n = s.length();
int cycleLen = 2 * numRows - 2;
//逐行去读取
for (int i = 0; i < numRows; i++) {
for (int j = 0; j + i < n; j += cycleLen) {
ret.append(s.charAt(j + i));
//排除第一行和最后一行,因为两竖笔之间没有数字
if (i != 0 && i != numRows - 1 && j + cycleLen - i < n)
ret.append(s.charAt(j + cycleLen - i));
}
}
return ret.toString();
}
}
LeetCode第六题—— ZigZag Conversion(字符串的“之”字形转换)的更多相关文章
- leetcode第六题 ZigZag Conversion (java)
ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...
- leetcode第六题--ZigZag Conversion
Problem: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of r ...
- LeetCode 6. ZigZag Conversion & 字符串
ZigZag Conversion 看了三遍题目才懂,都有点怀疑自己是不是够聪明... 就是排成这个样子啦,然后从左往右逐行读取返回. 这题看起来很简单,做起来,应该也很简单. 通过位置计算行数: P ...
- Leetcode 6 ZigZag Conversion 字符串处理
题意:将字符串排成Z字形. PAHNAPLSIIGYIR 如果是5的话,是这样排的 P I AP YR H L G N SI A I 于是,少年少女们,自己去找规律吧 提示:每个Z ...
- 【leetcode❤python】 6. ZigZag Conversion
#-*- coding: UTF-8 -*- #ZigZag Conversion :之字型class Solution(object): def convert(self, s, numRow ...
- [leetcode]6. ZigZag Conversion字符串Z形排列
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- LeetCode(6) ZigZag Conversion
题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- LeetCode(6)ZigZag Conversion
题目如下: C++代码: #include <iostream> #include <string> using namespace std; class Solution { ...
- LeetCode ZigZag Conversion(将字符串排成z字型)
class Solution { public: string convert(string s, int nRows) { string a=""; int len=s.leng ...
随机推荐
- scala 列表List
列表: 列表是不可变,也就是说不能通过赋值改变列表的元素: 列表有递归结构,而数据是连续的 List 类型:List() 同样也是List(String) 列表是基于Nil (是空的)和::(列表从前 ...
- java中Date日期类型的大小比较
方法一:java.util.Date类实现了Comparable接口,可以直接调用Date的compareTo()方法来比较大小 String beginTime = "2018-07-28 ...
- 2018年第九届蓝桥杯B组第四题:摔手机题解
摔手机 摔手机 动态规划 在蓝桥杯的时候遇到一次 当时没有做对 看了题解也没明白 如今再次遇到这个类似的题目 于是拿出来补补吧 摔手机题目如下: 星球的居民脾气不太好,但好在他们生气的时候唯一的 ...
- NetCore2.2使用Nlog自定义日志写入路径配置方式
在一些特定场景的业务需求下,日志需要写入到不同的路径下提供日志分析.第一种:默认Nlog可以通过日志级别来区分路径,——优点是不需要额外配置,开箱即用——缺点是不够灵活,如果超过级别数量,则不满足需求 ...
- tomcat启动内存修改
# USE_NOHUP (Optional) If set to the string true the start command will # ...
- vue 外卖app(3) 利用slot分发内容
1. 增加一个HeaderTop.vue <template> <header class="header"> <slot name="le ...
- 【BZOJ 3569】DZY Loves Chinese II
题面 Description 神校XJ之学霸兮,Dzy皇考曰JC. 摄提贞于孟陬兮,惟庚寅Dzy以降. 纷Dzy既有此内美兮,又重之以修能. 遂降临于OI界,欲以神力而凌♂辱众生. 今Dzy有一魞歄图 ...
- 每天进步一点点-WPF-根据数据类型加载控件
目的,根据数据类型的不同,动态的加载适用于不同数据类型的控件(布局) 原理:为自定义的数据类型添加数据魔板,绑定的时候绑定这些数据类型的实例. 例子: 数据类型: 数据模板: <DataTemp ...
- 解决MySQL登录密码正确却提示错误-1045的方法
MySQL密码正确却无法本地登录-1045 Access denied for user 'root'@'localhost' (using password:YES MySQL密码正确却无法本地登录 ...
- electron-vue中关闭烦人的es语法检查
本项目环境是 electron-vue搭建的项目,项目结构根vue-cli创建的项目结构稍微有所不同 主要修改的地方有3个 把这3个文件里面的以下代码全部删掉 { test: /\.(js)$/, e ...