二分查找
Leetcode704. 二分查找
- 题源链接:https://leetcode.cn/problems/binary-search/description/
- 解题思路:
class Solution {
public int search(int[] nums, int target) {
if(nums == null || nums.length == 0){
return -1;
}
if(target < nums[0] || nums[nums.length - 1] < target){
return -1;
}
int left = 0;
int right = nums.length - 1;
while(left <= right){
int temp = (left + right) >> 1;
if(nums[temp] == target){
return temp;
}else if(target < nums[temp]){
right = temp - 1;
}else{
left = temp + 1;
}
}
return -1;
}
}
Leetcode27. 移除元素
public int removeElement(int[] nums, int val) {
if(nums == null || nums.length == 0){
return 0;
}
int point = 0;
for(int i = 0; i < nums.length; i++){
if(nums[i] == val){
continue;
}
nums[point] = nums[i];
point++;
}
return point;
}