File tree Expand file tree Collapse file tree
collectiontutorial/maptutorial
test/java/cn/byhieg/algorithmtutorialtest Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package cn .byhieg .algorithmtutorial ;
2+
3+ /**
4+ * Created by shiqifeng on 2017/3/29.
5+ 6+ */
7+ public class Find {
8+
9+
10+ /**
11+ * 二查查找算法,要求准确找到目标值,没有找到则是-1.
12+ * 此方法保证在相同元素都满足条件时,取到的是最大的下标
13+ * 时间复杂度 o(lgN)
14+ * @param nums int型数组,要求有序
15+ * @return 找到,返回下标,没找到,返回-1
16+ */
17+ public int binarySerachFind (int [] nums ,int des ) {
18+ int length = nums .length ;
19+ int low = 0 ;
20+ int high = length - 1 ;
21+ while (low < high ) {
22+ int mid = low + (high - low ) / 2 ;
23+ if (nums [mid ] == des ) {
24+ return mid ;
25+ } else if (nums [mid ] < des ) {
26+ low = mid + 1 ;
27+ } else {
28+ high = mid - 1 ;
29+ }
30+ }
31+ return -1 ;
32+ }
33+
34+ /**
35+ * 给定一个单调不降的数组,查找大于des条件的最小的数
36+ * @param nums
37+ * @param des
38+ * @return
39+ */
40+ public int binarySearchMinFind (int [] nums , int des ) {
41+ int length = nums .length ;
42+ int low = 0 ;
43+ int high = length - 1 ;
44+ int mid ;
45+ while (low < high ) {
46+ mid = (low + high ) / 2 ;
47+ if (nums [mid ] <= des ){
48+ low = mid + 1 ;
49+ }else {
50+ high = mid ;
51+ }
52+ }
53+ if (nums [high ] > des ) return high ;
54+ return -1 ;
55+ }
56+
57+ /**
58+ * 给定一个单调不降的数组,查找小于des条件的最大的数
59+ * @param nums
60+ * @param des
61+ * @return
62+ */
63+ public int binarySearchMaxFind (int [] nums , int des ) {
64+ int length = nums .length ;
65+ int low = 0 ;
66+ int high = length - 1 ;
67+ int mid ;
68+ int result = -1 ;
69+ while (low < high ) {
70+ mid = low + (high - low + 1 ) / 2 ;
71+ if (nums [mid ] < des ){
72+ low = mid ;
73+ }else {
74+ high = mid - 1 ;
75+ }
76+ }
77+ if (nums [low ] < des ) return low ;
78+ return -1 ;
79+ }
80+
81+
82+
83+ }
Original file line number Diff line number Diff line change 11package cn .byhieg .collectiontutorial .maptutorial ;
22
3- import java .util .HashMap ;
4- import java .util .Iterator ;
5- import java .util .Map ;
3+ import java .util .*;
64
75/**
86 * Created by shiqifeng on 2017/2/24.
Original file line number Diff line number Diff line change 1+ package cn .byhieg .algorithmtutorialtest ;
2+
3+ import cn .byhieg .algorithmtutorial .Find ;
4+ import junit .framework .TestCase ;
5+
6+ /**
7+ * Created by shiqifeng on 2017/3/29.
8+ 9+ */
10+ public class FindTest extends TestCase {
11+ int [] nums ;
12+ int result ;
13+ public void setUp () throws Exception {
14+ super .setUp ();
15+ nums = new int []{1 };
16+ }
17+
18+ public void tearDown () throws Exception {
19+ System .out .println (result );
20+ }
21+
22+ // public void testBinarySerachFind() throws Exception {
23+ // result = new Find().binarySerachFind(nums,6);
24+ // }
25+ ////
26+ // public void testBinarySearchMinFind() throws Exception {
27+ // result = new Find().binarySearchMinFind(nums,1);
28+ // }
29+
30+ public void testBinarySearchMaxFind () throws Exception {
31+ result = new Find ().binarySearchMaxFind (nums , 2 );
32+ }
33+
34+ }
You can’t perform that action at this time.
0 commit comments