https://leetcode-cn.com/problems/binary-search/solution/er-fen-cha-zhao-by-leetcode/

package dataStructureAndAlgorithms.leetcode;

/**

  • ${DESCRIPTION}

  • @author yangningkai

  • @create 2021-07-08 下午8:52
    **/
    public class BinarySearch {
    public static void main(String[] args) {
    int[] nums = {-1,0,3,5,9,12};
    int target = 2;
    int search = search(nums, target);
    System.out.println(search);

    }

    public static int search(int[] nums, int target) {
    int start = 0;
    int end = nums.length - 1;
    int flag = 0;
    while (start < end){
    flag = (start + end) / 2;
    if (nums[flag] == target) {
    return flag;
    } else if (nums[flag] < target) {
    start = flag + 1;
    }else {
    end = flag - 1;
    }
    }
    return -1;
    }
    }

https://leetcode-cn.com/problems/binary-search/solution/er-fen-cha-zhao-by-leetcode/

上一篇:发生了Post错误:错误代码40005,微信返回错误信息:invalid file type


下一篇:linux 配置/更改 php环境变量,切换默认php版本