LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)
package leetcode_50; /***
*
* @author pengfei_zheng
* 数组中找到target起止下标
*/
public class Solution34 {
public static int[] searchRange(int[] nums, int target) { int start = 0, end = nums.length-1; int []ans = {-1,-1}; while(start<=end){
int mid = (start + end)/2;
if(nums[mid] >= target)
end = mid - 1;
else
start = mid + 1;
if(nums[mid]==target){
ans[0]=mid;
}
}
start = ans[0]==-1 ? 0 : ans[0];
end=nums.length-1;
while(start<=end){
int mid = (start+end)/2;
if(nums[mid]<=target)
start = mid + 1;
else
end = mid - 1;
if(nums[mid]==target)
ans[1]=mid;
}
return ans;
}
public static void main(String[]args){
int []nums = {5, 7, 7, 8, 8, 10};
int []ans = {0};
ans = searchRange(nums,8);
for(int item:ans){
System.out.print(item+" ");
}
}
}
LeetCode 34 Search for a Range (有序数组中查找给定数字的起止下标)的更多相关文章
- [array] leetcode - 34. Search for a Range - Medium
leetcode - 34. Search for a Range - Medium descrition Given an array of integers sorted in ascending ...
- [LeetCode每日一题]80. 删除有序数组中的重复项 II
[LeetCode每日一题]80. 删除有序数组中的重复项 II 问题 给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 最多出现两次 ,返回删除后数组的新长度. 不要使用额外 ...
- [LeetCode] 34. Find First and Last Position of Element in Sorted Array 在有序数组中查找元素的第一个和最后一个位置
Given an array of integers nums sorted in ascending order, find the starting and ending position of ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- [LeetCode]面试题53 - I. 在排序数组中查找数字 I(二分);面试题53 - II. 0~n-1中缺失的数字(二分)
##面试题53 - I. 在排序数组中查找数字 I ###题目 统计一个数字在排序数组中出现的次数. 示例 1: 输入: nums = [5,7,7,8,8,10], target = 8 输出: 2 ...
- [LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)
原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array Given an ...
- leetcode@ [34] Search for a Range (STL Binary Search)
https://leetcode.com/problems/search-for-a-range/ Given a sorted array of integers, find the startin ...
- leetCode 34.Search for a Range (搜索范围) 解题思路和方法
Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...
- leetcode 34 Search for a Range(二分法)
Search for a Range Given a sorted array of integers, find the starting and ending position of a give ...
随机推荐
- UGUI 加载图片
图片是动态加载的,然后转换为sprite赋值到ugui的按钮上 代码如下 using UnityEngine; using System.Collections; using System.IO; u ...
- fedora26 编译内核出现Can't use 'defined(@array)' 错误
cd /kernel/ vim timeconst.pl 把373行中的if (!defined(@val)) { 改为if (!@val) {
- 安装vmware vCenter Appliance
vcenter appliance是一个vmware配置好的基于suse系统的vcenter的all in one式的虚拟机,比安装基于windows的vcenter服务省事多了,所以我选择部署vCe ...
- 【WP8】线程安全的StorageHelper
14-08-29 12:32更新:修复StorageHelper部分bug WP8以后提供了StorageFile的方式访问文件,StorageFile对文件的操作只提供了异步的支持,包括WP8.1 ...
- Node.js静态文件服务器实战[转]
p.s. 在下面这篇文章的指导下,做了一个静态文件服务器,见:https://github.com/walkerwzy/node_static_server ==== 这是一篇阐述得比较详细的文章,从 ...
- java http post上传文件
1.上传接口 @IgnoreToken @RequestMapping(value = "/upload/cpicFile", method = RequestMethod.POS ...
- Windbg在软件调试中的应用
Windbg在软件调试中的应用 Windbg是微软提供的一款免费的,专门针对Windows应用程序的调试工具.借助于Windbg, 我们常见的软件问题:软件异常,死锁,内存泄漏等,就可以进行高效的排查 ...
- java 实现类似于python requests包的Session类,自动管理cookie。
1.在py中requests.post()和get()函数都是在那个函数内部里面自动生成了一个Session类的实例,所以requests,post和get函数要想干登陆后才能干的事情,需要添加coo ...
- [RN] 02 - Overview: React Native Practice of 50 lectures
观看笔记:零基础 React Native 实战开发视频 50讲 本篇效果:RN入门,整体认识 基本原理 # 1 React到RN # 2 一个简单的例子 /** * Sample React Nat ...
- Postman模拟Request Payload发送请求
Postman模拟Request Payload发送请求,如下图所示: