dengxinyi 6 anos atrás
pai
commit
51043f0928
4 arquivos alterados com 106 adições e 26 exclusões
  1. 8 8
      medium/130.go
  2. 6 6
      medium/166.go
  3. 25 12
      medium/173.js
  4. 67 0
      medium/179.go

+ 8 - 8
medium/130.go

@@ -4,11 +4,11 @@ func solve(board [][]byte) {
 
 
 }
 }
 
 
-// func main() {
-// 	b1 := [][]byte{
-// 		{'X', 'X', 'X', 'X'},
-// 		{'X', 'O', 'O', 'X'},
-// 		{'X', 'O', 'X', 'X'},
-// 		{'X', 'X', 'O', 'X'}}
-// 	solve(b1)
-// }
+func main() {
+	b1 := [][]byte{
+		{'X', 'X', 'X', 'X'},
+		{'X', 'O', 'O', 'X'},
+		{'X', 'O', 'X', 'X'},
+		{'X', 'X', 'O', 'X'}}
+	solve(b1)
+}

+ 6 - 6
medium/166.go

@@ -56,9 +56,9 @@ func absInt64(x int64) int64 {
 	return int64(x)
 	return int64(x)
 }
 }
 
 
-func main() {
-	println(fractionToDecimal(-1, -2147483648))
-	println(fractionToDecimal(-50, 8))
-	println(fractionToDecimal(2, 1))
-	println(fractionToDecimal(0, -5))
-}
+// func main() {
+// 	println(fractionToDecimal(-1, -2147483648))
+// 	println(fractionToDecimal(-50, 8))
+// 	println(fractionToDecimal(2, 1))
+// 	println(fractionToDecimal(0, -5))
+// }

+ 25 - 12
medium/173.js

@@ -9,8 +9,8 @@
  */
  */
 
 
 function TreeNode(val) {
 function TreeNode(val) {
-    this.val = val;
-    this.left = this.right = null;
+    this.val = val
+    this.left = this.right = null
 }
 }
 
 
 /**
 /**
@@ -18,7 +18,20 @@ function TreeNode(val) {
  * @param {TreeNode} root - root of the binary search tree
  * @param {TreeNode} root - root of the binary search tree
  */
  */
 var BSTIterator = function (root) {
 var BSTIterator = function (root) {
-
+    const stack = []
+    this.array = []
+    let curr = root
+    while (stack.length !== 0 || curr !== null) {
+        while (curr !== null) {
+            stack.push(curr)
+            curr = curr.left
+        }
+        if (stack.length !== 0) {
+            curr = stack.pop()
+            this.array.push(curr.val)
+            curr = curr.right
+        }
+    }
 }
 }
 
 
 /**
 /**
@@ -26,7 +39,7 @@ var BSTIterator = function (root) {
  * @returns {boolean} - whether we have a next smallest number
  * @returns {boolean} - whether we have a next smallest number
  */
  */
 BSTIterator.prototype.hasNext = function () {
 BSTIterator.prototype.hasNext = function () {
-
+    return this.array.length !== 0
 }
 }
 
 
 /**
 /**
@@ -34,7 +47,7 @@ BSTIterator.prototype.hasNext = function () {
  * @returns {number} - the next smallest number
  * @returns {number} - the next smallest number
  */
  */
 BSTIterator.prototype.next = function () {
 BSTIterator.prototype.next = function () {
-
+    return this.array.shift()
 }
 }
 
 
 /**
 /**
@@ -44,22 +57,22 @@ BSTIterator.prototype.next = function () {
  */
  */
 
 
 function __main__() {
 function __main__() {
-    /* eslit no-console: ["error", {"allow": ["log"]}] */
+    /* eslint no-console: ["error", {"allow": ["log"]}] */
     const logger = console.log.bind(console)
     const logger = console.log.bind(console)
     //       1
     //       1
     //      / \
     //      / \
     //     2   3
     //     2   3
     //    /    /\
     //    /    /\
     //   4    6  7
     //   4    6  7
-    const n7 = new TreeLinkNode(7)
-    const n6 = new TreeLinkNode(6)
-    const n4 = new TreeLinkNode(4)
-    const n3 = new TreeLinkNode(3)
+    const n7 = new TreeNode(7)
+    const n6 = new TreeNode(6)
+    const n4 = new TreeNode(4)
+    const n3 = new TreeNode(3)
     n3.left = n6
     n3.left = n6
     n3.right = n7
     n3.right = n7
-    const n2 = new TreeLinkNode(2)
+    const n2 = new TreeNode(2)
     n2.left = n4
     n2.left = n4
-    const n1 = new TreeLinkNode(1)
+    const n1 = new TreeNode(1)
     n1.left = n2
     n1.left = n2
     n1.right = n3
     n1.right = n3
     const i = new BSTIterator(n1)
     const i = new BSTIterator(n1)

+ 67 - 0
medium/179.go

@@ -0,0 +1,67 @@
+package main
+
+import (
+	"sort"
+	"strconv"
+	"strings"
+)
+
+// StringSlice ...
+type StringSlice []string
+
+func (strs StringSlice) Len() int {
+	return len(strs)
+}
+
+func (strs StringSlice) Swap(i, j int) {
+	strs[i], strs[j] = strs[j], strs[i]
+}
+
+func (strs StringSlice) Less(i, j int) bool { // Less is "More"
+	len1, len2 := len(strs[i]), len(strs[j])
+	for k := 0; k < len1 && k < len2; k++ {
+		if strs[i][k] != strs[j][k] {
+			return !(strs[i][k] < strs[j][k])
+		}
+	}
+	if len1 < len2 {
+		for k := len1; k < len2; k++ {
+			if strs[i][len1-1] != strs[j][k] {
+				return !(strs[i][len1-1] < strs[j][k])
+			}
+		}
+	} else if len1 > len2 {
+		for k := len2; k < len1; k++ {
+			if strs[i][k] != strs[j][len2-1] {
+				return !(strs[i][k] < strs[j][len2-1])
+			}
+		}
+	}
+	return !false
+}
+
+func largestNumber(nums []int) string {
+	strs := make([]string, 0)
+	for _, n := range nums {
+		strs = append(strs, strconv.Itoa(n))
+	}
+	sort.Sort(StringSlice(strs))
+	var sb strings.Builder
+	for _, s := range strs {
+		if sb.Len() == 0 && s == "0" {
+			continue // No leading zero
+		}
+		sb.WriteString(s)
+	}
+	if sb.Len() == 0 {
+		return "0"
+	}
+	return sb.String()
+}
+
+// func main() {
+// 	println(largestNumber([]int{
+// 		3, 30, 34, 5, 9}))
+// 	println(largestNumber([]int{
+// 		824, 938, 1399, 5607, 6973, 5703, 9609, 4398, 8247}))
+// }