Skip to content

Commit

Permalink
日常练习
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunbeam-5 committed Jun 9, 2022
1 parent 80f7575 commit 765004a
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 0 deletions.
3 changes: 3 additions & 0 deletions 20030609/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions 20030609/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions 20030609/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions 20030609/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions 20030609/20030609.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Binary file added 20030609/out/production/20030609/二分查找.class
Binary file not shown.
23 changes: 23 additions & 0 deletions 20030609/src/二分查找.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
public class 二分查找 {
public static int binarySearch(int[] nums, int target) {
if (null == nums || nums.length == 0) { return -1; }

int left = 0;
int right = nums.length - 1;
while (left < right) {
int mid = (left + right) >> 1;
if (nums[mid] < target) {
left = mid + 1;
} else {
right = mid;
}
}
return nums[left] == target ? left : -1;
}

public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = binarySearch(arr, 6);
System.out.println(n);
}
}

0 comments on commit 765004a

Please sign in to comment.