Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
5zhu
algorithm-learning
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
algorithm-learning
/
src
/
com
/
lintcode
/
Solution14.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
50 lines (43 loc) · 1.65 KB
master
Breadcrumbs
algorithm-learning
/
src
/
com
/
lintcode
/
Solution14.java
Copy path
Top
File metadata and controls
Code
Blame
50 lines (43 loc) · 1.65 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.lintcode;
/**
* 14. 二分查找
*
* 给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1。
*/
public class Solution14 {
/**
* @param nums: The integer array.
* @param target: Target to find.
* @return: The first position of target. Position starts from 0.
*/
public int binarySearch(int[] nums, int target) {
// write your code here
int length=nums.length;
int pos=length/2;
//目标值比最小值还要小或者比最大值还要大,则肯定不在数组中
if(nums[0]>target||nums[length-1]<target){
return -1;
}
//如果pos处的值比目标值还要大,那么目标值只可能出现在pos的左边
while(nums[pos]>target){
pos=pos/2;
}
//pos处的值小于或者等于目标值,那么目标值可能的范围就是[pos,pos*2];
for(int j=pos;j<=pos*2;j++){
if(nums[j]==target){
pos=j;
break;
}
//遍历到pos*2位置了,还没找到目标值,那么目标值肯定不在数组中
if((j==pos*2)&&(nums[j]!=target)){
return -1;
}
}
//这样只是保证pos处的值是目标值,但不一定是第一次出现的
//因此,比较pos前面一个元素的值与pos处的值是否相等,若相等,则pos往前挪一个
while((pos>=1)&&(nums[pos]==nums[pos-1])){
pos--;
}
return pos;
}
}
You can’t perform that action at this time.