File tree Expand file tree Collapse file tree 3 files changed +77
-0
lines changed Expand file tree Collapse file tree 3 files changed +77
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for a binary tree node.
3+ * function TreeNode(val, left, right) {
4+ * this.val = (val===undefined ? 0 : val)
5+ * this.left = (left===undefined ? null : left)
6+ * this.right = (right===undefined ? null : right)
7+ * }
8+ */
9+ /**
10+ * @param {TreeNode } root
11+ * @return {boolean }
12+ */
13+ var isBalanced = function ( root ) {
14+ if ( ! root ) return true
15+ const left = findHeight ( root . left )
16+ const right = findHeight ( root . right )
17+ return Math . abs ( left - right ) <= 1 && isBalanced ( root . left ) && isBalanced ( root . right )
18+ } ;
19+
20+ function findHeight ( node ) {
21+ if ( node == null ) return 0 ;
22+ return 1 + Math . max ( this . findHeight ( node . left ) , this . findHeight ( node . right ) ) ;
23+ }
24+
25+ // Runtime: 78 ms, faster than 90.43% of JavaScript online submissions for Balanced Binary Tree.
26+ // Memory Usage: 47.1 MB, less than 32.41% of JavaScript online submissions for Balanced Binary Tree.
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number[] } nums
3+ * @param {number } target
4+ * @return {number }
5+ */
6+ var search = function ( nums , target ) {
7+
8+ let left = 0 ;
9+ let right = nums . length - 1 ;
10+
11+ while ( left <= right ) {
12+ let middle = Math . floor ( ( left + right ) / 2 ) ;
13+
14+ if ( nums [ middle ] === target ) {
15+ return middle ;
16+ } else if ( nums [ middle ] < target ) {
17+ left = middle + 1 ;
18+ } else {
19+ right = middle - 1 ;
20+ }
21+ }
22+
23+ return - 1 ;
24+ } ;
25+
26+ // Runtime: 98 ms, faster than 34.02% of JavaScript online submissions for Binary Search.
27+ // Memory Usage: 44.3 MB, less than 99.18% of JavaScript online submissions for Binary Search.
Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number } x
3+ * @return {boolean }
4+ */
5+ var isPalindrome = function ( x ) {
6+
7+ // Creates array from int characters
8+ // 121 -> [1,2,1]
9+ let arr = Array . from ( String ( x ) , Number ) ;
10+
11+ // Uses two pointer
12+ for ( let i = 0 ; i < arr . length ; i ++ ) {
13+ if ( arr [ i ] !== arr [ arr . length - 1 - i ] ) {
14+ return false ;
15+ }
16+ }
17+
18+ return true ;
19+ } ;
20+
21+
22+ // Runtime: 302 ms, faster than 40.50% of JavaScript online submissions for Palindrome Number.
23+ // Memory Usage: 51.8 MB, less than 8.36% of JavaScript online submissions for Palindrome Number.
24+
You can’t perform that action at this time.
0 commit comments