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 ...
随机推荐
- java 8 bug
jpa保存实体的时候,不能用{{}}初始化对象,否则会报异常 org.springframework.dao.InvalidDataAccessApiUsageException: Unknown e ...
- docker容器的常见操作
进入容器 docker exec -it 12a022ee8127 /bin/bash 交互模式进入容器 docker exec -it 12a022ee8127 ip a 查看容器的ip等信息 批量 ...
- 第三记 Java面向对象
相信很多人都有听到,见到这么一句话:Java是一门面向对象编程的语言,但是又是否对这句话有了自己的理解呢? 一.面向对象 面向对象是一种新兴的程序设计方法,也可以说是一种新的程序设计规范(paradi ...
- while循环和for循环
while循环 n='我怎么那么可爱呀' count= while count<len(n): print(n[count]) count+= for循环 n='我怎么那么可爱呀' for i ...
- eclipse创建MAVEN项目是出现Could not resolve archetype的解决办法
eclipse第一次创建MAVEN项目时出现这个问题,查了很多文档改了没用,后来问了别人知道是maven中央仓库下载插件包失败就会报错. 解决办法: 用国内阿里云镜像会好很多 在settings.xm ...
- redis数据库操作
一.String(字符串)操作 String在redis中的存储是按照key-value的形式存储 1.SET key value [EX seconds] [PX milliseconds] [NX ...
- spark-sql中的分析函数的使用
分析函数的应用场景: (1)用于分组后组内排序 (2)指定计算范围 (3)Top N (4)累加计算 (5)层次计算 分析函数的一般语法: 分析函数的语法结构一般是: 分析函数名(参数) over ...
- 单源最短路径问题2 (Dijkstra算法)
用邻接矩阵 /* 单源最短路径问题2 (Dijkstra算法) 样例: 5 7 0 1 3 0 3 7 1 2 4 1 3 2 2 3 5 2 4 6 3 4 4 输出: [0, 3, 7, 5, 9 ...
- 微信小程序布局篇
刚刚接触小程序,小程序与HTML5有一定的差别,小程序就几个标签,而HTML5一大堆标签,还不断更新,但是新增标签功能强大.做一下微信小程序的布局练练手.感觉还是挺不错的,也封装了很多东西功能出来,与 ...
- ES,kibana通过nginx添加访问权限
一.安装nginx yum install epel-release -y yum install -y nginx 二.安装Apache Httpd 密码生成工具 # 生成密码 yum instal ...