LeetCode(6) ZigZag Conversion
题目
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;
}
};
LeetCode(6) ZigZag Conversion的更多相关文章
- LeetCode(6)ZigZag Conversion
题目如下: C++代码: #include <iostream> #include <string> using namespace std; class Solution { ...
- (字符串)ZigZag Conversion
[解析] 第一次看到这个题目的人,可能不知道ZigZag是什么意思,简单解释一下,就是把字符串原顺序012345……按下图所示排列: 发现所有行的重复周期都是 2 * nRows - 2 对于首行和末 ...
- 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 ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- 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 ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- 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 ...
随机推荐
- Ocelot(一)- .Net Core开源网关
Ocelot - .Net Core开源网关 作者:markjiang7m2 原文地址:https://www.cnblogs.com/markjiang7m2/p/10857688.html 源码地 ...
- Go语言Flag的简单示例
flag 命令行参数解析,大家可能不太清楚是什么命令行参数解析,不要紧,我们来看看: 他就是干这个活的 func FlagTest1(){ var username string var userag ...
- Servlet,jsp,jsp的9大内置对象
以servlet作为控制器 1:servlet的生命周期:以下方法都是servlet容器进行调用 1)构造函数:只被调用一次,当项目启动时或者该servlet被容器第一次调用时,会创建servlet实 ...
- socket servlet webservice 区别及使用场景
原文地址: http://blog.csdn.net/sdjkjsdh/article/details/51569481 1. Socket:使用TCP/IP或者UDP协议在服务器与客户端之间进行传输 ...
- Minimal Ratio Tree HDU - 2489
Minimal Ratio Tree HDU - 2489 暴力枚举点,然后跑最小生成树得到这些点时的最小边权之和. 由于枚举的时候本来就是按照字典序的,不需要额外判. 错误原因:要求输出的结尾不能有 ...
- Robot Framework and Ride
Robot framework是诺西(NSN)开源的一套自动化测试工具,在通信设备自动化测试中很实用,它基于Python开发,主要模拟NMS网管配置数据到网元NODE,并读取配置看配置是否生效,ECI ...
- Rsync 12种故障排查及思路
Rsync 故障排查整理 Rsync服务常见问题汇总讲解: ====================================================================== ...
- React.js 基本环境安装
安装 React.js React.js 单独使用基本上是不可能的事情.不要指望着类似于 jQuery 下载放到 <head /> 标签就开始使用.使用 React.js 不管在开发阶段生 ...
- jsp中提示修改成功
修改成功提示 servert包 request.setAttribute("success", "修改失败"); 效果而 function f(){ var n ...
- Redis相关问题收集
问题一.强制关闭Redis快照导致不能持久化 MISCONF Redis is configured to save RDB snapshots, but is currently not able ...