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 ...
随机推荐
- 一条SQL语句是如何执行的?--Mysql45讲笔记记录 打卡day1
写在前面的话:回想以前上班的时候,空闲时间还是挺多的,但是都荒废了.如今找工作着实费劲了.但是这段时间在极客时间买了mysql45讲,就好像发现了新大陆一样,这是我认真做笔记的第一天,说实话第一讲我已 ...
- NOIp 2014 联合权值 By cellur925
题目传送门 这题自己(真正)思考了很久(欣慰). (轻而易举)地发现这是一棵树后,打算从Dfs序中下功夫,推敲了很久规律,没看出来(太弱了). 开始手动枚举距离为2的情况,模模糊糊有了一些概念,但没有 ...
- ssh 公钥登录远程主机
ssh-keygen 然后一路回车就可以了 ssh-copy-id user@host user代表用户名,host代表主机地址 然后根据提示输入远程主机的密码,成功,再登录就不用输入密码了
- vue 相关技术文章集锦
不断更新,如果看到好的文章~~~ 总结篇 vue组件间通信六种方式(完整版) - 原作者:简书-浪里行舟 原理/源码篇 Vue.js 技术揭秘 Vue技术内幕 实战/经验篇 Vue相关开源项目库汇总 ...
- c语言程序设计案例教程(第2版)笔记(二)—函数、递归
零散知识点 模块化:将一个问题分解成若干个子问题的过程成为模块化. 模块化的优点:不但可以将一个复杂的问题分解成几个相对简单的问题:还可以提高程序代码的重用性. 函数:函数是构成C程序的基本单位.函数 ...
- 实验 - cut的应用
题目一: 1.1 创建一个通讯录 vi phone.txt #进行编辑 cat phone.txt #查看内容 2.1 取出手机号码 cut -f phone.txt 3.1 取出手机前三位 cut ...
- How many Fibs? POJ - 2413
How many Fibs? POJ - 2413 高精模板 #include<cstdio> #include<cstring> #include<algorithm& ...
- 字符串处理 Codeforces Round #285 (Div. 2) B. Misha and Changing Handles
题目传送门 /* 题意:给出一系列名字变化,问最后初始的名字变成了什么 字符串处理:每一次输入到之前的找相印的名字,若没有,则是初始的,pos[m] 数组记录初始位置 在每一次更新时都把初始pos加上 ...
- magento 添加事件
首先是配置文件config.xml里的配置 <checkout_cart_save_after> /*事件名字*/ <observers> <deal> /*模块名 ...
- 针对谷歌默认最小字体12px的正确解决方案
利用css3的缩放,其最终大小就是:12px * 0.9(缩放比例) = 10.8px; 居然行得通.但回头一想,这么写的话,IE7 IE8会不会不兼容,还是12px呢?不出所料,果然不兼容.此时,又 ...