https://leetcode.com/problems/zigzag-conversion/

水题纯考细心

题目:依照Z字形来把一个字符串写成矩阵,然后逐行输出矩阵。

O(n)能够处理掉

记i为行数

第0行和第numRow-1行。 ans += str[i+k*(numRows*2-2)], k=0,1,2,...

其它, 每一个Z字形(事实上仅仅是一竖一斜两条线)须要加上两个,下标见代码。

特殊处理:numRows=1的时候numRows*2-2==0 ,会死循环的。另外numRows=1的时候直接输出即可

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std; class Solution {
public:
    string convert(string s, int numRows) {
        string ret;
        if(numRows == 1){
            return s;
        }
        for(int i=0; i<numRows; i++){
            if(i == 0 || i == numRows-1){
                int j=i;
                while(j<s.size()){
                    ret = ret + s[j];
                    j += numRows*2-2;
                }
            }else{
                int j=i;
                while(j<s.size()){
                    ret = ret + s[j];
                    if(j+numRows*2-2-(i*2) < s.size())ret = ret + s[j+numRows*2-2-(i*2)];
                    j += numRows*2-2;
                }
            }         }
        return ret;
    }
}; int main(){
string s;
int numRows;
Solution sol;
while(cin >> s >> numRows){
cout << sol.convert(s, numRows) << endl;
}
return 0;
}

LeetCode 06 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 [Difficulty: Medium]

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

  7. [LeetCode 题解]: ZigZag Conversion

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

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

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

  9. 【leetcode】ZigZag Conversion

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

随机推荐

  1. 【Fiddler】使用fiddler抓取指定浏览器的包

    参考资料:http://blog.csdn.net/sufubo/article/details/49331705 使用fiddler抓取不到浏览器的包时常用的解决办法: 1.必须先打开Fiddler ...

  2. Git 服务器更换了IP的解决方法

    1.找到项目根目录中的.git文件夹 2..git文件夹里有一个config文件 3.用记事本打开后,修改为服务器的新ip就行了.

  3. codevs——T2806 红与黑

    http://codevs.cn/problem/2806/  时间限制: 1 s  空间限制: 64000 KB  题目等级 : 白银 Silver 题解       题目描述 Descriptio ...

  4. COGS——T2084. Asm.Def的基本算法

    http://cogs.pro/cogs/problem/problem.php?pid=2084 ★☆   输入文件:asm_algo.in   输出文件:asm_algo.out   简单对比时间 ...

  5. JAVA爬虫Nutch、WebCollector的正则约束

    爬虫爬取时,须要约束爬取的范围. 基本全部的爬虫都是通过正則表達式来完毕这个约束. 最简单的,正则: http://www.xinhuanet.com/.* 代表"http://www.xi ...

  6. Id选择器和Class选择器

    http://www.runoob.com/css/css-id-class.html http://www.w3school.com.cn/css/css_syntax_id_selector.as ...

  7. 关于jacob支持BSTR类型的经验总结

    作者:朱金灿 来源:http://blog.csdn.net/clever101 jacob是实现Java和COM之间互操作的一个开源中间件.网上大多的程序示例基本上是使用jacob调用微软的offi ...

  8. 轻松掌握Ubuntu Linux的3D桌面快捷键使用

    视频下载地址: http://115.com/file/be4n23v6#linux3d.rar 轻松掌握Ubuntu Linux的3D桌面快捷键使用 高级3D桌面展示 本文出自 "李晨光原 ...

  9. js创建dom操作select

    document.getElementById("column-left").getElementsByTagName("header")[0].onclick ...

  10. ES6学习笔记(六)数组的扩展

    1.扩展运算符 1.1含义 扩展运算符(spread)是三个点(...).它好比 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列. console.log(...[1, 2, 3]) // ...