翻译

字符串“PAYPALISHIRING”通过一个给定的行数写成如下这种Z型模式:
P A H N
A P L S I I G
Y I R

然后一行一行的读取:“PAHNAPLSIIGYIR”

写代码读入一个字符串并通过给定的行数做这个转换:

string convert(string text, int nRows);

调用convert(“PAYPALISHIRING”, 3),应该返回”PAHNAPLSIIGYIR”。

原文

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 text, int nRows);
convert(“PAYPALISHIRING”, 3) should return “PAHNAPLSIIGYIR”.

分析

如果还是没明白题目的意思,看下图吧……

public class Solution
{public string Convert(string s, int numRows){if (numRows == 1)return s;StringBuilder strBuilder = new StringBuilder();int lengthOfGroup = 2 * numRows - 2;        // 如上图所示,每组的长度为4     for (int row = 0; row < numRows; row++)      // 按从第0行到numRows-1行的顺序遍历  {if (row == 0 || row == numRows - 1)        // 此处负责第0行和numRows-1行{for (int j = row; j < s.Length; j += lengthOfGroup){strBuilder.Append(s[j]);}}else                   // 此处负责第0行和numRows-1行之间的所有行{int currentRow = row;           // 在当前行中向右移动(看上图)bool flag = true;int childLenOfGroup1 = 2 * (numRows - 1 - row);                //  怎么说呢……中间行的各个索引吧int childLenOfGroup2 = lengthOfGroup - childLenOfGroup1;while (currentRow < s.Length){strBuilder.Append(s[currentRow]);if (flag)currentRow += childLenOfGroup1;elsecurrentRow += childLenOfGroup2;flag = !flag;}}}return strBuilder.ToString();}
}

C++的代码肯定是有的:

class Solution {
public:string convert(string s, int numRows) {      if(numRows==1)return s;string str="";int lengthOfGroup=2*numRows-2;for(int row=0;row<numRows;row++){if(row==0||row==numRows-1){for(int currentRow=row;currentRow<s.length();currentRow+=lengthOfGroup){str+=s[currentRow];}}else{int currentRow=row;bool flag=true;int childLenOfGroup1=2*(numRows-1-row);int childLenOfGroup2=lengthOfGroup-childLenOfGroup1;while(currentRow<s.length()){str+=s[currentRow];if(flag)currentRow+=childLenOfGroup1;elsecurrentRow+=childLenOfGroup2;flag=!flag;}}}return str;   }
};

至于Java嘛,当然也有……不过每次我都是直接把C#的代码拷贝过去然后改改就好了。

public class Solution {public String convert(String s, int numRows) {if (numRows == 1)return s;StringBuilder strBuilder = new StringBuilder();int lengthOfGroup = 2 * numRows - 2;       for(int row=0; row < numRows; row++){if (row == 0 || row == numRows - 1){for(int currentRow = row; currentRow < s.length(); currentRow += lengthOfGroup){strBuilder.append(s.charAt(currentRow));}}else{int currentRow = row;        boolean flag = true;int childLenOfGroup1 = 2 * (numRows - 1 - row);           int childLenOfGroup2 = lengthOfGroup - childLenOfGroup1;while (currentRow <s.length()){strBuilder.append(s.charAt(currentRow));if (flag)currentRow += childLenOfGroup1;elsecurrentRow += childLenOfGroup2;flag = !flag;}}}return strBuilder.toString();}
}
updated at 2016/09/17

做一个小幅修改~

还是Java代码,这里首先将String转成了Char数组,其实不转也一样。while循环中有两个for,一个是如上图中的向下遍历,一个向上遍历,向上遍历的时候也只是取中间段。因为虽然图形上画出来是倾斜的,但是在Char数组中其实还是连续的,所以也不用过多担心。

后面的for循环,其实只是将所有的StringBuilder,集中到第一个然后作为输出而已。

    public String convert(String s, int numRows) {char[] c = s.toCharArray();int len = c.length;StringBuilder[] sb = new StringBuilder[numRows];for (int i = 0; i < sb.length; i++) {sb[i] = new StringBuilder();}int index = 0;while (index < len) {for (int i = 0; i < numRows && index < len; i++) {sb[i].append(c[index++]);}for (int i = numRows - 2; i >= 1 && index < len; i--) {sb[i].append(c[index++]);}}for (int i = 1; i < sb.length; i++) {sb[0].append(sb[i]);}return sb[0].toString();}

LeetCode 6 ZigZag Conversion(Z型转换)(String)相关推荐

  1. [勇者闯LeetCode] 6. ZigZag Conversion

    [勇者闯LeetCode] 6. ZigZag Conversion Description The string "PAYPALISHIRING" is written in a ...

  2. [LeetCode题解] ZigZag Conversion

    原文在这,可以来我blog翻翻哦. 第二天.今天AC掉了一道之前没AC掉的题目... 今天的题目是6. ZigZag Conversion 题目描述: The string "PAYPALI ...

  3. LeetCode 6. ZigZag Conversion

    原题链接在这里:https://leetcode.com/problems/zigzag-conversion/ 题目: The string "PAYPALISHIRING" i ...

  4. leetCode 6. ZigZag Conversion 字符串 (上传费劲)

    6. ZigZag Conversion 题目:https://leetcode.com/problems/zigzag-conversion/ 1 2 3 4 5 6 7 8 9 10 11 12 ...

  5. 【LeetCode】6. ZigZag Conversion Z 字形变换

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:字形变换,ZigZag,题解,Leetcode, 力扣,P ...

  6. 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]

    题目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...

  7. LeetCode 6 - ZigZag Conversion

    原题如下: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...

  8. LeetCode-6:ZigZag Conversion(Z字形变换)

    题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...

  9. 【leetcode】ZigZag Conversion

    题目简述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...

最新文章

  1. 楼盘历史价格管理导入功能优化
  2. SOR迭代求解线性方程组代码实现
  3. UA MATH571A 检验异方差的非参数回归方法
  4. 三味Capsule:矩阵Capsule与EM路由
  5. nosql怎么使用_使用NoSQL实施实体服务–第5部分:使用云提高自治性
  6. 电脑SSH登陆树莓派Raspberry的两种方式
  7. python 持续集成 教程_jenkins+python自动化测试持续集成教程
  8. linux查看网卡物理编号_Centos 网卡命名规范及信息查看(物理网卡,虚拟网卡)...
  9. win10多合一原版系统_手把手教你制作官方原版的WIN10系统安装盘
  10. Halcon OCR识别
  11. 樊昌信 通信原理第七版 第十一章 思考题答案
  12. linux中gzip与bzip2的区别,gzip与bzip2命令
  13. [转]采购订单或采购申请审批状态为IN PROCESS的解决方法
  14. 信息安全工程师自学笔记(1)
  15. iOS TestFlight Beta版本测试
  16. 物联网模块选择注意事项
  17. Android 蓝牙 搜索周围设备代码流程分析-framework到协议栈流程
  18. SXSSFWorkbook 表格内换行
  19. 【UI设计No7】单页
  20. 【H264解析Demo】10、变换量化_3_反变换

热门文章

  1. sap router更新
  2. Lingo软件在数学建模中的常见问题
  3. DBCP数据源连接池实现原理分析
  4. S32K144中CAN位时序计算/CANFD波形分析
  5. VS2017 VisualSVN过期用不了问题
  6. SSH框架之Maven
  7. 怎么解决VS编译器上用不了scanf函数(会报错)
  8. 企业如何通过EDI系统降低供应链成本
  9. android android:theme.light,android – 我在哪里可以找到Theme.AppCompat.Light的默认样式?...
  10. C语言素数因子思路,完美解决质因子(素数+因数)(C语言代码)