Day1(704, 27)

704. Binary Search

Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.

You must write an algorithm with O(log n) runtime complexity.
Example 1:

Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4

Example 2:

Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1

class Solution {
    public int search(int[] nums, int target) {
	    //先找到最小索引和最大索引
        int min = 0;
        int max = nums.length-1;
        while(true){
	        //索引位置错误min跑到max前面, 说明没有, 直接返回-1
            if (min > max)return -1;
            //二分法找中间索引
            int mid = (min + max)/2;
            //取出中间元素并用来后续比较大小
            int mid_value = nums[mid];
            //分别对应大于小于等于三种情况的处理方法
            if (mid_value < target){
                min = mid + 1;
            } else if (mid_value > target) {
                max = mid - 1;
            } else return mid;
        }
    }
}

心得:二分查找主要就是跟中间值进行比较然后选择区间即可,原理很简单,代码实现细心一点就好

相关题目推荐:

35. Search Insert Position

Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You must write an algorithm with O(log n) runtime complexity.

Example 1:

Input: nums = [1,3,5,6], target = 5
Output: 2

Example 2:

Input: nums = [1,3,5,6], target = 2
Output: 1

Example 3:

Input: nums = [1,3,5,6], target = 7
Output: 4

class Solution {  
    public int searchInsert(int[] nums, int target) {  
        int length = nums.length;  
        int min = 0;  
        int max = length-1;  
        while(min <= max){  
            int mid = (min + max) / 2;  
            int num = nums[mid];  
            if (num == target)return mid;  
            else if (num < target){  
                min = mid + 1;  
            } else {  
                max = mid - 1;  
            }  
        }  
        return min;  
    }  
}

34. Find First and Last Position of Element in Sorted Array

Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value.

If target is not found in the array, return [-1, -1].

You must write an algorithm with O(log n) runtime complexity.

Example 1:

Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]

Example 2:

Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]

Example 3:

Input: nums = [], target = 0
Output: [-1,-1]

class Solution {  
    public int[] searchRange(int[] nums, int target) {  
        int index = BinarySearch(nums, target);  
        if (index == -1)return new int[]{-1, -1};  
        int length = nums.length-1;  
        int[] result = new int[]{0, length};  
        int i, j;  
        i = j = index;  
        while(i >= 0){  
            if (nums[i] != target){  
                result[0] = i + 1;  
                break;  
            }  
            i--;  
        }  
        while(j <= length){  
            if (nums[j] != target){  
                result[1] = j - 1;  
                break;  
            }  
            j++;  
        }  
        return result;  
    }  
  
    public int BinarySearch(int[] nums, int target){  
        int min = 0;  
        int max = nums.length-1;  
        while(true){  
            if (min > max)return -1;  
            int mid = (min + max)/2;  
            int mid_value = nums[mid];  
            if (mid_value < target){  
                min = mid + 1;  
            } else if (mid_value > target) {  
                max = mid - 1;  
            } else return mid;  
        }  
    }  
}

69. Sqrt(x)

Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well.

You must not use any built-in exponent function or operator.

  • For example, do not use pow(x, 0.5) in c++ or x ** 0.5 in python.

Example 1:

Input: x = 4
Output: 2
Explanation: The square root of 4 is 2, so we return 2.

Example 2:

Input: x = 8
Output: 2
Explanation: The square root of 8 is 2.82842…, and since we round it down to the nearest integer, 2 is returned.

class Solution {  
    public int mySqrt(int x) {  
        if (x == 0) return 0;  
        int left = 1, right = x;  
        while (left <= right) {  
            int mid = left + (right - left) / 2;  
            if (mid == x / mid) {  
                return mid;  
            } else if (mid < x / mid) {  
                left = mid + 1;  
            } else {  
                right = mid - 1;  
            }  
        }  
        return right;  
    }  
}

27. Remove Element

Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The relative order of the elements may be changed.

Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.

Return k after placing the final result in the first k slots of nums.

Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.

Example 1:

Input: nums = [3,2,2,3], val = 3
Output: 2, nums = [2,2,,]
Explanation: Your function should return k = 2, with the first two elements of nums being 2.
It does not matter what you leave beyond the returned k (hence they are underscores).

Example 2:

Input: nums = [0,1,2,2,3,0,4,2], val = 2
Output: 5, nums = [0,1,4,0,3,,,_]
Explanation: Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4.
Note that the five elements can be returned in any order.
It does not matter what you leave beyond the returned k (hence they are underscores).

class Solution {
    public int removeElement(int[] nums, int val) {
	    //i为新索引, number为和val比较的数字
        int i = 0;
        int number;
        //此处取局部变量length是为了减少length的调用
        int length = nums.length;
        for(int j = 0; j < length; j++){
            if((number = nums[j]) != val){
	            //相等则给num[i]赋值并且i++
                nums[i++] = number;
            }
        }
        return i;
    }
}

心得:删除元素只需要两个索引分别表示查找索引和新的索引即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值