c/c++语言开发共享C++实现LeetCode(6.字型转换字符串)

[leetcode] 6. zigzag conversion 之字型转换字符串the string ”paypalishiring” is written in a zigzag


[leetcode] 6. 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”

example 2:

input: s = “paypalishiring”, numrows = 4
output: ”pinalsigyahrpi”
explanation:

p     i    n
a   l s  i g
y a   h r
p     i

这道题刚开始看了半天没看懂是咋样变换的,上网查了些资料,终于搞懂了,就是要把字符串摆成一个之字型的,比如有一个字符串 “0123456789abcdef”,转为 zigzag 如下所示:

当 n = 2 时:

0 2 4 6 8 a c e

1 3 5 7 9 b d f

当 n = 3 时:

0   4    8     c

1 3 5 7 9 b d f

2    6   a     e

当 n = 4 时:

0     6        c

1   5 7   b  d

2 4   8 a    e

3      9       f

可以发现,除了第一行和最后一行没有中间形成之字型的数字外,其他都有,而首位两行中相邻两个元素的 index 之差跟行数是相关的,为  2*nrows – 2, 根据这个特点,可以按顺序找到所有的黑色元素在元字符串的位置,将他们按顺序加到新字符串里面。对于红色元素出现的位置(github 上可能无法正常显示颜色,请参见)也是有规律的,每个红色元素的位置为 j + 2 x numrows-2 – 2 x i, 其中,j为前一个黑色元素的 index,i为当前行数。 比如当 n = 4 中的那个红色5,它的位置为 1 + 2 x 4-2 – 2 x 1 = 5,为原字符串的正确位置。知道了所有黑色元素和红色元素位置的正确算法,就可以一次性的把它们按顺序都加到新的字符串里面。代码如下:

解法一:

  class solution {  public:      string convert(string s, int numrows) {          if (numrows <= 1) return s;          string res;          int size = 2 * numrows - 2, n = s.size();          for (int i = 0; i < numrows; ++i) {              for (int j = i; j < n; j += size) {                  res += s[j];                  int pos = j + size - 2 * i;                  if (i != 0 && i != numrows - 1 && pos < n) res += s[pos];              }          }          return res;      }  };

若上面解法中的规律不是很好想的话,我们也可以用下面这种更直接的方法来做,建立一个大小为 numrows 的字符串数组,为的就是把之字形的数组整个存进去,然后再把每一行的字符拼接起来,就是想要的结果了。顺序就是按列进行遍历,首先前 numrows 个字符就是按顺序存在每行的第一个位置,然后就是 ‘之’ 字形的连接位置了,可以发现其实都是在行数区间 [1, numrows-2] 内,只要按顺序去取字符就可以了,最后把每行都拼接起来即为所求,参见代码如下:

解法二:

  class solution {  public:      string convert(string s, int numrows) {          if (numrows <= 1) return s;          string res;          int i = 0, n = s.size();          vector<string> vec(numrows);          while (i < n) {              for (int pos = 0; pos < numrows && i < n; ++pos) {                  vec[pos] += s[i++];              }              for (int pos = numrows - 2; pos >= 1 && i < n; --pos) {                  vec[pos] += s[i++];              }          }          for (auto &a : vec) res += a;          return res;      }  };

到此这篇关于c++实现leetcode(6.字型转换字符串)的文章就介绍到这了,更多相关c++实现字型转换字符串内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!

需要了解更多c/c++开发分享C++实现LeetCode(6.字型转换字符串),都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/648300.html

(0)
上一篇 2021年7月12日
下一篇 2021年7月12日

精彩推荐