File tree Expand file tree Collapse file tree 4 files changed +5
-42
lines changed
solution/src/main/java/com/inuker/solution
test/src/main/java/com/inuker/test Expand file tree Collapse file tree 4 files changed +5
-42
lines changed Original file line number Diff line number Diff line change @@ -44,18 +44,14 @@ public TreeNode helper(TreeNode root) {
44
44
if (root == null ) {
45
45
return null ;
46
46
}
47
- if (root .left == null && root .right == null ) {
48
- return root ;
49
- }
50
-
51
47
TreeNode right = root .right ;
52
48
TreeNode leftTail = null , rightTail = null ;
53
49
54
50
if (root .left != null ) {
55
51
leftTail = helper (root .left );
56
52
root .right = root .left ;
57
- leftTail .right = right ;
58
53
root .left = null ;
54
+ leftTail .right = right ;
59
55
}
60
56
61
57
rightTail = helper (right );
Original file line number Diff line number Diff line change 14
14
* remove时要判断删除的是不是最后一个,另外交换了结尾后要更新结尾数的idx
15
15
*/
16
16
// 耗时111ms
17
- public class RandomizedSet {
17
+ public class InsertDeleteGetRandom {
18
18
19
19
private HashMap <Integer , Integer > mMap ;
20
20
private List <Integer > mList ;
21
21
private Random mRandom ;
22
22
23
- public RandomizedSet () {
23
+ public InsertDeleteGetRandom () {
24
24
mList = new ArrayList <Integer >();
25
25
mMap = new HashMap <Integer , Integer >();
26
26
mRandom = new Random ();
Original file line number Diff line number Diff line change 14
14
/**
15
15
* 这题的区别在于允许数字重复,那么保存index需要用set,删除的时候从set中随便删一个就行了
16
16
*/
17
- public class RandomizedCollection {
17
+ public class InsertDeleteGetRandomII {
18
18
19
19
private HashMap <Integer , HashSet <Integer >> map ;
20
20
private List <Integer > list = new ArrayList <Integer >();
21
21
private Random random ;
22
22
23
23
/** Initialize your data structure here. */
24
- public RandomizedCollection () {
24
+ public InsertDeleteGetRandomII () {
25
25
map = new HashMap <Integer , HashSet <Integer >>();
26
26
list = new Vector <Integer >();
27
27
random = new Random ();
Original file line number Diff line number Diff line change 21
21
*/
22
22
23
23
public class Test2 {
24
- /**
25
- * TestCase
26
- * [3,1,1], 3
27
- */
28
- public int search (int [] nums , int target ) {
29
- int left = 0 , right = nums .length - 1 ;
30
-
31
- while (left <= right ) {
32
- int mid = (left + right ) / 2 ;
33
-
34
- if (target == nums [mid ]) {
35
- return mid ;
36
- }
37
-
38
- if (nums [mid ] > nums [left ]) {
39
- if (target >= nums [left ] && target < nums [mid ]) {
40
- right = mid - 1 ;
41
- } else {
42
- left = mid + 1 ;
43
- }
44
- } else if (nums [mid ] == nums [left ]) {
45
- left ++;
46
- } else {
47
- if (target > nums [mid ] && target <= nums [right ]) {
48
- left = mid + 1 ;
49
- } else {
50
- right = mid - 1 ;
51
- }
52
- }
53
- }
54
-
55
- return -1 ;
56
- }
57
24
58
25
}
You can’t perform that action at this time.
0 commit comments