File tree 3 files changed +104
-0
lines changed
3 files changed +104
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int divide (int dividend , int divisor ) {
3
+
4
+ boolean isNeg = false ;
5
+ if ((dividend < 0 && divisor > 0 ) || (dividend > 0 && divisor < 0 )) {
6
+ isNeg = true ;
7
+ }
8
+
9
+ int ans = 0 ;
10
+
11
+ long Ldividend = Math .abs ((long ) dividend );
12
+ long Ldivisor = Math .abs ((long ) divisor );
13
+
14
+ if (Ldivisor == 0 ) return Integer .MAX_VALUE ;
15
+ if (Ldividend == 0 || (Ldividend < Ldivisor )) return 0 ;
16
+
17
+ long quot = ldivide (Ldividend , Ldivisor );
18
+
19
+ if (quot > Integer .MAX_VALUE ) {
20
+ ans = isNeg == false ? Integer .MAX_VALUE : Integer .MIN_VALUE ;
21
+ }
22
+ else {
23
+ ans = (int )(isNeg ? -quot : quot );
24
+ }
25
+
26
+ return ans ;
27
+ }
28
+
29
+ private long ldivide (long ldividend , long ldivisor ) {
30
+ if (ldividend < ldivisor ) return 0 ;
31
+
32
+ long sum = ldivisor ;
33
+ long multiple = 1 ;
34
+ while ((sum +sum ) <= ldividend ) {
35
+ sum += sum ;
36
+ multiple += multiple ;
37
+ }
38
+
39
+ return multiple + ldivide (ldividend - sum , ldivisor );
40
+ }
41
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int findMinDifference (List <String > timePoints ) {
3
+ int [] mins = new int [timePoints .size ()];
4
+ int i = 0 ;
5
+ for (String s : timePoints ) {
6
+ int hr = Integer .parseInt (s .split (":" )[0 ]);
7
+ int ms = Integer .parseInt (s .split (":" )[1 ]);
8
+
9
+ mins [i ] = hr *60 + ms ;
10
+ i ++;
11
+ }
12
+
13
+ Arrays .sort (mins );
14
+
15
+ int minTime = Integer .MAX_VALUE ;
16
+
17
+ for (int j =1 ;j <mins .length ;j ++) {
18
+ minTime = Math .min (minTime , mins [j ] - mins [j -1 ]);
19
+ }
20
+
21
+ return Math .min (minTime , mins [0 ] + 1440 - mins [mins .length -1 ]);
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+
3
+ private int [] nums ;
4
+ private Random random ;
5
+
6
+ public Solution (int [] nums ) {
7
+ this .nums = nums ;
8
+ random = new Random ();
9
+ }
10
+
11
+ /** Resets the array to its original configuration and return it. */
12
+ public int [] reset () {
13
+ return nums ;
14
+ }
15
+
16
+ /** Returns a random shuffling of the array. */
17
+ public int [] shuffle () {
18
+ if (nums == null ) return null ;
19
+ int [] shuf = nums .clone ();
20
+ for (int i =1 ;i <shuf .length ;i ++) {
21
+ int l = random .nextInt (i +1 );
22
+ swap (shuf , i , l );
23
+ }
24
+
25
+ return shuf ;
26
+ }
27
+
28
+ private void swap (int [] arr , int a , int b ) {
29
+ int temp = arr [a ];
30
+ arr [a ] = arr [b ];
31
+ arr [b ] = temp ;
32
+ }
33
+ }
34
+
35
+ /**
36
+ * Your Solution object will be instantiated and called as such:
37
+ * Solution obj = new Solution(nums);
38
+ * int[] param_1 = obj.reset();
39
+ * int[] param_2 = obj.shuffle();
40
+ */
You can’t perform that action at this time.
0 commit comments