【解析】

第一次看到这个题目的人,可能不知道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. };

(字符串)ZigZag Conversion的更多相关文章

  1. 字符串按照Z旋转90度然后上下翻转的字形按行输出字符串--ZigZag Conversion

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

  2. LeetCode 6. ZigZag Conversion & 字符串

    ZigZag Conversion 看了三遍题目才懂,都有点怀疑自己是不是够聪明... 就是排成这个样子啦,然后从左往右逐行读取返回. 这题看起来很简单,做起来,应该也很简单. 通过位置计算行数: P ...

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

    class Solution { public: string convert(string s, int nRows) { string a=""; int len=s.leng ...

  4. No.006 ZigZag Conversion

    6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...

  5. leetcode第六题 ZigZag Conversion (java)

    ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag pattern on a given nu ...

  6. leetcode题解 6.ZigZag Conversion

    6.ZigZag Conversion 题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a gi ...

  7. LeetCode--No.006 ZigZag Conversion

    6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...

  8. leetcode-algorithms-6 ZigZag Conversion

    leetcode-algorithms-6 ZigZag Conversion The string "PAYPALISHIRING" is written in a zigzag ...

  9. 《LeetBook》leetcode题解(6): ZigZag Conversion[E]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  10. LeetCode解题报告—— 2 Keys Keyboard & Longest Palindromic Substring & ZigZag Conversion

    1. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...

随机推荐

  1. 异常:org.springframework.http.converter.HttpMessageNotReadableException

    spring(springboot.springmvc)出现标题的异常一般是由于controller的入参失败引起的. 介绍下常规入参的两种格式: ,这种方式的入参主要是接受key-value的参数, ...

  2. Linux: How to delete a disk or LUN reference from /dev

    In AIX, there is rmdev command to remove a disk/LUN from /dev directory i.e to make the disk/LUN una ...

  3. unittest之跳过用例(skip) (含如何调用类里面函数相互调取变量的方法)

    当测试用例写完后,有些模块有改动时候,会影响到部分用例的执行,这个时候我们希望暂时跳过这些用例. 或者前面某个功能运行失败了,后面的几个用例是依赖于这个功能的用例,如果第一步就失败了,后面的用例也就没 ...

  4. 第五章 深入class文件结构

    一次编译好的class文件是如何到处运行的 5.1 JVM指令集简介 5.1.1 与类相关的指令 5.1.2 方法的定义 5.1.3 属性的定义 5.1.4 其他指令集 5.2 class文件头的表示 ...

  5. Python Twisted系列教程16:Twisted 进程守护

    作者:dave@http://krondo.com/twisted-daemonologie/  译者: Cheng Luo 你可以从”第一部分 Twist理论基础“开始阅读:也可以从”Twisted ...

  6. UML中关系的分类及其概念——总结备忘

    UML中关系分类: 依赖:依赖是两个事物间的语义关系,其中一个事物(独立事物)发生变化会影响另一个事物(依赖事物)的语义. 关联:关联是类与类之间的联接,它使一个类知道另一类的属性和方法. 聚合:聚合 ...

  7. python 通过ftplib 实现上传下载

    #!/usr/bin/python #-*- coding: utf-8 -*- from ftplib import FTP def ftpconnect() ftp_server = 'ftp.p ...

  8. MIS系统部署方案

  9. Elasticsearch集群如何扩容机器?

    前提, Elasticsearch-2.4.3的3节点安装(多种方式图文详解)   比如,你已经成功搭建了3台机器的es集群,如我这里分别是192.168.80.10.192.168.80.11.19 ...

  10. uboot重定位代码分析(转)

    概述 重定位(relocate)代码将BootLoader自身由Flash复制到SDRAM,以便跳转到SDRAM执行.之所以需要进行重定位是因为在Flash中执行速度比较慢,而系统复位后总是从0x00 ...