6-Z字形变换
6-Z字形变换
将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。
比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下:
L C I R
E T O E S I I G
E D H N
之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"LCIRETOESIIGEDHN"。
请你实现这个将字符串进行指定行数变换的函数:
string convert(string s, int numRows);
示例 1:
输入: s = "LEETCODEISHIRING", numRows = 3
输出: "LCIRETOESIIGEDHN"
示例 2:
输入: s = "LEETCODEISHIRING", numRows = 4
输出: "LDREOEIIECIHNTSG"
解释:
L D R
E O E I I
E C I H N
T S G
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/zigzag-conversion
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
public String convert(String s, int numRows) {
if(s == null || s.equals("") || numRows == 1) {
return s;
}
int numCols = (s.length() / (numRows * 2 - 2) + 1) * (numRows - 1);
char[][] chs = new char[numRows][numCols];
int i = 0;
int j = 0;
char[] ss = s.toCharArray();
chs[i][j] = ss[0];
for (int k = 1; k < ss.length; ) {
// 往下填充
while (i < numRows - 1 && k < ss.length) {
chs[++i][j] = ss[k++];
}
// 左下到右上
while (i > 0 && k < ss.length) {
chs[--i][++j] = ss[k++];
}
}
StringBuilder sb = new StringBuilder();
for (char[] c : chs) {
for (char ch : c) {
if (ch != 0) {
sb.append(ch);
}
}
}
return sb.toString();
}
找规律
public String convert(String s, int numRows) {
if(s == null || s.equals("") || numRows == 1) {
return s;
}
int step = numRows * 2 - 2;
char[] ss = s.toCharArray();
StringBuilder sb = new StringBuilder();
// i = 0
for(int i = 0; i < s.length(); i += step) {
sb.append(ss[i]);
}
// i = 1 | i = step-1
for(int k = 1; k < numRows - 1; k++) {
int j = step - k;
for (int i = k; i < s.length(); i += step, j += step) {
sb.append(ss[i]);
if(j < s.length()) {
sb.append(ss[j]);
}
}
}
// i = numRows-1
for(int i = numRows - 1; i < s.length(); i += step) {
sb.append(ss[i]);
}
return sb.toString();
}
官方题解
class Solution {
public String convert(String s, int numRows) {
if (numRows == 1) return s;
List<StringBuilder> rows = new ArrayList<>();
for (int i = 0; i < Math.min(numRows, s.length()); i++)
rows.add(new StringBuilder());
int curRow = 0;
boolean goingDown = false;
for (char c : s.toCharArray()) {
rows.get(curRow).append(c);
if (curRow == 0 || curRow == numRows - 1) goingDown = !goingDown;
curRow += goingDown ? 1 : -1;
}
StringBuilder ret = new StringBuilder();
for (StringBuilder row : rows) ret.append(row);
return ret.toString();
}
}
作者:LeetCode
链接:https://leetcode-cn.com/problems/zigzag-conversion/solution/z-zi-xing-bian-huan-by-leetcode/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
6-Z字形变换的更多相关文章
- C#版[击败100.00%的提交] - Leetcode 6. Z字形变换 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- Leetcode题库——6.Z字形变换
@author: ZZQ @software: PyCharm @file: convert.py @time: 2018/9/20 20:12 要求: Z字形变换 将字符串 "PAYPAL ...
- LeetCode Golang 6. Z 字形变换
6. Z 字形变换 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: L ...
- Z 字形变换 C++实现 java实现 leetcode系列(六)
Z 字形变换 java实现 C++实现 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 ...
- Leetcode(6)Z字形变换
Leetcode(6)Z字形变换 [题目表述]: 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" ...
- Java实现 LeetCode 6 Z字形变换
6. Z 字形变换 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: L ...
- leetcode刷题六<z字形变换>
将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 时,排列如下: L C I R E T O E S I I G E D H N 之后,你的输出需要从左往右逐 ...
- [Swift]LeetCode6. Z字形变换 | ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- [LeetCode] 6. Z 字形变换
题目链接:(https://leetcode-cn.com/problems/zigzag-conversion/) 题目描述: 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列 ...
- Z 字形变换
将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: L C I R E T ...
随机推荐
- CSS颜色表示的几种方式
在CSS中颜色有很多表示方式,今天列出一些常见的颜色表示方式及它们的用法. ①color:blue; 第一种,调用颜色属性,将颜色的英文输入在冒号后,以分号结束. 这种方法直接了当,但是能表示的颜色 ...
- Android头像更换之详细操作
Android开发之头像的更换(拍照,从手机照片中选择) 先说一下昨天未解决的问题:原因是自己在获取对象时,没有将新加的图片属性加到该对象里,导致一直爆空指针异常. 接下来分析一下头像更换的具体操作: ...
- Shiro自动登录
Shiro RememberMe spring.xml <bean class="org.apache.shiro.web.mgt.DefaultWebSecurityManager& ...
- 强烈推荐 10 款珍藏的 Chrome 浏览器插件
Firebug 的年代,我是火狐(Mozilla Firefox)浏览器的死忠:但后来不知道为什么,该插件停止了开发,导致我不得不寻求一个新的网页开发工具.那段时间,不少人开始推荐 Chrome 浏览 ...
- 使用 Visual Studio 2015 + Python3.6 + tensorflow 构建神经网络时报错:'utf-8' codec can't decode byte 0xcc in position 78: invalid continuation byte
使用 Visual Studio 2015 + Python3.6 + tensorflow 构建神经网络时报错:'utf-8' codec can't decode byte 0xcc in pos ...
- Classmethod and Staticmethod - Python 类方法 和 静态方法
classmethod and staticmethod classmethod 的是一个参数是类对象 cls (本类,或者子类), 而不是实例对象 instance (普通方法). classmet ...
- Linux访问权限控制及时间同步实践
1.编写脚本/root/bin/checkip.sh,每5分钟检查一次,如果发现通过ssh登录失败 次数超过10次,自动将此远程IP放入Tcp Wrapper的黑名单中予以禁止防问 方式一:脚本+定时 ...
- SpringProfile轻松切换多环境配置文件
在项目开发的过程中,我们难免会遇到开发.测试.生产等环境的切换,而各个环境的配置肯定是不同的.传统的办法是在项目打包的时候修改配置文件.但人为做的事情难免产生意外.Spring 为我们提供了一种多环境 ...
- mysql8.0编译安装
#下载依赖 yum install -y ncurses ncurses-devel cmake bison bison-devel openssl openssl-libs openssl-deve ...
- Android Webview H5资源本地化
Android Webview H5资源本地化 一. 创建读取资源项目独立模块 1. 项目依赖的好处 符合模块化的思想,他们相互独立.一个项目持有另一个项目的引用,修改更加方便. (注:compile ...