题目

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. Docker+Jenkins+Git发布SpringBoot应用

    Doccker Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之 ...

  2. ios开发-常见的项目文件介绍

    一.项目文件结构示意图 二.文件介绍 1.products文件夹:主要用于mac电脑开发的可执行文件,ios开发用不到这个文件 2.frameworks文件夹主要用来放依赖的框架 3.test文件夹是 ...

  3. javascript---DOM大编程2

    编程挑战 现在利用之前我们学过的JavaScript知识,实现选项卡切换的效果. 效果图: 文字素材: 房产: 275万购昌平邻铁三居 总价20万买一居    200万内购五环三居 140万安家东三环 ...

  4. [UOJ311]积劳成疾

    题解 dp 似乎这个最大值不好设计状态啊== 但是可以发现这\(n\)个点每个点都是相同的 可以设计状态\(f_{i,j}\)表示一个长度为\(i\)的一段区间的最大值不会超过\(j\)的价值 那么转 ...

  5. Android偏好设置(3)启动偏好设置后显示的界面PreferenceActivity和PreferenceFragment

    Creating a Preference Activity To display your settings in an activity, extend the PreferenceActivit ...

  6. 转 Docker Swarm vs Kubernetes

    容器化已经改变我们部署软件和微服务开发的方式.如果你刚听说容器, 这篇博客帮你入门. 什么是容器编排 容器能够把服务打包成基本单元,你可以把它部署到任何地方:本地机器.测试环境或者生产系统.但是在生产 ...

  7. 224 Basic Calculator 基本计算器

    实现一个基本的计算器来计算一个简单的字符串表达式. 字符串表达式可以包含左括号 ( ,右括号),加号+ ,减号 -,非负整数和空格 . 假定所给的表达式语句总是正确有效的. 例如: "1 + ...

  8. sdut1642Simple Arithmetics(模拟)

    链接 发个长长的模拟 这题要注意的地方挺多 -的个数 以及对齐的情况 全都注意好了 大数的加减乘就可以了 #include <iostream> #include<cstdio> ...

  9. [转]在ASP.NET MVC3中使用EFCodeFirst 1.0

    本文转自:http://kb.cnblogs.com/page/97003/ 作者: NinoFocus  来源: 博客园  发布时间: 2011-04-12 10:41  阅读: 11971 次   ...

  10. AJPFX总结IO流中的缓冲思想

    缓冲思想   (因为内存的运算速度要远大于硬盘的原酸速度,所以只要降低硬盘的读写次数,就可以提高效率)    1. 字节流一次读写一个数组的速度明显比一次读写一个字节的速度快很多,    2. 这是加 ...