add
This commit is contained in:
parent
91f7af922b
commit
5a2adb11cb
49
5.leetcode/src/com/fanxb/common/offer/Q53.java
Normal file
49
5.leetcode/src/com/fanxb/common/offer/Q53.java
Normal file
@ -0,0 +1,49 @@
|
||||
package com.fanxb.common.offer;
|
||||
|
||||
/**
|
||||
* 两数相加
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2021/6/1
|
||||
**/
|
||||
public class Q53 {
|
||||
|
||||
public int search(int[] nums, int target) {
|
||||
if (nums.length == 0) {
|
||||
return 0;
|
||||
}
|
||||
//二分查找目标值在数组中的位置
|
||||
int l = 0, r = nums.length - 1;
|
||||
while (l < r) {
|
||||
if (nums[l] == target) {
|
||||
break;
|
||||
}
|
||||
int mid = (l + r) / 2;
|
||||
if (nums[mid] == target) {
|
||||
l = mid;
|
||||
break;
|
||||
}
|
||||
if (nums[mid] < target) {
|
||||
l = mid + 1;
|
||||
} else {
|
||||
r = mid - 1;
|
||||
}
|
||||
}
|
||||
if (nums[l] != target) {
|
||||
return 0;
|
||||
}
|
||||
int index = l, res = 1;
|
||||
//找左边的
|
||||
while (--index > 0 && nums[index] == target) {
|
||||
res++;
|
||||
}
|
||||
while (++l < nums.length && nums[l] == target) {
|
||||
res++;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q53().search(new int[]{5, 7, 7, 8, 8, 10}, 6));
|
||||
}
|
||||
}
|
4
5.leetcode/src/com/fanxb/common/offer/package-info.java
Normal file
4
5.leetcode/src/com/fanxb/common/offer/package-info.java
Normal file
@ -0,0 +1,4 @@
|
||||
package com.fanxb.common.offer;
|
||||
/**
|
||||
* 剑指offer相关题目
|
||||
*/
|
Loading…
x
Reference in New Issue
Block a user