Leetcode006 ZigZag Conversion

 1 /* simple simulation algorithm
 2  * we cann`t make sure the size of the string,
 3  * so it had better to storage in vector.
 4  * show[] to buffer the new order of the string
 5  */
 6 class Solution {
 7 public:
 8     string convert(string s, int numRows) {
 9      string result;
10      vector<char> show[numRows];
11      for(int index=0;index<s.size();)   //divide into two parts
12      {
13          for(int i=0;i<numRows;i++)     //simple row full storage
14          {
15              show[i].push_back(s[index++]);
16              if(index==s.size())break;
17          }
18          for(int i=1;i<=numRows-2;i++)  //middle row only only one char storaged
19          {
20              for(int j=numRows-1;j>=0;j--)
21              {
22                  if(i+j==numRows-1)show[j].push_back(s[index++]);
23                  if(index==s.size())break;
24              }
25              if(index==s.size())break;
26          }
27      }
28      for(int i=0;i<numRows;i++)
29      {
30          for(int j=0;j<show[i].size();j++)result+=show[i][j];
31      }
32      return result;
33     }
34 };
时间: 2025-01-01 23:18:30

Leetcode006 ZigZag Conversion的相关文章

No.006 ZigZag Conversion

6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy 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 bette

LeetCode ZigZag Conversion(将字符串排成z字型)

1 class Solution { 2 public: 3 string convert(string s, int nRows) { 4 string a=""; 5 int len=s.length(); 6 if(len<=nRows||nRows==1) return s; //只有n个,不足一个周期||排成一行 7 int teams=len/(nRows*2-2); //teams个完整的周期 8 int rest=len%(nRows*2-2); //最后一个不足

leetcode -day16 ZigZag Conversion

1.  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 b

【LeetCode】ZigZag Conversion

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

LeetCode【6】. ZigZag Conversion --思路图解与java实现

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

64. ZigZag Conversion

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

6. ZigZag Conversion

/* * 6. ZigZag Conversion * 2016-8-26 by Mingyang * 觉得这个题目面试的必要性不大,有规律,第一行和最后一行每两个相差2n-2 * 中间的每行除了2n-2有外,中间的一个也有2 * (n - 1 - i) */ public String convert(String s, int numRows) { if (numRows == 1) return s; StringBuilder sb = new StringBuilder(); // s

leetCode 6. ZigZag Conversion 字符串

6. ZigZag Conversion

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

6. ZigZag Conversion 题目:https://leetcode.com/problems/zigzag-conversion/ string convert2(string s, int numRows) { if (s.length() < 2 || numRows < 2) return s; int cycle = 2 * numRows - 2; string tmp; string result; for (int i = 0; i < numRows; i+