File tree 3 files changed +77
-0
lines changed
3 files changed +77
-0
lines changed Original file line number Diff line number Diff line change
1
+ vector<int > Solution::searchRange (const vector<int > &A, int B) {
2
+
3
+ int firstOcc=-1 ;
4
+ int lastOcc=-1 ;
5
+
6
+ int start=0 ;
7
+ int end=A.size ()-1 ;
8
+
9
+ while (start<=end)
10
+ {
11
+ int mid=start+(end-start)/2 ;
12
+
13
+ if (A[mid]==B)
14
+ {
15
+ firstOcc=mid;
16
+ end=mid-1 ;
17
+ }
18
+ else if (B<A[mid])
19
+ {
20
+ end=mid-1 ;
21
+ }
22
+ else
23
+ start=mid+1 ;
24
+ }
25
+
26
+ start=0 ; end=A.size ()-1 ;
27
+
28
+ while (start<=end)
29
+ {
30
+ int mid=start+(end-start)/2 ;
31
+
32
+ if (A[mid]==B)
33
+ {
34
+ lastOcc=mid;
35
+ start=mid+1 ;
36
+ }
37
+ else if (B<A[mid])
38
+ {
39
+ end=mid-1 ;
40
+ }
41
+ else
42
+ start=mid+1 ;
43
+ }
44
+
45
+ vector<int > sol;
46
+ sol.push_back (firstOcc);
47
+ sol.push_back (lastOcc);
48
+
49
+ return sol;
50
+
51
+ }
52
+
Original file line number Diff line number Diff line change
1
+ string Solution::longestCommonPrefix (vector<string> &A) {
2
+
3
+ string current=A[0 ];
4
+ int min=INT_MAX;
5
+
6
+ int i,j;
7
+
8
+ for (j=1 ;j<A.size ();j++)
9
+ {
10
+ int k=0 ;
11
+ while (current[k]==A[j][k] && k<current.length ()&&k<A[j].length ())
12
+ {
13
+ k++;
14
+ }
15
+
16
+ if (k<min)
17
+ min=k;
18
+
19
+ }
20
+
21
+ string res=current.substr (0 ,min);
22
+ return res;
23
+
24
+ }
25
+
You can’t perform that action at this time.
0 commit comments