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. users---显示当前登录系统的所有用户的用户列表

    users命令用于显示当前登录系统的所有用户的用户列表.每个显示的用户名对应一个登录会话.如果一个用户有不止一个登录会话,那他的用户名将显示相同的次数. 语法 users(选项) 选项 --help: ...

  2. iotop---监控磁盘I/O 使用状况

    iotop命令是一个用来监视磁盘I/O使用状况的top类工具.iotop具有与top相似的UI,其中包括PID.用户.I/O.进程等相关信息.Linux下的IO统计工具如iostat,nmon等大多数 ...

  3. 【Codeforces Round #420 (Div. 2) B】Okabe and Banana Trees

    [题目链接]:http://codeforces.com/contest/821/problem/B [题意] 当(x,y)这个坐标中,x和y都为整数的时候; 这个坐标上会有x+y根香蕉; 然后给你一 ...

  4. ECNUOJ 2859 表达式的个数

    表达式的个数 Time Limit:5000MS Memory Limit:65536KBTotal Submit:47 Accepted:28 Description  世情薄,人情恶,雨送黄昏花易 ...

  5. [Python] Normalize the data with Pandas

    import os import pandas as pd import matplotlib.pyplot as plt def test_run(): start_date='2017-01-01 ...

  6. atitit.js&#160;与c#&#160;java交互html5化的原理与总结.doc

    atitit.js 与c# java交互html5化的原理与总结.doc 1. 实现html5化界面的要解决的策略 1 1.1. Js交互 1 1.2. 动态參数个数 1 1.3. 事件监听 2 2. ...

  7. centos 6.5搭建Samba

    Samba共享服务器 先将selinux关闭和防火墙端口号打开 #iptables -A INPUT -p udp -dport -j ACCEPT #iptables -A INPUT -p udp ...

  8. 关于Javascript的forEach 和 map

    本篇博客转载自 https://blog.fundebug.com/2018/02/05/map_vs_foreach/ 如果你已经有使用JavaScript的经验,你可能已经知道这两个看似相同的方法 ...

  9. P3908 异或之和

    题目描述 求1 \bigoplus 2 \bigoplus\cdots\bigoplus N1⨁2⨁⋯⨁N 的值. A \bigoplus BA⨁B 即AA , BB 按位异或. 输入输出格式 输入格 ...

  10. @Accessors

    @Accessors 作用:存取器,用于配置getter和setter方法的生成结果 三个属性:fluent.chain.prefix 1.fluent:流畅的,设置为true,getter和sett ...