String
文章平均质量分 68
Dylan_Java_NYC
练很重要,总结更重要,感谢CSDN给了我这么好的平台交流。
展开
-
LeetCode Minimum Window Substring
原题链接在这里: https://leetcode.com/problems/minimum-window-substring/又是一道双指针问题,与Longest Substring Without Repeating Characters, Substring with Concatenation of All Words相似。维护一个字典和一个窗口,不同的地方是题目说contai原创 2015-08-13 04:51:50 · 327 阅读 · 0 评论 -
LeetCode Implement strStr()
原题链接在这里:https://leetcode.com/problems/implement-strstr/原来Java里有这么个API, str.indexOf(subString). 这是API的用法:http://www.tutorialspoint.com/java/java_string_indexof.htmAC Java:public class Solution {原创 2015-09-20 05:19:40 · 503 阅读 · 0 评论 -
LeetCode Length of Last Word
原题链接在这里:https://leetcode.com/problems/length-of-last-word/从最后一个字符开始数,最后一个字符是 ' ', 就一直向前移动指针直到出现第一个字符。开始计数,然后继续扫面直到出现下一个 ' ', 返回计数结果即可。Note: 1. while loop加 i--, 这里已经犯过很多次类似的错误了。2. 在使用s.charA原创 2015-09-19 23:51:54 · 413 阅读 · 0 评论 -
LeetCode Count and Say
原题链接在这里:https://leetcode.com/problems/count-and-say/当前的读音用来做数列的下一个"1" 读成1个1,写成"11"用来作为下一个。从i = 2开始一直到n作为外层loop, i是第 ith 个结果。进入循环后,新生成count = 1, index = 1, 然后进入内层loop, 用index 从第二位走res, 现字符等同于前字符就原创 2015-09-18 11:32:48 · 406 阅读 · 0 评论 -
LeetCode Compare Version Numbers
原题链接在这里:https://leetcode.com/problems/compare-version-numbers/用string.split()方法把原有string 拆成 string 数组,但这里要注意 . 和 * 是不能直接用split(".") 或者split("*")拆开的,因为 . 可以代表任意char, * 可以代表任意字符串。所以要加 \\. 来避免individua原创 2015-09-18 10:01:56 · 378 阅读 · 0 评论 -
LeetCode Interleaving String
原题链接在这里:https://leetcode.com/problems/interleaving-string/这是一道DP题,用boolean数组dp维护历史数据,dp[i][j]表示长度为i的s1和长度为j的s2能否组成长度为 i+j 的s3.递推式是若当前s3的最后一个char与s1的最后一个char相同,并且dp[i-1][j]为true, 那么dp[i][j]为true;原创 2015-09-18 05:46:12 · 366 阅读 · 0 评论 -
LeetCode Longest Common Prefix
原题链接在这里:https://leetcode.com/problems/longest-common-prefix/strs中的第一个字符串从i = 0 开始,取char, 取出char后,用来比较其他所有字符串,若是此时index i 已经等于其他字符串长度或者其他某个字符创相应位置上的char不同,说明已经走不下去了,就地返回res. 若其他字符串的响应位置都有char, 且相同,原创 2015-09-18 10:28:45 · 356 阅读 · 0 评论 -
LeetCode Distinct Subsequences
原题链接在这里:https://leetcode.com/problems/distinct-subsequences/这是一道DP. 简历二维数组dp, dp[i,j]表示s[0....i]有多少种方法可以减成t[0.......j].更新方法是: 若s.charAt(i-1) == t.charAt(j-1), dp[i][j] = dp[i-1][j-1](用上最新的match, 之原创 2015-09-18 04:31:50 · 436 阅读 · 0 评论 -
LeetCode Longest Valid Parentheses
原题链接在这里:https://leetcode.com/problems/longest-valid-parentheses/和Largest Rectangle in Histogram都是往栈内存index. 生成一个stack, 从头往后扫string, 遇到'(', 就把当前index存入到stack中。当遇到 ')'时要看当前stack是后为空,若为空,更新新起点start原创 2015-09-17 23:24:06 · 371 阅读 · 0 评论 -
LeetCode Decode Ways
原题链接在这里:https://leetcode.com/problems/decode-ways/DP题,保存的历史信息是到历史点有多少种decode的方法,用长度为len+1的数组保存,更新方式是若当前位i 和前一位 i-1两个组合的数字不合法则decode方法并没有增加,仍是dp[i-1]; 若合法,则应更改为dp[i-2]+dp[i-1]. 跟走楼梯相似。初始化为了方便把dp[0]原创 2015-09-15 07:37:57 · 375 阅读 · 0 评论 -
LeetCode Basic Calculator II
原题链接在这里:https://leetcode.com/problems/basic-calculator-ii/与Basic Calculator类似。思路: 扫一遍string,遇见数字时看栈顶是不是 '*' 或者 '/',若不是,就压栈,若是就取出栈顶两个元素算结果,再押回栈。遇见符号就压栈。基本就是先算了 乘除运算。剩下栈里的就是加减运算了。这里注意需用到翻转栈, 使原创 2015-08-21 12:21:54 · 270 阅读 · 0 评论 -
LeetCode Longest Substring Without Repeating Characters
原题链接在这里:https://leetcode.com/problems/longest-substring-without-repeating-characters/String 题目,基本思路就是维护一个窗口[walker,runner]. walker,runner都是向右移,同时维护一个HashSet hs,用来存储这一段中的所有不同character。首先右侧窗口runner在前跑原创 2015-08-08 10:30:16 · 372 阅读 · 0 评论 -
LeetCode Valid Parentheses
原题链接在这里:https://leetcode.com/problems/valid-parentheses/当遇到'(', '[', '{' 时压栈,当遇到')', ']', '}'时观察stk是否为空,若是空,返回false,若不是,pop()出来的第一个元素是否对应,若不对应, 返回false. 读完整个string若stk不空,返回false。若没问题,返回true。AC原创 2015-08-18 12:02:08 · 254 阅读 · 0 评论 -
LeetCode Multiply Strings
原题链接在这里:https://leetcode.com/problems/multiply-strings/Method 1: 通过这道题,学到了BigInteger的用法, 他的constructor 可以直接从string建立BigInteger, 但要注意它的乘法API是bi1.multiply(bi2).若是中间需要考虑溢出,还有bi1.intValue(), bi1.lon原创 2015-08-18 03:32:59 · 236 阅读 · 0 评论 -
LeetCode Add Binary
原题链接在这里: https://leetcode.com/problems/add-binary/这道题与Plus One相似。都是维护一个currentDigit(%) 和一个carryDigit(/). Time O(max(m,n)), Space O(max(m,n)), m = a.length(), n = b.length()Note:1. 注意是从String原创 2015-08-15 09:36:03 · 310 阅读 · 0 评论 -
LeetCode Integer to Roman
原题链接在这里:https://leetcode.com/problems/integer-to-roman/与Roman to Integer相呼应。采用贪心算法。之所以需要考虑900,400,90,40这些情况而不考虑800,300,200,80,30,20这些情况是因为:只有左侧加有特罗马字符的才需要添加,右侧加罗马字符可以等下一位。e.g. num = 8, 加"V",然后剩下原创 2015-08-14 11:39:11 · 288 阅读 · 0 评论 -
LeetCode Anagrams
原题链接在这里:https://leetcode.com/problems/anagrams/作者道题让我想起刚才的 Valid Anagram 简直想的太麻烦了。这道题的思路就是sort strs 数组中的每一个string,建立HashMap,key 是 sorted string, value 是 list of all original strings.最后iterate hm原创 2015-08-03 12:09:17 · 329 阅读 · 0 评论 -
LeetCode Roman to Integer
原题链接在这里:https://leetcode.com/problems/roman-to-integer/取当前数值若是比之前的小,就正常加上当前数值,若是比之前位大说明刚才加错了,需要先减掉在加上(cur-pre).e.g. "XIV", at the beginning, res = 10. When i = 1, cur = 1, pre = 10, curpre, res =原创 2015-08-14 04:02:34 · 299 阅读 · 0 评论 -
LeetCode ZigZag Conversion
原题链接在这里:https://leetcode.com/problems/zigzag-conversion/e.g. 若是给出0,1,2,3.....11. numRow = 40 61 57 112 48 103 9发现第一行和最后一行 每两位之间差了interval = 2*(numRow-1). 中间行没两位之间脚踢差了interval - 2*i,原创 2015-09-20 00:31:04 · 387 阅读 · 0 评论