题目

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)



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”.

分析

思路参考博客,在此表示对博主的感谢。

向下循环:nRows

斜角线循环:nRows-2(减去首尾两个端点)

重复

AC代码

class Solution {
public:
string convert(string s, int numRows) { if (s.empty() || numRows == 1)
return s;
//声明numRows个字符串,对该之字型序列处理
string *res = new string[numRows];
int i = 0, j, gap = numRows - 2;
while (i < s.size()){
for (j = 0; i < s.size() && j < numRows; ++j) res[j] += s[i++];
for (j = gap; i < s.size() && j > 0; --j) res[j] += s[i++];
}
string str = "";
for (i = 0; i < numRows; ++i)
str += res[i];
return str;
} };

GitHub测试程序源码

LeetCode(6) ZigZag Conversion的更多相关文章

  1. LeetCode(6)ZigZag Conversion

    题目如下: C++代码: #include <iostream> #include <string> using namespace std; class Solution { ...

  2. (字符串)ZigZag Conversion

    [解析] 第一次看到这个题目的人,可能不知道ZigZag是什么意思,简单解释一下,就是把字符串原顺序012345……按下图所示排列: 发现所有行的重复周期都是 2 * nRows - 2 对于首行和末 ...

  3. LeetCode(107) Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

  4. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  5. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  6. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  7. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  8. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  9. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

随机推荐

  1. ubuntu vim设置显示行号

    打开vim的配置文件 /etc/vim/vimrc sudo vim /etc/vim/vimrc 然后找到 #set number ,把注释取消就行了 如果没有,就自己加一行

  2. vultr 购买vps

    基本安装转自:https://github.com/uxh/shadowsocks_bash/wiki/Vultr%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B 连接 Vul ...

  3. JS 数据类型入门与typeof操作符

    标准的数据类型划分: 基本类型: number(数字).string(字符串).undefined.boolean(布尔值).null(空对象) //空对象与非空对象,最大的区别就是不能进行属性操作 ...

  4. Python之Linux下的virtualenv&&virtualenvwrapper

    virtualenv 可以在系统中建立多个不同并且相互不干扰的虚拟环境. #指定清华源下载pip的包 pip3 install -i https://pypi.tuna.tsinghua.edu.cn ...

  5. [SHOI2002]取石子游戏之三

    Wythoff's Game,详解请见浅谈算法--博弈论中的例6 /*program from Wolfycz*/ #include<cmath> #include<cstdio&g ...

  6. Windows查杀端口

    Windows环境下当某个端口被占用时,通过netstat命令进行查询pid,然后通过taskkill命令杀进程. 一.查询占用端口号的进程信息 netstat -an|findstr 二.杀掉占用端 ...

  7. break跳出嵌套循环体

    package com.wh.Object; public class Test { public static void main(String[] args) { // TODO Auto-gen ...

  8. tuple元组创建单元素

    创建tuple单元素,一定要在结尾时添加一个逗号(,)解:如果不加逗号,哪怕是使用tuple()正确的创建元组,也会有歧义,它会把创建tuple元组的单元素,当成一个普通的输出语句结果列:如下,错误的 ...

  9. 微信小程序flex布局

    一.flex布局基础 二.相对定位和绝对定位   flex的容器和元素   主轴(左-右),交叉轴(上-下)     flex容器属性详解 flex-direction 决定元素的排列方向(默认row ...

  10. Burp Suite集成sqlmap

    本地环境 JDK1.8 Burp Suite 1.7.26 Python2.7 64位安装包 sqlmap zip包 安装python及sqlmap python下载下来默认安装即可,配置系统环境变量 ...