【解析】

第一次看到这个题目的人,可能不知道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. 微信H5支付 EasyWechat

    其中如果想在一个laravel中使用多个不同主题的支付账户,可以在方法实例对象时,将对应的参数进行修改配置. 其中小程序支付,已得到验证. 1.公众号支付等资格申请 2.公众号对应的支付商户主体申请 ...

  2. laravel中好用的支付安装包

    是包括支付宝和微信的支付 准用包,在测试中 https://github.com/yansongda/laravel-pay 这个包,看上去很好但是composer require时,要求php太高, ...

  3. FPGA热设计

    任何芯片要工作,必须满足一个温度范围,这个温度是指硅片上的温度,通常称之为结温(junction temperature). ALTERA的FPGA分为商用级(commercial)和工业级(indu ...

  4. Unable to resolve target 'android-21'

    本文转载自:http://blog.csdn.net/love_javc_you/article/details/37728355 重新装完Ecplise+ATD+Android SDK 在Ecpli ...

  5. UiAutomator viewer 在tools下启动、资源jar包在platforms下(如android22)

    1.UiAutomator viewer 在tools下启动. 2.资源jar包在platforms下(如android22):android.jar/ uiautomator.jar

  6. windows抓包工具Wireshark(过滤)

    1.IP过滤 ip.src ==192.168.0.208(ip.src eq 192.168.0.208) //来源等于某个ip ip.dst ==192.168.0.208(ip.dst eq 1 ...

  7. 杂项-EMS:目录

    ylbtech-杂项-EMS:目录 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   7.返回顶部   8.返回顶部   9.返回顶部   ...

  8. ajax-简介和实现注册登录

    ajax知识点介绍: 异步Javascript和XML,用Javascript语言与服务器进行异步交互,传输的数据为XML(当然,传输的数据不只是XML),也有可能是json 优点: 1. AJAX使 ...

  9. java 调用系统外部的某个程序

    有时候我们java 调用系统外部的某个程序 可能需要调用系统外部的某个程序,此时就可以用Runtime.getRuntime().exec()来调用,他会生成一个新的进程去运行调用的程序. 此方法返回 ...

  10. 【js与jquery】javascript中url编码与解码

    本文主要针对URI编解码的相关问题做了介绍,对Url编码中哪些字符需要编码.为什么需要编码做了详细的说明,并对比分析了Javascript 中和 编解码相关的几对函数 编码/解码 escape / u ...