摘要: 暴力解决。 1 public boolean isValidSudoku(char[][] board) { 2 if(board == null || board.length == 0) { 3 return false; 4 } 5 for(int i = 0; i < 9; i += 3) 阅读全文
posted @ 2016-02-27 01:32 warmland 阅读(169) 评论(0) 推荐(0) 编辑
摘要: 肯定一看就是二分搜索。问题在于数列中并没有找到这个数的情况 当退出的时候,如果nums[mid] < target的时候,说明刚才退出时因为改变了low,导致low > high所以退出了循环,所以就直接返回low 如果nums[mid] > target,说明因为改变了high退出的,所以结果是h 阅读全文
posted @ 2016-02-27 01:03 warmland 阅读(118) 评论(0) 推荐(0) 编辑
摘要: 就是二分搜索,做三遍。 1.第一遍找到第一个target 2.第二遍找到左侧的边界 3.第三遍找到右侧的边界 1 public int[] searchRange(int[] nums, int target) { 2 int[] res = new int[2]; 3 res[0] = -1; 4 阅读全文
posted @ 2016-02-26 10:10 warmland 阅读(121) 评论(0) 推荐(0) 编辑
摘要: 这道题思路如下: 1.从后往前找到第一个不是递增的数字的位置,记录下为p,如果p已经走到尽头,那么直接颠倒所有数字然后返回 2.从p往后找到比p大的最小的那个数的位置q 3.交换p,q的位置 4.对于p之后的所有的数颠倒位置 理由是这样的,如果一个数从后往前的数字都是递增的,那么这个数已经是排列出来 阅读全文
posted @ 2016-02-26 08:53 warmland 阅读(345) 评论(0) 推荐(0) 编辑
摘要: 最简单的方法,暴力找 1 public int strStr(String haystack, String needle) { 2 if(needle == null || needle.length() == 0) { 3 return 0; 4 } 5 if(haystack.length() 阅读全文
posted @ 2016-02-24 09:04 warmland 阅读(133) 评论(0) 推荐(0) 编辑
摘要: 和26题Remove Duplicates from Sorted Array是一样的 1 public int removeElement(int[] nums, int val) { 2 if(nums == null || nums.length == 0) { 3 return 0; 4 } 阅读全文
posted @ 2016-02-02 05:02 warmland 阅读(86) 评论(0) 推荐(0) 编辑
摘要: 用一个cnt来记录有效的位置,遍历一边 1 public int removeDuplicates(int[] nums) { 2 if(nums == null || nums.length == 0) { 3 return 0; 4 } 5 int cnt = 1; 6 for(int i = 阅读全文
posted @ 2016-02-02 04:34 warmland 阅读(98) 评论(0) 推荐(0) 编辑
摘要: 新建一个链表要方便的多 要注意一下初始的判断条件,并不是 l1 == null || l2 == null, return null这种,是其中一个为空的话,就返回另外一个 阅读全文
posted @ 2016-01-28 08:59 warmland 阅读(79) 评论(0) 推荐(0) 编辑
摘要: 基础题目 建一个堆栈,对于这个字符串,如果是左括号就放到堆栈里面,如果是右括号,就弹出堆栈里的一个元素,如果此时堆栈是空的,肯定是不匹配的,如果弹出的不匹配,也返回false。做完以后看堆栈是不是正好的空的,如果是空的就返回true,否则就是false。 1 public boolean isVal 阅读全文
posted @ 2016-01-28 08:37 warmland 阅读(105) 评论(0) 推荐(0) 编辑
摘要: 基础链表操作。 但是居然还是犯了好几个错误。 在链表为一个节点,并且要求删除倒数第一个节点的case里面错了。 我自己的做法是,设置一个假头。 1 public ListNode removeNthFromEnd(ListNode head, int n) { 2 if(head == null) 阅读全文
posted @ 2016-01-28 08:17 warmland 阅读(172) 评论(0) 推荐(0) 编辑