- 浏览: 137572 次
文章分类
- 全部博客 (189)
- Tree (14)
- Dynamic Programming (34)
- Array (20)
- Search (1)
- Hash (12)
- Backtracking (22)
- Divide and Conque (8)
- Greedy (6)
- Stack (12)
- software (0)
- List (7)
- Math (22)
- Two pointers (16)
- String (20)
- Linux (1)
- Sliding Window (4)
- Finite State Machine (1)
- Breadth-first Search (7)
- Graph (4)
- DFS (6)
- BFS (3)
- Sort (9)
- 基础概念 (2)
- 沟通表达 (0)
- Heap (2)
- Binary Search (15)
- 小结 (1)
- Bit Manipulation (8)
- Union Find (4)
- Topological Sort (1)
- PriorityQueue (1)
- Design Pattern (1)
- Design (1)
- Iterator (1)
- Queue (1)
最新评论
-
likesky3:
看了数据结构书得知并不是迭代和递归的区别,yb君的写法的效果是 ...
Leetcode - Graph Valid Tree -
likesky3:
迭代和递归的区别吧~
Leetcode - Graph Valid Tree -
qb_2008:
还有一种find写法:int find(int p) { i ...
Leetcode - Graph Valid Tree -
qb_2008:
要看懂这些技巧的代码确实比较困难。我是这么看懂的:1. 明白这 ...
Leetcode - Single Num II -
qb_2008:
public int singleNumber2(int[] ...
Leetcode - Single Num II
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".
[分析] 找规律类型的题目,给出三种实现,实现1和2均是按找到的规律计算每一行各列元素在原串中的下标,实现3是按照原串的顺序构造出zigzag图,然后从上到下逐行遍历,时间和空间使用上均不如前两种好,但是更直观易懂。
[ref]
http://blog.unieagle.net/2012/11/08/leetcode%E9%A2%98%E7%9B%AE%EF%BC%9Azigzag-conversion/
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".
[分析] 找规律类型的题目,给出三种实现,实现1和2均是按找到的规律计算每一行各列元素在原串中的下标,实现3是按照原串的顺序构造出zigzag图,然后从上到下逐行遍历,时间和空间使用上均不如前两种好,但是更直观易懂。
[ref]
http://blog.unieagle.net/2012/11/08/leetcode%E9%A2%98%E7%9B%AE%EF%BC%9Azigzag-conversion/
// method 3 public String convert(String s, int numRows) { if (s == null || s.length() <= 1 || numRows == 1) return s; // build zigzag map ArrayList<ArrayList<Character>> map = new ArrayList<ArrayList<Character>>(numRows); for (int i = 0; i < numRows; i++) map.add(new ArrayList<Character>()); boolean down = true; int n = s.length(); int i = 0, row = 0; while (i < n) { if (row < numRows && down) { map.get(row++).add(s.charAt(i++)); } else if (row >= 0 && !down) { map.get(row--).add(s.charAt(i++)); } else {//ajust direction row += down ? -2 : 2; down = !down; } } // read map row by row StringBuilder ret = new StringBuilder(); for (ArrayList<Character> line : map) { for (Character c : line) { ret.append(c); } } return ret.toString(); } // method 2 public String convert2(String s, int numRows) { if (s == null || s.length() <= 1 || numRows == 1) return s; int n = s.length(); int delta = 2 * numRows - 2, diff1 = 0; StringBuilder ret = new StringBuilder(); for (int i = 0; i < numRows; i++) { int j = i; diff1 = delta - 2 * i; while (j < n) { if (diff1 > 0) ret.append(s.charAt(j)); j += diff1; diff1 = delta - diff1; } } return ret.toString(); } // method 1 public String convert1(String s, int numRows) { if (s == null || s.length() <= 1 || numRows == 1) return s; int n = s.length(); if (n < numRows) numRows = n; int delta = 2 * numRows - 2; StringBuilder ret = new StringBuilder(); for (int i = 0; i < numRows; i++) { int j = i; while (j < n) { ret.append(s.charAt(j)); int next = j + delta - 2 * i; if (i != 0 && i != numRows - 1 && next < n) { ret.append(s.charAt(next)); } j += delta; } } return ret.toString(); }
发表评论
-
Leetcode - Integer to English Words
2015-09-04 20:53 1104[分析] 这题通过率之所以非常低是因为有很多corner ca ... -
Leetcode - Read N Characters Given Read4 II - Call Multiple Times
2015-08-28 09:00 852The API: int read4(char *buf) r ... -
Leetcode - Read N Characters Given Read4
2015-08-27 20:56 691The API: int read4(char *buf) r ... -
Leetcode - One Edit Distance
2015-08-27 20:26 542[分析] 两字符串相同或者长度差异大于等于2都不符合要求,只需 ... -
Leetcode - Isomorphic Strings
2015-08-23 09:51 547[分析] 思路1:维护两个哈希表,char[] map, bo ... -
Leetcode - Group Shifted String
2015-08-22 16:20 1728Given a string, we can "sh ... -
Leetcode - Strobogrammatic Number
2015-08-22 10:48 1092A strobogrammatic number is a n ... -
Leetcode - Text Justification
2015-06-22 18:29 384Given an array of words and a l ... -
Leetcode - Valid Number
2015-06-21 10:42 658Validate if a given string is n ... -
Leetcode - Substring with Contentaion of All Words
2015-06-18 10:01 511You are given a string, s, and ... -
Leetcode - Simplify Path
2015-06-17 21:58 427Given an absolute path for a fi ... -
Leetcode - Multiply String
2015-06-15 09:39 684Given two numbers represented a ... -
Leetcode - Minimum Window String
2015-06-14 19:33 576Given a string S and a string T ... -
Leetcode - Longest Substring Without Repeating Characters
2015-06-14 15:34 563Given a string, find the length ... -
Leetcode - Implement strStr()
2015-06-13 19:54 756Implement strStr(). Returns the ... -
Leetcode - Add Binary
2015-06-13 17:34 299Given two binary strings, retu ... -
Leetcode - Compare Version Numbers
2015-06-13 16:36 460Compare two version numbers ver ... -
Leetcode - Shortest Palindrome
2015-06-13 10:55 559Given a string S, you are allow ... -
Leetcode - Longest Palindrome Substring
2015-06-11 09:48 453<div class="iteye-blog- ...
相关推荐
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) Java AC版本
c c语言_leetcode 0006_zigzag_conversion.zip
c语言入门 C语言_leetcode题解之06-zigzag-conversion.c
js js_leetcode题解之6-zigzag-conversion.js
java入门 java_leetcode题解之006_ZigZag_Conversion
- ZigZag Conversion: 将字符串从左到右依次在Z字形排列填充到指定的行数。需要找出转换后的字符串。 - String to Integer (atoi): 将字符串转换为整数,需要处理各种边界情况,例如溢出、非法输入等。 - Reverse ...
leetcode题库 LeetCode-Go 理论基础 见Note 脑图 TODO ...Conversion 37.5% Medium 0007 Reverse Integer 25.8% Easy 0008 String to Integer (atoi) 15.5% Medium 0009 Palindrome Number 49.4% Easy
Conversion.java) 7 [Java](/Java/007 反向整数.java) 8 [Java](/Java/008 String to Integer (atoi).java) 9 [Java](/Java/009 回文数.java) 10 [Java](/Java/010 正则表达式匹配.java) 11 [Java](/Java/011 最多水...
leetcode 2 代码挑战 之字形转换 字符串"PAYPALISHIRING"在给定的行数上以锯齿形图案书写,如下所示:(您可能希望以固定字体显示此图案以获得更好的可读性) P A H N A P L S I I G Y I R 然后逐行阅读: ...
leetcode实现strstr leetcode-js 记录刷leetcode分析过程,...Conversion 逻辑 2019/09/13 0007 Reverse Integer 逻辑 2019/09/13 0008 String To Integer 2019/09/14 0009 Palindrome Number 双指针 2019/09/17 0010
Conversion 45.6% 中等 7 Reverse Integer 33.2% 简单 8 String to Integer (atoi) 18.5% 中等 9 Palindrome Number 56.7% 简单 10 Regular Expression Matching 25.3% 困难 11 Container With Most Water 59.3% ...
Conversion 观察规律 7. Reverse Integer 翻转整数 8. String to Integer 解析字符串 9. Palindrome Number 回文数字 10. Regular Expression Matching 动态规划,列出转换方程即可,注意初值 记T[i][j] = 是否S[0:i...
八皇后 leetcode DSA(Data Structures and Algorithm) + Practice ...Conversion Algorithm Medium 0007 Reverse Integer Algorithm Easy 0011 Container With Most Water Algorithm Medium 0015 3Sum A
O(n)时间复杂度就可以解决###LeetCode4AddTwoNumbers简单题,注意空指针的情况和进位###LeetCode5LongestPalindromicSubstringO(N^2)的时间复杂度,不知道有没有更快的###LeetCode6ZigZagConversion边界条件一定要想...
Conversion Easy -> Medium 8 String to Integer (atoi) Easy -> Medium 19 Remove Nth Node From End of List Easy -> Medium 33 Search in Rotated Sorted Array Hard -> Medium 35 Search Insert Position Medium...
ZigZag Conversion SingleNumber 其他算法 各种SingleNumber变种 LinkListCycle I II 其他变种 编程之美 Preorder Traversal Inorder Traver sal postorder 非递归 不用栈! 字符串常用操作 字符串 各种转换 Pow(x,n...
6. **Z字形变换 (06 Zigzag Conversion)**:此题涉及到字符串处理和模拟。给定一个字符串,按照“Z”字形排列,即奇数位置的字符按顺序排列,偶数位置的字符逆序排列。这个问题可以通过迭代或递归方法实现。 这些...
leetcode 知乎 此项目介绍 此项目旨在是为leetcode里面的习题提供解析(数据结构相关知识),锻炼了自己,同时也给别人带来了方便。 网站习题链接: ** 作者知乎链接**: ...Conversion Reverse Integer Strin
leetcode ...Conversion Medium com.bcat.algorithms.medium.ZigZagConversionSol 7 Reverse Integer Easy com.bcat.algorithms.easy.ReverseIntegerSol 8 String to Integer (atoi) Medium com.bc
Conversion 7.Reverse Integer 8.String To Integer 9.Palindrome Number 10.String To Integer 11.Container With Most Water 12.Integer To Roman 13.Roman To Integer 289 347 380 442 457 Circular Array Loop ...