网站首页 > 知识剖析 正文
2023-04-28:将一个给定字符串 s 根据给定的行数 numRows 以从上往下、从左到右进行 Z 字形排列 比如输入字符串为 "PAYPALISHIRING" 行数为 3 时,排列如下 P A H N A P L S I I G Y I R 之后,你的输出需要从左往右逐行读取,产生出一个新的字符串 "PAHNAPLSIIGYIR" 请你实现这个将字符串进行指定行数变换的函数 string convert(string s, int numRows)。
答案2023-04-28:
算法过程大体可以分为以下步骤:
1.计算给定字符串 s 的长度 n 和指定行数 numRows。
2.如果 numRows 等于 1 或者 numRows 大于等于 n,则返回原始字符串 s。
3.计算一个周期 t,其值为 2 * (numRows - 1)。
4.创建一个字符数组 ans,其长度与输入字符串 s 相同,并用空格符初始化。
5.根据 Z 字形排列的规律,按顺序遍历每一行 i(从第 0 行到第 numRows-1 行)及其对应的列 j(每一列长度为 t)。在遍历的过程中,根据当前所在行的位置 i 和周期 t,计算出对应列的顶部的行号 nextColTop。
6.对于每个字符 s[j],将其填入字符数组 ans 中,并将 fill 指针向后移动一位。如果该字符所在的行不是第 0 行和最后一行,并且在下一个周期中对应的位置 nextColTop-i 小于字符串的长度 n,则将 s[nextColTop-i] 也填入 ans 数组中,并将 fill 指针再次向后移动一位。
7.遍历完所有行和列后,将字符数组 ans 转换为字符串并返回。
时间复杂度:O(n),其中 n 是字符串 s 的长度。我们只需要遍历一次字符串 s。
空间复杂度:O(n),我们需要使用一个字符数组 ans 存储变换后的字符串,数组的大小为输入字符串 s 的长度 n。另外,我们还使用了常数级别的额外空间存储变换时需要的一些变量。
go完整代码如下:
package main
import "fmt"
func convert(s string, row int) string {
n := len(s)
if row == 1 || row >= n {
return s
}
t := 2 * (row - 1)
ans := make([]byte, n)
fill := 0
for i := 0; i < row; i++ {
nextColTop := t
for j := i; j < n; j += t {
ans[fill] = s[j]
fill++
if i >= 1 && i <= row-2 && nextColTop-i < n {
ans[fill] = s[nextColTop-i]
fill++
}
nextColTop += t
}
}
return string(ans)
}
func main() {
s := "PAYPALISHIRING"
result := convert(s, 3)
fmt.Println(result)
}
在这里插入图片描述
rust完整代码如下:
fn convert(s: String, row: i32) -> String {
let n = s.chars().count();
if row == 1 || row >= n as i32 {
return s;
}
let t = 2 * (row - 1);
let mut ans: Vec<char> = vec![' '; n];
let mut fill = 0;
for i in 0..row {
let mut next_col_top = t;
for j in (i as usize..n).step_by(t as usize) {
ans[fill] = s.chars().nth(j).unwrap();
fill += 1;
if i >= 1 && i <= row - 2 && next_col_top - i < n as i32 {
ans[fill] = s.chars().nth((next_col_top - i) as usize).unwrap();
fill += 1;
}
next_col_top += t;
}
}
ans.iter().collect()
}
fn main() {
let s = "PAYPALISHIRING".to_string();
let result = convert(s, 3);
println!("{}", result);
}
在这里插入图片描述
c++完整代码如下:
#include <iostream>
#include <string>
using namespace std;
string convert(string s, int row) {
int n = s.length();
if (row == 1 || row >= n) {
return s;
}
int t = 2 * (row - 1);
string ans(n, ' ');
int fill = 0;
for (int i = 0; i < row; i++) {
int nextColTop = t;
for (int j = i; j < n; j += t, nextColTop += t) {
ans[fill++] = s[j];
if (i >= 1 && i <= row - 2 && nextColTop - i < n) {
ans[fill++] = s[nextColTop - i];
}
}
}
return ans;
}
int main() {
string s = "PAYPALISHIRING";
string result = convert(s, 3);
cout << result << endl;
return 0;
}
在这里插入图片描述
c完整代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* convert(char* s, int row) {
int n = strlen(s);
if (row == 1 || row >= n) {
return s;
}
int t = 2 * (row - 1);
char* ans = (char*)malloc(sizeof(char) * (n + 1));
memset(ans, ' ', sizeof(char) * n);
int fill = 0;
for (int i = 0; i < row; i++) {
int nextColTop = t;
for (int j = i; j < n; j += t, nextColTop += t) {
ans[fill++] = s[j];
if (i >= 1 && i <= row - 2 && nextColTop - i < n) {
ans[fill++] = s[nextColTop - i];
}
}
}
ans[n] = '\0';
return ans;
}
int main() {
char s[] = "PAYPALISHIRING";
char* result = convert(s, 3);
printf("%s\n", result);
free(result);
return 0;
}
在这里插入图片描述
- 上一篇: “烟蒂装瓶”挑战正在风靡全球,你会参加吗?
- 下一篇: 花10分钟学一项技能 ImageJ 图像分析
猜你喜欢
- 2024-11-24 英国降温在即,你的过冬大衣准备好了吗?2021英国羽绒服推荐
- 2024-11-24 世界威士忌爱好者最喜欢的10款威士忌,都喝得起
- 2024-11-24 60行代码实现经典论文:0.7秒搞定泊松盘采样,比Numpy快100倍
- 2024-11-24 为什么跑步后吃不下,游完泳却变饿死鬼?
- 2024-11-24 通过事例重温一下 JS 中 常见的15 种数组操作(备忘清单),收藏
- 2024-11-24 无括号和svg的xss构造利用
- 2024-11-24 游泳减肥和跑步减肥,哪个效果更好?
- 2024-11-24 圣诞节快到了,用python、turtle画棵圣诞树吧
- 2024-11-24 花10分钟学一项技能 ImageJ 图像分析
- 2024-11-24 “烟蒂装瓶”挑战正在风靡全球,你会参加吗?
- 04-29php开发者composer使用看这一篇就够了
- 04-29引用和变量声明在不同语言中的实作
- 04-29PHP 没你想的那么差
- 04-29Ubuntu linux 上的 Nginx 和 Php 安装
- 04-29CentOS下通过yum搭建lnmp(单版本PHP)
- 04-29为什么 PHP8 是个高性能版本
- 04-29PHP8函数包含文件-PHP8知识详解
- 04-29使用无参数函数进行命令执行
- 最近发表
- 标签列表
-
- xml (46)
- css animation (57)
- array_slice (60)
- htmlspecialchars (54)
- position: absolute (54)
- datediff函数 (47)
- array_pop (49)
- jsmap (52)
- toggleclass (43)
- console.time (63)
- .sql (41)
- ahref (40)
- js json.parse (59)
- html复选框 (60)
- css 透明 (44)
- css 颜色 (47)
- php replace (41)
- css nth-child (48)
- min-height (40)
- xml schema (44)
- css 最后一个元素 (46)
- location.origin (44)
- table border (49)
- html tr (40)
- video controls (49)