Skip to content

Commit 79785d4

Browse files
author
YILING
committed
[0.binarySearch]
1 parent d2b24e0 commit 79785d4

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

LeetCode.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
6D668CEE1E96878E0001054C /* 0_Fibonacci.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6D668CEC1E96878E0001054C /* 0_Fibonacci.cpp */; };
11+
6D668CF11E969CD30001054C /* 0_BinarySearch.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6D668CEF1E969CD30001054C /* 0_BinarySearch.cpp */; };
1112
6DEF8F5F1E75A0EC0028351C /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6DEF8F5E1E75A0EC0028351C /* main.cpp */; };
1213
6DEF8F671E75A1780028351C /* 1_two_sum.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6DEF8F651E75A1780028351C /* 1_two_sum.cpp */; };
1314
6DEF8F6A1E76E0F50028351C /* 43_Multiply_Strings.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6DEF8F681E76E0F50028351C /* 43_Multiply_Strings.cpp */; };
@@ -28,6 +29,8 @@
2829
/* Begin PBXFileReference section */
2930
6D668CEC1E96878E0001054C /* 0_Fibonacci.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = 0_Fibonacci.cpp; sourceTree = "<group>"; };
3031
6D668CED1E96878E0001054C /* 0_Fibonacci.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = 0_Fibonacci.hpp; sourceTree = "<group>"; };
32+
6D668CEF1E969CD30001054C /* 0_BinarySearch.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = 0_BinarySearch.cpp; sourceTree = "<group>"; };
33+
6D668CF01E969CD30001054C /* 0_BinarySearch.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = 0_BinarySearch.hpp; sourceTree = "<group>"; };
3134
6DEF8F5B1E75A0EC0028351C /* LeetCode */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = LeetCode; sourceTree = BUILT_PRODUCTS_DIR; };
3235
6DEF8F5E1E75A0EC0028351C /* main.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = main.cpp; sourceTree = "<group>"; };
3336
6DEF8F651E75A1780028351C /* 1_two_sum.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = 1_two_sum.cpp; sourceTree = "<group>"; };
@@ -73,6 +76,8 @@
7376
6DEF8F691E76E0F50028351C /* 43_Multiply_Strings.hpp */,
7477
6D668CEC1E96878E0001054C /* 0_Fibonacci.cpp */,
7578
6D668CED1E96878E0001054C /* 0_Fibonacci.hpp */,
79+
6D668CEF1E969CD30001054C /* 0_BinarySearch.cpp */,
80+
6D668CF01E969CD30001054C /* 0_BinarySearch.hpp */,
7681
);
7782
path = LeetCode;
7883
sourceTree = "<group>";
@@ -137,6 +142,7 @@
137142
6DEF8F671E75A1780028351C /* 1_two_sum.cpp in Sources */,
138143
6DEF8F6A1E76E0F50028351C /* 43_Multiply_Strings.cpp in Sources */,
139144
6D668CEE1E96878E0001054C /* 0_Fibonacci.cpp in Sources */,
145+
6D668CF11E969CD30001054C /* 0_BinarySearch.cpp in Sources */,
140146
6DEF8F5F1E75A0EC0028351C /* main.cpp in Sources */,
141147
);
142148
runOnlyForDeploymentPostprocessing = 0;

LeetCode/0_BinarySearch.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// 0_BinarySearch.cpp
3+
// LeetCode
4+
//
5+
// Created by Yi-Ling Wu on 06/04/2017.
6+
// Copyright © 2017 YILING. All rights reserved.
7+
//
8+
9+
#include "0_BinarySearch.hpp"
10+
11+
int BinarySearch::BinarySearchIterative(int *array, int length, int num) {
12+
int left = 0, right = length - 1;
13+
while (left <= right) {
14+
int middle = (left + right) / 2;
15+
if (array[middle] == num) {
16+
return middle;
17+
}
18+
19+
if (array[middle] > num) {
20+
right = middle - 1;
21+
} else {
22+
left = middle + 1;
23+
}
24+
}
25+
return -1;
26+
}
27+
28+
int BinarySearch::BinaarySearchDeviceAndConquer(int *array, int num, int left, int right) {
29+
if (left > right) return -1;
30+
int middle = (left + right) / 2;
31+
32+
if (array[middle] == num) return middle;
33+
34+
if (array[middle] > num)
35+
return BinarySearch::BinaarySearchDeviceAndConquer(array, num, left, middle - 1);
36+
else
37+
return BinarySearch::BinaarySearchDeviceAndConquer(array, num, middle + 1, right);
38+
return 0;
39+
}

LeetCode/0_BinarySearch.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// 0_BinarySearch.hpp
3+
// LeetCode
4+
//
5+
// Created by Yi-Ling Wu on 06/04/2017.
6+
// Copyright © 2017 YILING. All rights reserved.
7+
//
8+
9+
#ifndef __BinarySearch_hpp
10+
#define __BinarySearch_hpp
11+
12+
#include <stdio.h>
13+
14+
class BinarySearch {
15+
public:
16+
static int BinarySearchIterative(int *array, int length, int num);
17+
static int BinaarySearchDeviceAndConquer(int *array, int num, int left, int right);
18+
};
19+
20+
#endif /* __BinarySearch_hpp */

0 commit comments

Comments
 (0)