File tree Expand file tree Collapse file tree 6 files changed +37
-106
lines changed
0000-0099/0029.Divide Two Integers
0200-0299/0285.Inorder Successor in BST Expand file tree Collapse file tree 6 files changed +37
-106
lines changed Original file line number Diff line number Diff line change @@ -89,8 +89,8 @@ class Solution {
8989 if ((a < 0 ) != (b < 0 )) {
9090 sign = - 1 ;
9191 }
92- long x = abs(a);
93- long y = abs(b);
92+ long x = Math . abs(( long ) a);
93+ long y = Math . abs(( long ) b);
9494 long tot = 0 ;
9595 while (x >= y) {
9696 int cnt = 0 ;
@@ -106,13 +106,6 @@ class Solution {
106106 }
107107 return Integer . MAX_VALUE ;
108108 }
109-
110- private long abs (long a ) {
111- if (a < 0 ) {
112- return - a;
113- }
114- return a;
115- }
116109}
117110```
118111
Original file line number Diff line number Diff line change @@ -85,8 +85,8 @@ class Solution {
8585 if ((a < 0 ) != (b < 0 )) {
8686 sign = - 1 ;
8787 }
88- long x = abs(a);
89- long y = abs(b);
88+ long x = Math . abs(( long ) a);
89+ long y = Math . abs(( long ) b);
9090 long tot = 0 ;
9191 while (x >= y) {
9292 int cnt = 0 ;
@@ -102,13 +102,6 @@ class Solution {
102102 }
103103 return Integer . MAX_VALUE ;
104104 }
105-
106- private long abs (long a ) {
107- if (a < 0 ) {
108- return - a;
109- }
110- return a;
111- }
112105}
113106```
114107
Original file line number Diff line number Diff line change @@ -4,8 +4,8 @@ public int divide(int a, int b) {
44 if ((a < 0 ) != (b < 0 )) {
55 sign = -1 ;
66 }
7- long x = abs (a );
8- long y = abs (b );
7+ long x = Math . abs (( long ) a );
8+ long y = Math . abs (( long ) b );
99 long tot = 0 ;
1010 while (x >= y ) {
1111 int cnt = 0 ;
@@ -21,11 +21,4 @@ public int divide(int a, int b) {
2121 }
2222 return Integer .MAX_VALUE ;
2323 }
24-
25- private long abs (long a ) {
26- if (a < 0 ) {
27- return -a ;
28- }
29- return a ;
30- }
31- }
24+ }
Original file line number Diff line number Diff line change @@ -174,34 +174,18 @@ public:
174174 * @param {TreeNode} p
175175 * @return {TreeNode}
176176 */
177- var inorderSuccessor = function (root, p) {
178- function findMin(root) {
179- if (!root) {
180- return null;
181- }
182- while (root.left) {
183- root = root.left;
184- }
185- return root;
186- }
187- if (!root) {
188- return null;
189- }
190- let successor = null;
191- while (root) {
192- if (root.val > p.val) {
193- successor = root;
194- root = root.left;
195- } else if (root.val < p.val) {
196- root = root.right;
177+ var inorderSuccessor = function(root, p) {
178+ let cur = root;
179+ let ans = null;
180+ while (cur != null) {
181+ if (cur.val <= p.val) {
182+ cur = cur.right;
197183 } else {
198- if (root.right) {
199- successor = findMin(root.right);
200- }
201- break;
184+ ans = cur;
185+ cur = cur.left;
202186 }
203187 }
204- return successor ;
188+ return ans ;
205189};
206190```
207191
Original file line number Diff line number Diff line change @@ -158,34 +158,18 @@ public:
158158 * @param {TreeNode} p
159159 * @return {TreeNode}
160160 */
161- var inorderSuccessor = function (root, p) {
162- function findMin(root) {
163- if (!root) {
164- return null;
165- }
166- while (root.left) {
167- root = root.left;
168- }
169- return root;
170- }
171- if (!root) {
172- return null;
173- }
174- let successor = null;
175- while (root) {
176- if (root.val > p.val) {
177- successor = root;
178- root = root.left;
179- } else if (root.val < p.val) {
180- root = root.right;
161+ var inorderSuccessor = function(root, p) {
162+ let cur = root;
163+ let ans = null;
164+ while (cur != null) {
165+ if (cur.val <= p.val) {
166+ cur = cur.right;
181167 } else {
182- if (root.right) {
183- successor = findMin(root.right);
184- }
185- break;
168+ ans = cur;
169+ cur = cur.left;
186170 }
187171 }
188- return successor ;
172+ return ans ;
189173};
190174```
191175
Original file line number Diff line number Diff line change 1010 * @param {TreeNode } p
1111 * @return {TreeNode }
1212 */
13- var inorderSuccessor = function ( root , p ) {
14- function findMin ( root ) {
15- if ( ! root ) {
16- return null ;
17- }
18- while ( root . left ) {
19- root = root . left ;
20- }
21- return root ;
22- }
23- if ( ! root ) {
24- return null ;
25- }
26- let successor = null ;
27- while ( root ) {
28- if ( root . val > p . val ) {
29- successor = root ;
30- root = root . left ;
31- } else if ( root . val < p . val ) {
32- root = root . right ;
33- } else {
34- if ( root . right ) {
35- successor = findMin ( root . right ) ;
36- }
37- break ;
38- }
39- }
40- return successor ;
13+ var inorderSuccessor = function ( root , p ) {
14+ let cur = root ;
15+ let ans = null ;
16+ while ( cur != null ) {
17+ if ( cur . val <= p . val ) {
18+ cur = cur . right ;
19+ } else {
20+ ans = cur ;
21+ cur = cur . left ;
22+ }
23+ }
24+ return ans ;
4125} ;
You can’t perform that action at this time.
0 commit comments