zoukankan      html  css  js  c++  java
  • (字符串)ZigZag Conversion

    【解析】

    第一次看到这个题目的人,可能不知道ZigZag是什么意思,简单解释一下,就是把字符串原顺序012345……按下图所示排列:

    发现所有行的重复周期都是 2 * nRows - 2

    对于首行和末行之间的行,还会额外重复一次,重复的这一次距离本周期起始字符的距离是 2 * nRows - 2 - 2 * i


      1. class Solution {  
      2. public:  
      3.     string convert(string s, int nRows) {  
      4.         // Start typing your C/C++ solution below  
      5.         // DO NOT write int main() function  
      6.         string result;  
      7.         if (nRows < 2) return s;  
      8.         for (int i = 0;i < nRows;++i) {  
      9.             for (int j = i;j < s.length();j += 2 * (nRows - 1)) {  
      10.                 result.push_back(s[j]);  
      11.                 if (i > 0 && i < nRows - 1) {  
      12.                     if (j + 2 * (nRows - i - 1) < s.length())  
      13.                         result.push_back(s[j + 2 * (nRows - i - 1)]);  
      14.                 }  
      15.             }  
      16.         }  
      17.         return result;  
      18.     }  
      19. }; 
  • 相关阅读:
    odoo11 访问MSQL Server等第三发数据源
    学习 Git Rebase
    Arch Linux 启用 MTU 探测
    可控函数
    DIY:从零开始写一个 SQL 构建器
    F# 4.6 预览版正式公布
    使用 Immutable Subject 来驱动 Angular 应用
    如何对付运行时可能为 null 的 Record Type
    Angular Forms
    不要使用 JWT 进行会话管理
  • 原文地址:https://www.cnblogs.com/Kobe10/p/6364220.html
Copyright © 2011-2022 走看看