LeetCode 35 Search Insert Position 二叉查找相关(一)
来源:互联网 发布:吕中 奚美娟 知乎 编辑:程序博客网 时间:2024/06/11 01:57
题目:
https://leetcode.com/problems/search-insert-position/
Given a sorted array 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 may assume no duplicates in the array.
Here are few examples.[1,3,5,6]
, 5 → 2[1,3,5,6]
, 2 → 1[1,3,5,6]
, 7 → 4[1,3,5,6]
, 0 → 0
考查基本的二叉查找算法,直接上代码:
public class Solution { public int searchInsert(int[] A, int target) { if (A == null || A.length == 0) { return 0; } int l = 0; int r = A.length - 1; while (l <= r) { int mid = (l + r) / 2; if (A[mid] == target) { return mid; } else if (A[mid] < target) { l = mid + 1; } else { r = mid -1; } } return l; }}
0 0
- LeetCode 35 Search Insert Position 二叉查找相关(一)
- leetcode---search-insert-position---查找
- LeetCode(35) Search Insert Position
- [LeetCode-35] Search Insert Position(二分法)
- LeetCode (35)Search Insert Position
- 【LeetCode】- Search Insert Position(查找插入的位置)
- LeetCode | Search Insert Position(查找插入位置)
- LeetCode OJ 之 Search Insert Position (查找插入位置)
- LeetCode---35. Search Insert Position(查找插入位置)
- LeetCode Search Insert Position查找插入位置
- Leetcode 二分查找 Search Insert Position
- LeetCode @ Search Insert Position 二分查找
- [leetcode] 【查找】 35. Search Insert Position
- [leetcode-查找]--35. Search Insert Position
- LeetCode 35. Search Insert Position 二分查找
- (二分查找)LeetCode#35. Search Insert Position
- leetcode 35: Search Insert Position
- Leetcode(35) Search Insert Position
- 利用boost获取时间并格式化
- 一小时搞定DIV+CSS布局-固定页面开度布局
- 我bug心得
- deadline_timer
- Mysql show slave status 的研究
- LeetCode 35 Search Insert Position 二叉查找相关(一)
- C#中的定义修饰符总结
- 学习shader之前必须知道的东西之计算机图形学(一)渲染管线
- C语言之结构体浅谈
- boost锁的概述
- IOS SEL (@selector) 原理及使用总结(一)
- 郭靖和黄蓉竟然是这样死的!---某些人最近看了柴静的《穹顶之下》后狂喷她,这些人可能就是本文中的甲乙丙丁
- C++ Primer Plus第六版编程练习6.9解答
- Ural1298 Knight 经典DFS