剑指 Offer 03. 数组中重复的数字https://leetcode-cn.com/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof/
class Solution {
public int findRepeatNumber(int[] nums) {
Set se = new HashSet();
for(int i = 0; ; i++){
se.add(nums[i]);
if(se.size() != i+1){
return nums[i];
}
}
}
}
class Solution {
public int search(int[] nums, int target) {
int n = nums.length;
if(n == 0) return 0;
int l = 0, r = n-1;
while(l < r){
int mid = (l + r) / 2;
if(nums[mid] >= target){
r = mid;
}
else {
l = mid+1;
}
}
int cnt = 0;
for(int i = r; i < n; i++){
if(nums[i] == target) cnt++;
else break;
}
return cnt;
}
}
剑指 Offer 53 - II. 0~n-1中缺失的数字https://leetcode-cn.com/problems/que-shi-de-shu-zi-lcof/
class Solution {
public int missingNumber(int[] nums) {
int n = nums.length;
for(int i = 0; i < n; i++){
if(nums[i] != i) return i;
}
return n;
}
}