![](https://img-blog.csdnimg.cn/20201014180756724.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
二分
sjphiChina
代码是最为耐心、最能忍耐和最令人愉快的伙伴,在任何艰难困苦的时刻,它都不会抛弃你。
展开
-
Sqrt(x)
这个记住是2分法处理,i从0,j从x/2+1开始算。 当中,必须用long而非int,否则遇到类似2147395599的大数,会超过int的范围,出差。 public class Solution { public int mySqrt(int x) { long i = 0; long j = x/2 + 1; while (i <=原创 2016-06-29 11:56:48 · 278 阅读 · 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 评论 -
Search in Rotated Sorted Array
一道两年前做过的题,差点漏网。再次理解二分的核心思维:通过一次比较,能把目标确定在一边 public class Solution { public int search(int[] A, int target) { if (A == null || A.length == 0) { return -1; } int原创 2016-07-11 14:50:54 · 225 阅读 · 0 评论 -
Find Minimum in Rotated Sorted Array
典型二分法 public class Solution { public int findMin(int[] nums) { if (nums == null || nums.length == 0) { return 0; } if (nums.length == 1) { return n原创 2016-06-19 19:34:07 · 204 阅读 · 0 评论