摘要: 一般的二分查找都是low = mid - 1或者high = mid + 1之类来避免重复,但此题必须要比较两个数值,故而要low=mid或者high = mid。public class Solution { public int findMin(int[] num) { if... 阅读全文
posted @ 2014-12-30 12:50 江南第一少 阅读(78) 评论(0) 推荐(0)
摘要: Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array[2,3,-2,4],the... 阅读全文
posted @ 2014-12-30 11:56 江南第一少 阅读(126) 评论(0) 推荐(0)
摘要: 先按空格split, 然后把不是空格的加进一个list,然后reverse list,然后连起来。public class Solution { public String reverseWords(String s) { if(s == null || s.length() =... 阅读全文
posted @ 2014-12-30 11:06 江南第一少 阅读(104) 评论(0) 推荐(0)
摘要: 自己裸写,一遍过,牛!搜了一下,别人跟我写的差不多,还有讲解: 戳参考链接public class Solution { public int evalRPN(String[] tokens) { Stack s = new Stack(); for(int i =... 阅读全文
posted @ 2014-12-30 10:25 江南第一少 阅读(100) 评论(0) 推荐(0)
摘要: 数学题,以任意一点为中心,算其他所有点跟该点得斜率,斜率相同则在同一条直线上,用hashmap记下同一直线上的数字。要注意corner case,坐标完全相同的点,以及横坐标相同的点(相当于斜率为无穷)。遍历以每一个点为中心,更新最大值,即可得结果。public class Solution { ... 阅读全文
posted @ 2014-12-30 09:40 江南第一少 阅读(119) 评论(0) 推荐(0)
摘要: 这道题是一个数据结构设计题,在leetcode里面就这么一道,还是挺经典的一道题,可以好好看看。这道题要求设计实现LRU cache的数据结构,实现set和get功能。学习过操作系统的都应该知道,cache作为缓存可以帮助快速存取数据,但是确定是容量较小。这道题要求实现的cache类型是LRU,LR... 阅读全文
posted @ 2014-12-30 05:13 江南第一少 阅读(199) 评论(0) 推荐(0)
摘要: Merge Sort.public class Solution { public ListNode sortList(ListNode head) { if(head == null || head.next == null) return head; ListN... 阅读全文
posted @ 2014-12-29 13:30 江南第一少 阅读(93) 评论(0) 推荐(0)
摘要: Insertion Sort就是把一个一个元素往已排好序的list中插入的过程。初始时,sorted list是空,把一个元素插入sorted list中。然后,在每一次插入过程中,都是找到最合适位置进行插入。因为是链表的插入操作,需要维护pre,cur和next3个指针。pre始终指向sorted... 阅读全文
posted @ 2014-12-29 13:12 江南第一少 阅读(94) 评论(0) 推荐(0)
摘要: 参考链接1参考链接2两个stack:public class Solution { public List postorderTraversal(TreeNode root) { Stack s1 = new Stack(); Stack s2 = new Stac... 阅读全文
posted @ 2014-12-29 11:19 江南第一少 阅读(107) 评论(0) 推荐(0)
摘要: 三步走: 第一步,分成两部分;第二步,将第二部分reverse;第三步,将两个list merge起来。一定要舍得画图,把指针的指向搞清楚。public class Solution { public void reorderList(ListNode head) { if(he... 阅读全文
posted @ 2014-12-29 06:40 江南第一少 阅读(96) 评论(0) 推荐(0)