• Leetcode ZigZag Conversion


    Leetcode ZigZag Conversion

    做这道题遇到几个点,记录如下

    1. numRows <= 1的情况需要注意
    2. C++ string::append不可附加字符char,顾可以考虑用push_back或+=
    3. java的话最好用StringBuffer,可本地修改
    4. 这道题的思路有些像波的传播+钟摆采样
     1 class Solution {
     2 public:
     3     string convert(string s, int numRows) {
     4         string* buffer = new string[numRows];
     5         if(numRows <= 1)
     6         {
     7             return s;
     8         }
     9         int index = 0, step = 1;
    10         for(int i = 0; i < s.length(); i++)
    11         {
    12             if(index == 0)
    13             {
    14                 step = 1;
    15             }else if(index == numRows - 1){
    16                 step = -1;
    17             }
    18             buffer[index] += s[i];
    19             index += step;
    20         }
    21         //string result;    /**for saving space **/
    22         s.clear();
    23         for(int i = 0; i < numRows; i++)
    24         {
    25             s.append(buffer[i]);
    26         }
    27         delete[] buffer;
    28         return s;
    29     }
    30 };

    附上说明

    void push_back ( char c );
     

    Append character to string

    Appends a single character to the string content, increasing its size by one.

    To append more than one character at a time, refer to either member append or operator+=.

  • 相关阅读:
    实验一
    BZOJ 2564
    P4557 [JSOI2018]战争
    移动自动化-Mac-IOS-appium环境搭建
    Node安装mac版本
    删除N天前文件和空文件
    Python之jsonpath模块
    性能学习
    参数化
    查找测试用例
  • 原文地址:https://www.cnblogs.com/vin-yuan/p/5471499.html
Copyright © 2020-2023  润新知