一天一道LeetCode系列

(一)题目

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:

“PAHNAPLSIIGYIR” Write the code that will take a string and make this

conversion given a number of rows:

string convert(string text, int nRows); convert(“PAYPALISHIRING”, 3)

should return “PAHNAPLSIIGYIR”.

题意:

(二)解题

已知输入条件:初始string s和numRows。可以得出循环间隔gap = 2*numRows-2。

统计规律:

- 第0行 从j=0开始,间距gap,取s[j]

- 第1~numRows-2行 从j=1开始,间距gap=(j%gap)<numRows?(gap-2(j%gap)):(2*gap-2(j%gap)),取s[j]

- 第numRows-1行,间距gap,取s[j]

代码如下:

class Solution {
public:
    string convert(string s, int numRows) {
        string result;
        int gap=2*numRows-2; //初始化gap
        if(numRows == 1) return s;//rows为1,直接返回
        for(int i = 0 ; i < numRows ; i++)
        {
            int j = i ;
            bool flag = true;
            if(i == 0 || i == numRows-1)//如果第0行或最后一行
            {
                int j = i;
                while(j<s.length())
                {
                    result+=s[j];
                    j += gap;//间距为gap
                }
            }
            else{
                int j = i;
                while(j<s.length()){
                    result+=s[j];
                    j += (j%gap)<numRows?(gap-2*(j%gap)):(2*gap-2*(j%gap));
                }
            }
        }
        return result;
    }
};

【一天一道LeetCode】#6 ZigZag Conversion的更多相关文章

  1. LeetCode 6. ZigZag Conversion & 字符串

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

  2. Leetcode 6. ZigZag Conversion(找规律,水题)

    6. ZigZag Conversion Medium The string "PAYPALISHIRING" is written in a zigzag pattern on ...

  3. LeetCode 6 ZigZag Conversion 模拟 难度:0

    https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is written in ...

  4. LeetCode 6 ZigZag Conversion(规律)

    题目来源:https://leetcode.com/problems/zigzag-conversion/ The string "PAYPALISHIRING" is writt ...

  5. [LeetCode][Python]ZigZag Conversion

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/zigzag- ...

  6. LeetCode——6. ZigZag Conversion

    一.题目链接:https://leetcode.com/problems/zigzag-conversion/description/ 二.题目大意: 给定一个字符串和一个数字,将其转换成Zigzag ...

  7. 蜗牛慢慢爬 LeetCode 6. ZigZag Conversion [Difficulty: Medium]

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

  8. [LeetCode 题解]: ZigZag Conversion

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 The string ...

  9. [LeetCode] 6. ZigZag Conversion 之字型转换字符串

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

  10. 【leetcode】ZigZag Conversion

    题目简述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows ...

随机推荐

  1. Android新特性Instant Run详解

    关于 Instant Run Android Studio 2.0 中引入的 Instant Run 是 Run 和 Debug 命令的行为,可以大幅缩短应用更新的时间.尽管首次构建可能需要花费较长的 ...

  2. Android TV开发总结(六)构建一个TV app的直播节目实例

    请尊重分享成果,转载请注明出处:http://blog.csdn.net/hejjunlin/article/details/52966319 近年来,Android TV的迅速发展,传统的有线电视受 ...

  3. 重新安装nginx注意事项

    记得清理/etc/nginx/sites-enabled/default

  4. -eq、-ne、-gt、-ge、-lt、-le英文意思

    在shell脚本中,使用-eq.-ne.-gt.-ge.-lt.-le进行整数的比较.英文意思分别为: -eq :equal(相等) -ne :not equal(不等) -gt  :greater ...

  5. Android ColorMatrix类图像颜色处理-黑白老照片、泛黄旧照片、高对比度等效果

    在Android中,对图像进行颜色方面的处理,如黑白老照片.泛黄旧照片.高对比度.低饱和度等效果,都可以通过使用颜色矩阵(ColorMatrix)来实现. 1.颜色矩阵(ColorMatrix)介绍 ...

  6. iOS中 Realm的学习与使用 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博! 有问题或技术交流可以咨询!欢迎加入! 这篇直接搬了一份官方文档过来看的 由于之前没用markdown搞的乱七八糟的 ...

  7. C++中struct类型增强

    struct类型的加强: C语言的struct定义了一组变量的集合,C编译器并不认为这是一种新的类型. C++中的struct是一个新类型的定义声明. demo struct Student { ch ...

  8. 【Linux驱动】字符设备驱动

    一.linux系统将设备分为3类:字符设备.块设备.网络设备.使用驱动程序: 1.字符设备:是指只能一个字节一个字节读写的设备,不能随机读取设备内存中的某一数据,读取数据需要按照先后数据.字符设备是面 ...

  9. 【一天一道LeetCode】#136. Single Number

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  10. UNIX网络编程——套接字选项(SO_RCVBUF和SO_SNDBUF)

    有时候我们需要控制套接字的行为(如修改缓冲区的大小),这个时候我们就要学习套接字选项. int getsockopt(int sockfd,int level,int optname,void *op ...