leetcode35-Search Insert Position

排序数组搜索某个元素,这种思维一定要往二分法上靠

public class searchInsertPosition{
	public static void main(String[] args) {
		int arr[] = {1,3,5,6};
		System.out.println(getIndex(arr,2));
	}
	public static int getIndex(int[] arr,int target) {
		int start = 0;
		int end = arr.length - 1;
		while(start <= end) {
			int mid = (start + end) / 2;
			if(arr[mid] == target) {
				return mid;
			}
			if(arr[mid] < target) {
				start = mid + 1;
			} else {
				end = mid - 1;
			}
		}
		return start;
	}
}

相关推荐

  1. LEETCODE-DAY35

    2024-03-26 01:08:05       43 阅读
  2. LeetCode 35, 242, 994

    2024-03-26 01:08:05       22 阅读
  3. LeetCode 36

    2024-03-26 01:08:05       36 阅读
  4. leetcode35-Search Insert Position

    2024-03-26 01:08:05       38 阅读
  5. LeetCode题(66,69,35,88)--《c++》

    2024-03-26 01:08:05       27 阅读
  6. LeetCode day30

    2024-03-26 01:08:05       71 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-26 01:08:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-26 01:08:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-26 01:08:05       87 阅读
  4. Python语言-面向对象

    2024-03-26 01:08:05       96 阅读

热门阅读

  1. linux下自动续签https证书

    2024-03-26 01:08:05       43 阅读
  2. 交换瓶子。

    2024-03-26 01:08:05       42 阅读
  3. 被指标化后的生活

    2024-03-26 01:08:05       36 阅读
  4. objection命令语句大全简洁版

    2024-03-26 01:08:05       40 阅读
  5. [HITCON 2016]Leaking

    2024-03-26 01:08:05       42 阅读
  6. 我的VSCode配置和常见插件

    2024-03-26 01:08:05       46 阅读
  7. 7-13 验证“哥德巴赫猜想”

    2024-03-26 01:08:05       38 阅读
  8. 网络编程基础

    2024-03-26 01:08:05       36 阅读