上一页 1 ··· 26 27 28 29 30 31 32 33 34 ··· 43 下一页
摘要: 简单题,但也思考了一会。就是pos标志下一个要写入的位置,而current标志下一个可以写入的内容。public class Solution { public int removeDuplicates(int[] A) { if (A.length == 0 || A.length == 1) return A.length; int pos = 0; int current = 0; boolean encounted = false; while (current < A.length) { if (current == 0 || A[curre... 阅读全文
posted @ 2013-09-17 22:08 阿牧遥 阅读(156) 评论(0) 推荐(0)
摘要: 简单题。public class Solution { public ListNode deleteDuplicates(ListNode head) { if (head == null) return null; if (head.next == null) return head; ListNode last = head; ListNode current = head.next; while (current != null) { if (last.val == curre... 阅读全文
posted @ 2013-09-17 21:51 阿牧遥 阅读(143) 评论(0) 推荐(0)
摘要: 树的BFS的简单变种。public class Solution { public ArrayList> zigzagLevelOrder(TreeNode root) { ArrayList> ans = new ArrayList>(); if (root == null) return ans; Queue queue = new LinkedList(); queue.offer(root); int lastLevel = 1; int thisLevel = 0; boolean... 阅读全文
posted @ 2013-09-17 21:36 阿牧遥 阅读(167) 评论(0) 推荐(0)
摘要: 三维DP,用备忘录方式写很简单。但是正如那天讨论时说的那样,使用备忘录法,可能会对复杂度和递推公式理解不够深刻。这道题目的递推公式很简单,但是复杂度的话。。。因为数组里的值每个都被计算仅为一次,所以应该是O(n^3)要注意的是,最后一维的是len+1public class Solution { public boolean isScramble(String s1, String s2) { char[] ca1 = s1.toCharArray(); char[] ca2 = s2.toCharArray(); if (s1.length()... 阅读全文
posted @ 2013-09-16 23:01 阿牧遥 阅读(252) 评论(0) 推荐(0)
摘要: 简单递归。public class Solution { public TreeNode sortedArrayToBST(int[] num) { return partialConvert(num, 0, num.length-1); } private TreeNode partialConvert(int[] num, int left, int right) { if (left > right) return null; if (left == right) { retur... 阅读全文
posted @ 2013-09-14 11:53 阿牧遥 阅读(154) 评论(0) 推荐(0)
摘要: 对这种树的操作,已经比较熟练了。class Solution {public: int sumNumbers(TreeNode *root) { int sum = 0; calculate(root, 0, sum); return sum; } void calculate(TreeNode *root, int currentSum, int& sum) { if (root == NULL) return; currentSum = currentSum * 10 + root->va... 阅读全文
posted @ 2013-09-11 23:44 阿牧遥 阅读(137) 评论(0) 推荐(0)
摘要: 这道题目一看就去翻答案了,因为以前见过,知道并查集可以用来做分组,但这样的题目真的要并查集么?想想也是HashSet之类就能搞定么。果然是HashMap而不是HashSet。下面这个解法我比较容易理解,刚开始乍看发现对每一个n又要左边遍历右边遍历的,仔细一看对每一段遍历过的数字就标记一下,那么保证每个数字只访问一次,还是O(n)。public class Solution { public int longestConsecutive(int[] num) { HashMap map = new HashMap(); int max = 0; f... 阅读全文
posted @ 2013-09-11 23:13 阿牧遥 阅读(257) 评论(0) 推荐(0)
摘要: 有一道链表的题目。今天面试别人出了链表的题目都被答出来了,可见这个一般训练过还是能做出来的,就是考虑corner case即可。这里主要是m为1的时候,head就要变了。class Solution {public: ListNode *reverseBetween(ListNode *head, int m, int n) { ListNode* current = head; ListNode* last = NULL; int i = 1; while (i != m && current != NULL) { ... 阅读全文
posted @ 2013-09-11 22:37 阿牧遥 阅读(167) 评论(0) 推荐(0)
摘要: 此题是DP,有了Edit Distance(http://www.cnblogs.com/lautsie/p/3251311.html)的经验,照理说是能想出来的,但我把字符串分两份后就开始想左边的数目乘以右边的数目,这思路是不对的,是分治的思路,而这种DP的话,是多了一个后,用之前的一两个的状态来计算。所以状态转移方程式dp[i][j]=dp[i-1][j] (s[i-1] != t[j-1]时)或dp[i][j] = dp[i-1][j-1]+dp[i-1][j] (s[i-1]==t[j-1]时)。其中dp[i][j]表示S和T长度为i和j时的Distinct SubSequences。 阅读全文
posted @ 2013-09-09 22:40 阿牧遥 阅读(237) 评论(0) 推荐(0)
摘要: 不要忘记在最后append进位的1,如果有的话;一开始用while写的很快,忘记了i++,死循环了;a+b的情况下,忘记了-'0'两次;还有是要a.charAt(lenA-i-1),一开始用a.charAt(i)了,脑子一乱忘记了0是最左边的;public class Solution { public String addBinary(String a, String b) { int carrior = 0; ArrayList list = new ArrayList(); int lenA = a.length(); int lenB ... 阅读全文
posted @ 2013-09-09 14:05 阿牧遥 阅读(210) 评论(0) 推荐(0)
上一页 1 ··· 26 27 28 29 30 31 32 33 34 ··· 43 下一页