背诵
sjphiChina
代码是最为耐心、最能忍耐和最令人愉快的伙伴,在任何艰难困苦的时刻,它都不会抛弃你。
展开
-
Median of two Sorted Arrays
参照点击打开链接用二叉搜索来处理此题以如下两个数组为例:012345a0a1a2a3a4a5012345b0b1b2b3原创 2016-05-29 20:27:50 · 335 阅读 · 0 评论 -
Different Ways to Add Parentheses
把数字用符号隔开,用分治法求出所有可能。public class Solution { public List diffWaysToCompute(String input) { List result = new LinkedList<>(); for (int i = 0; i < input.length(); i++) {原创 2016-06-23 12:18:56 · 186 阅读 · 0 评论 -
Basic Calculator II
对此,就是理解思路,熟练无bugpublic class Solution { public int calculate(String s) { int sign = 1; int prev = 0; int mulDiv = -1; int res = 0; for (int i = 0; i < s.l原创 2016-06-20 06:05:29 · 210 阅读 · 0 评论 -
Regular Expression Matching
参考:点击打开链接对于这种几乎最高难度的题,甚于word ladder II,要放平心态。坚持,加油!public class Solution { public boolean isMatch(String s, String p) { if (p.length() == 0) { return s.length() == 0;原创 2016-07-04 09:02:21 · 276 阅读 · 0 评论 -
Integer to English Words
此题当中有很多要注意的点:1. 各个字符数组的开头,都是空字符串;2. less100的下标第1个字符串是空的;3. 各个不同的空格;4. 最后返回res得做trim()处理;5. helper中的helper递归调用都是放在结尾的;6. 还是背一背吧,哈哈public class Solution { private final String[] less20原创 2016-07-01 15:17:19 · 230 阅读 · 0 评论 -
Permutations II
背诵。理解布尔数组的意义。public class Solution { public List> permuteUnique(int[] nums) { List> results = new LinkedList<>(); if (nums == null || nums.length == 0) { return resul原创 2016-06-15 17:15:29 · 208 阅读 · 0 评论 -
Longest Palindromic Substring
参考点击打开链接关键是第一个for循环,找到每个字符串的中心位置,然后以此中心位置向两边出发,public class Solution { public String longestPalindrome(String s) { if (s == null || s.length() == 0) { return ""; }原创 2016-06-15 14:38:40 · 219 阅读 · 0 评论 -
Substring with Concatenation of All Words
参考小莹子同学的点击打开链接自己刚才一直在纠结这段code,其实就是保证还剩下最后一个当前满足条件的word能留下。if (curDict.get(temp) < dict.get(temp)) { count--; }ArrayList list = findSubstring("原创 2016-06-30 16:44:46 · 254 阅读 · 0 评论 -
H-Index II
对于此题,背诵就够了。不过要明白的是,n-index得到的是大于等于这个index的数量,即在数组中,大于等于citation[i](含自己)的数量。最后返回n-min。常用0,1,3,5,6,7,8作例子吧public class Solution { public int hIndex(int[] citations) { int n = citatio原创 2016-06-29 13:12:12 · 287 阅读 · 0 评论 -
ZigZag Conversion
这是一道让自己呵呵呵呵的题,到现在依然还是红红火火恍恍惚惚哼哼哈嘿。当中这两个是关键点:1. int size = 2*numRows - 2;2.这个尤为不好懂,这个字符是紧接着size位置的字符(从行来看的话)int temp = j + size - 2*i;请参考:点击打开链接,人家还是个妹子!!!最后,即便都这样了,能不能不要犯下面的2个错误!!!public c原创 2016-06-10 12:15:26 · 178 阅读 · 0 评论 -
Rotate Image
置换规律:参考点击打开链接int offset = i - start;public class Solution { public void rotate(int[][] matrix) { int start = 0, end = matrix.length - 1; while (start < end) { for (原创 2016-06-23 07:26:34 · 257 阅读 · 0 评论 -
Lowest Common Ancestor
这道题,请背诵,理解/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class原创 2016-06-04 21:51:19 · 267 阅读 · 0 评论 -
Palindrome Linked List
这是一道很考验自己基本功的题我们采用了网上通行的O(n)时间,O(1)空间的解法1. 找到链表的中间节点,2. 将中间节点之后的链表反转,3. 用头结点和中间节点后的链表比较。当中第1步和第2步是基本功,自己必须好好掌握。/** * Definition for singly-linked list. * public class ListNode { * i原创 2016-06-04 21:37:02 · 191 阅读 · 0 评论 -
Contains Duplicate III
这道题确实和之前的I,II有很大差别,当中自己也是要学甚多新的东西1. 我们需要学习SortedSet的思维和用法2. 这种特殊的二叉搜索3. 知道为啥吗set.remove((long)nums[i - k]);public class Solution { public boolean containsNearbyAlmostDuplicate(int[] nums原创 2016-06-07 23:20:39 · 231 阅读 · 0 评论 -
Verify Preorder Sequence in Binary Search Tree
一道很好的拓展题,对于follow up,我们之后继续看。参考:点击打开链接public class Solution { public boolean verifyPreorder(int[] preorder) { int min = Integer.MIN_VALUE; Stack stack = new Stack<>();原创 2016-06-23 15:12:51 · 254 阅读 · 0 评论