【Leetcode】【Easy】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)
P A H N
A P L S I I G
Y I R
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".
解题:
引用http://blog.csdn.net/zhouworld16/article/details/14121477的图片说明:
所谓的ZigZag,就是这样的:


因此可以找到每行排列数字的规律,nRows = n,字符串字符下标从0开始。
普通青年:
① 先读第1行:0, 0 + 2n-2, 0 + (2n-2)*2,...
② 读第2行至第n-2行,设行数为e,则每行读取数字为:
e-1,e-1 + 2n-2 - (e-1)*2, e-1 + 2n-2, e-1+2n-2 + 2n-2 - (e-1)*2, ...
规律即,两个数字一组,每组间隔2n-2:
for (int i=e-1; i<string.length(); i+=2n-2) {
每组元素可表示为i 和 i + 2n-2 - (e-1)*2
}
③ 读最后一行:n-1, n-1 + 2n-2, n-1 + (2n-2)*2, ...
(由于每循环一组,会读出两个数字。注意判定字符串越界)
class Solution {
public:
string convert(string s, int nRows) {
string res;
int len = s.length();
if (nRows == )
return res;
if (nRows == )
return s;
for(int i=; i<len; i+=*nRows-)
res.push_back(s[i]);
int index = ;
while(nRows--index){
for(int i=index; i<len; i+=*nRows-) {
res.push_back(s[i]);
int zig = i + 2 * nRows - 2 * index - ;
if (zig < len)
res.push_back(s[zig]);
}
index++;
}
for(int i=nRows-; i<len; i+=*nRows-)
res.push_back(s[i]);
return res;
}
};
聪明的青年:
类似于期末考试考场排座位,一般为蛇形排号。将考场的座位按一竖列为一组,编号时,第一组的座位从前向后编号,对第二组从后向前进行编号,如下图:
① ⑧ ⑨
② ⑦ ...
③ ⑥
④ ⑤
nRows即为每组能坐多少人。因此先让同学们按编号入座,再从前向后按横排依次读出他们的编号即为本题答案。
class Solution {
public:
string convert(string s, int nRows) {
string sArray[nRows];
string res = "";
int index = ;
if (nRows == )
return s;
for(int i=; i<s.length(); ++i){
sArray[index] += s[i];
if (!(i / (nRows-) % )) {
index++;
} else {
index--;
}
}
for(int i=; i<nRows; ++i){
res += sArray[i];
}
return res;
}
};
当然是原创的。
【Leetcode】【Easy】ZigZag Conversion的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【LeetCode题意分析&解答】35. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【LeetCode算法题库】Day2:Median of Two Sorted Arrays & Longest Palindromic Substring & ZigZag Conversion
[Q4] There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of th ...
- 【leetcode刷题笔记】ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...
- 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number
[Q7] 把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...
- 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists
[Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...
- 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman
[Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...
- 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters
[Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...
随机推荐
- 【前缀和】【two-pointer】【贪心】洛谷 P3143 [USACO16OPEN]钻石收藏家Diamond Collector 题解
解法众多的一道毒瘤题? 题目描述 奶牛Bessie很喜欢闪亮亮的东西(Baling~Baling~),所以她喜欢在她的空余时间开采钻石!她现在已经收集了\(N\)颗不同大小的钻石,现在她想在谷 ...
- Mutation and Iteration
avoid mutating a list as you are iterating over it 代码: def remove_dups(L1,L2): for e in L1: if e in ...
- SGU - 507 启发式合并维护平衡树信息
题意:给定一颗树,每个叶子节点\(u\)都有权值\(val[u]\),求每个非叶子节点子树的最小叶子距离,若该子树只有一个叶子节点,输出INF 貌似本来是一道树分治(并不会)的题目,然而可以利用平衡树 ...
- vue 同一个组件的跳转, 返回时保留原来的下拉位置
1,需求分析 公司的项目有这样一个需求: 同一个list组件,根据传过来的listId渲染成多个页面,每个页面都可以下拉.在返回到不同的list页面时,要保留当时下拉的位置. 说的我自己都有点懵逼了, ...
- [转] Node.js的线程和进程
[From] http://www.admin10000.com/document/4196.html 前言 很多Node.js初学者都会有这样的疑惑,Node.js到底是单线程的还是多线程的?通过本 ...
- Oracle 11g 新特性 --SQL Plan Management 说明
Oracle 11g 新特性 --SQL Plan Management 说明 参见大神博主文章: http://blog.csdn.net/tianlesoftware/article/detail ...
- C#生成二維碼(ThoughtWorks.QRCode)
本人使用的是ThoughtWorks.QRCode.dll,在網上可以下載,但要注意dll文件的完整性和準確性,本人之前下載的dll就是不正確導致調試時出現錯誤. 以下為cs文件代碼: using S ...
- (转)shell命令:echo命令详解
shell命令:echo命令详解 原文:https://www.cnblogs.com/xyz0601/archive/2015/04/23/4450736.html 功能说明:显示文字. 语 法:e ...
- cxf 框架 webservice
cxf 内置了一个web服务器 cxf简单入门实例 package test; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import c ...
- pat1012. The Best Rank (25)
1012. The Best Rank (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To eval ...