leetcode 6 ZigZag Conversion(水题)】的更多相关文章

就是简单的模拟一下就可以了.但是我一开始是用一个二维char数组来存的,这样在最终扫全体时会扫很多空的位置,浪费了很多时间,所以就time limit error了. 所以改进一下就用string数组来存.其实可以发现,这个表我们需要的每一行的相对位置,而对于每一列的相对位置根本就无所谓,可以横向随便“拉伸”这个表. class Solution { public: string convert(string s, int numRows) { const int n=numRows; )ret…
https://leetcode.com/problems/zigzag-conversion/ 水题纯考细心 题目:依照Z字形来把一个字符串写成矩阵,然后逐行输出矩阵. O(n)能够处理掉 记i为行数 第0行和第numRow-1行. ans += str[i+k*(numRows*2-2)], k=0,1,2,... 其它, 每一个Z字形(事实上仅仅是一竖一斜两条线)须要加上两个,下标见代码. 特殊处理:numRows=1的时候numRows*2-2==0 ,会死循环的.另外numRows=1…
6. ZigZag Conversion Medium 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 li…
ZigZag Conversion 看了三遍题目才懂,都有点怀疑自己是不是够聪明... 就是排成这个样子啦,然后从左往右逐行读取返回. 这题看起来很简单,做起来,应该也很简单. 通过位置计算行数: P I N A L S I G Y A H R P I 0,0 1,1 2,2 3,3 4,2 5,1 6,0 (期望) 用简单的先思考: P A H N A P L S I I G Y I R 0,0 1,1 2,2 3,3 4,0 %4 (3+1)可以解决正常的 3,1(期望) 特殊的3(2行2排…
题目 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: "PAHNAPL…
https://leetcode.com/problems/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…
题目来源:https://leetcode.com/problems/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…
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/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…
前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 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 b…
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: "PAHNAPLSII…
题目简述 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: "PAHNA…
https://leetcode.com/problems/zigzag-conversion/ 题目: 将字符串转化成zigzag模式. 例如 "abcdefghijkmlnpq" 当为4行,zigzag模式为: a    g    l b f h m n c e i k   p d   j      q 输出为aglbfhmnceikpdjq 当为5行,zigzag模式为: a    i b h j q c g k p d f l  n e   m 输出为aibhjqcgkpdfl…
Question: 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: "…
问题描述: 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: "PAHN…
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: "PAHNAPLSII…
题目: 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: "PAHNAP…
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: "PAHNAPLSII…
一.题目链接:https://leetcode.com/problems/zigzag-conversion/description/ 二.题目大意: 给定一个字符串和一个数字,将其转换成Zigzag形式后,逐行输出该字符串.所谓的Zigzag形式,具体如下: /*n=numRows Δ=2n-2 1 2n-1 4n-3 Δ= 2 2n-2 2n 4n-4 4n-2 Δ= 3 2n-3 2n+1 4n-5 . Δ= . . . . . Δ= . n+2 . 3n . Δ= n-1 n+1 3n-…
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: "PAHNAPLSII…
problem: 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: "P…
原题链接 把字符串按照 ↓↗↓……的顺序,排列成一个 Z 形,返回 从左到右,按行读得的字符串. 思路: 建立一个二维数组来按行保存字符串. 按照 ↓↗↓……的方向进行对每一行加入字符. 太慢了这个解法,Runtime: 96 ms, faster than 3.61% of C++. class Solution { public: string convert(string s, int numRows) { ) return s; string res; string zigZag[num…
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: "PAHNAPLSII…
题目: 把一个字符串按照Z型排列后打印出来,例如 "PAYPALISHIRING" 重新排列后为3行,即 P A H N A P L S I I G Y I R 那么输出为"PAHNAPLSIIGYIR" 解法: 细节实现题,假如重新排列为5行,那么排列后的下标分布应该为 0  8   16 24 1 7 9   15   17 23 2 6 10 14 18 22 3 5 11 13 19 21 4  12 20 可以看出规律,输出为5行的时候,每两个完整列之间的…
leetcode 34 最早出现和最后出现 class Solution { public int[] searchRange(int[] nums, int target) { int []ans={-1,-1} ; for(int i=0;i<nums.length;i++){ if(nums[i]==target){ ans[0]=i; break; } } for(int j=nums.length-1;j>=0;j--){ if(nums[j]==target){ ans[1]=j;…
这道题是LeetCode里的第6道题. 题目要求: 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: L C I R E T O E S I I G E D H N 之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"LCIRETOESIIGEDHN". 请你实现这个将字符串进行指定行数变换的函数: string convert(string s…
Leetcode 44 实现一种类似正则表达式的字符串匹配功能. 复杂度要求不高, 调代码稍微费点劲.. 好像跟贪心也不太沾边, 总之 *把待匹配串分成若干个子串, 每一个子串尽量在模式串中靠前的部分匹配完成就算贪心了吧.. class Solution { public: bool match(string &s,string &p,int l2,int r2,int l1) { if(l2==r2)return true; if(l1+r2-l2-1>=s.length())re…
题意:将字符串排成Z字形. PAHNAPLSIIGYIR 如果是5的话,是这样排的 P     I AP   YR H L G N  SI A    I 于是,少年少女们,自己去找规律吧 提示:每个Z字形的字符串和原字符串的每个字母的位子一一映射 class Solution { public: string convert(string s, int numRows) { string t = s; ) return t; ; ; i < s.size(); i += * numRows -…
题意:给你一个字符串和行数numRows,要求把该字符串变成一个"之"字形状后,按行数输出该字符串. 例子:"ABCDEFGHIJKLMNO", 4. 该字符串的"之"字形状为: A G M B F H L N C E I K O D J 那么,输出为AGMBFHLNCEIKODJ (按行数输出) 思路:数学推导公式.在字符串中,有两行是特殊的,那就是第一行和最后一行.   第一行和最后一行,字符都是在一竖上,且字符之间相距的距离都是diff =…
自己写的: if(numRows == 1) return s; int ll = s.length() / 2 + 1; Character tc[] = new Character[numRows* ll]; int i = 0, j = 0; boolean down = true; for(int k = 0; k < s.length(); k++){ tc[i* ll + j] = s.charAt(k); if(down){ i++; if(i == numRows){ i -=…
public class Solution { public String convert(String s, int nRows) { if (s == null || s.isEmpty() || s.length() <= nRows || nRows == 1) { return s; } int length = s.length(); StringBuilder sb = new StringBuilder(); int step = 2 * (nRows - 1); int cou…