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. 免费录屏软件之OBS Studio

    好久没有再博客活动啦,今天给大家推荐一下录屏软件吧!首先我个人最喜欢的OBS Studio就说说它吧 1.免费.开源.功能强大.易上手 ​ ​ ​ 下面是下载地址: 官网下载 : https://ob ...

  2. wall---向系统当前所有打开的终端上输出信息

    wall命令用于向系统当前所有打开的终端上输出信息.通过wall命令可将信息发送给每位同意接收公众信息的终端机用户,若不给予其信息内容,则wall命令会从标准输入设备读取数据,然后再把所得到的数据传送 ...

  3. 从终端运行python程序

    终端窗口运行.py程序 首先你要安装python,命令行输入 python 有python提示符 >>> 出现说明安装成功 程序第一行应该是 #! python3 #! python ...

  4. Java中发送http的get、post请求

    近期做项目中,须要把消息通过中间件的形式通过http请求的方式推送给第三方,因此用到了http协议,小编花费了一个多小时.对于http协议中的post和get请求,封装了一个工具类.以下与大家分享一下 ...

  5. abap选择屏幕上的button

    1.背景:近期在看sap的一些abapDemo,看了一个比較好用的功能.分享一下.希望对用到的兄弟有帮助,主要功能是:在选择屏幕上弹出一个小窗体.放一些button在上面,触发不同button,会处理 ...

  6. uva 104 Arbitrage (DP + floyd)

    uva 104 Arbitrage Description Download as PDF Background The use of computers in the finance industr ...

  7. Oracle 复制随意表一行的SQL语句(測试Ok)

    測试了非常久,网上说的方法非常多,事实上都是错误的.正确的写法: declare cursor rowAll is select * from tb_news where 1=1; row1 tb_n ...

  8. libsvm 的使用

    1. libsvm 支持的SVM模型 官网地址:LIBSVM – A Library for Support Vector Machines libsvm 支持的 SVM 模型如下(C:classif ...

  9. Gym - 100203A Ariel 暴力+位运算

    题意:第i种生物有k[i]个特征,分数是score[i],现在要参加竞赛,报出一种生物a,和一些特征h[i],参加竞赛的所有生物在这些h[i]上面的特征是一样的,a生物有h[i],则所有竞赛的生物都必 ...

  10. 带你玩转Visual Studio——性能分析与优化

    找到性能瓶颈 二八法则适合很多事物:最重要的只占其中一小部分,约20%,其余80%的尽管是多数,却是次要的.在程序代码中也是一样,决定应用性能的就那20%的代码(甚至更少).因此优化实践中,我们将精力 ...