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. 装了ubuntu后笔记本电脑的无线网卡用不了,怎么设置?

    百度经验的一篇文章 http://jingyan.baidu.com/article/ca2d939dd4f1b4eb6c31ce09.html 点击右上角的齿轮,选择“系统设置”   点击“软件和更 ...

  2. JavaScript 回车键绑定登录 事件 常用键位码(keyCode)

    1.回车键绑定登录事件 $(document).keydown(function (e) { if ((e.keyCode || e.which) == 13) { //document.queryS ...

  3. 洛谷—— P1003 铺地毯

    https://www.luogu.org/problem/show?pid=1003 题目描述 为了准备一个独特的颁奖典礼,组织者在会场的一片矩形区域(可看做是平面直角坐标系的第一象限)铺上一些矩形 ...

  4. 七 HBase表结构设计

      表结构设计之  高表  与 宽表 选择       HBase 中的表可以设计为高表(tall-narrow table) 和 宽表(flat-wide table).         高表 : ...

  5. postgresql拓展if、ifnull、group_concat函数

    postgresql版本是8.2.15. 最近陆续有数据分析师从impala.hive转到查询gpdb,gpdb虽然能够支持在查询语句中带多个distinct,但是缺少相应的if.ifnull.gro ...

  6. UITextView自己定义键盘和系统键盘

    UITextView有inputView 和 inputAccessoryView 两个属性,都指定了对应的视图. inputAccessoryView 对象显示在 inputView 对象的上面.与 ...

  7. awk条件语句

    条件语句用于在运行操作之前做一个測试.在前面的章节中,我们看到了模式匹配规则的一些演示样例. 模式匹配规则本质上就是影响输入循环的条件表达式. 在这一部分,我们主要就在action中所使用的条件语句进 ...

  8. berkeley db储存URL队列的简单实现增、删、查

     Berkeley DB(BDB)是一个高效的嵌入式数据库编程库,C语言.C++.Java.Perl.Python.Tcl以及其它非常多语言都有其相应的API. Berkeley DB能够保存随意 ...

  9. java设计模式学习 ----- 单例模式(Singleton)

    单例模式(Singleton) 单例对象(Singleton)是一种经常使用的设计模式. 在Java应用中,单例对象能保证在一个JVM中,该对象仅仅有一个实例存在.单例模式也分三种:懒汉式单例.饿汉式 ...

  10. Servlet深入学习,规范,理解和实现(中)——深入理解Tomcat(一)

    心得:在写这篇博客之前.我大致阅读一些关于Tomcat的书籍和博客等资料.有些资料由于时间的关系,解说的Tomcat版本号太老.有些资料能够非常好的说明Tomcat整理结构和设计思想可是非常多重要的问 ...