dengxinyi 6 lat temu
rodzic
commit
3e2665a7ca

+ 10 - 0
easy/401.binary-watch.go

@@ -0,0 +1,10 @@
+func readBinaryWatch(num int) (res []string) {
+	for h := uint8(0); h < 12; h++ {
+		for m := uint8(0); m < 60; m++ {
+			if bits.OnesCount8(h)+bits.OnesCount8(m) == num {
+				res = append(res, fmt.Sprintf("%d:%02d", h, m))
+			}
+		}
+	}
+	return
+}

+ 30 - 0
easy/404.sum-of-left-leaves.go

@@ -0,0 +1,30 @@
+/**
+ * Definition for a binary tree node.
+ * type TreeNode struct {
+ *     Val int
+ *     Left *TreeNode
+ *     Right *TreeNode
+ * }
+ */
+func sumOfLeftLeaves(root *TreeNode) int {
+	if root == nil {
+		return 0
+	}
+	return sumRecurse(root, false)
+}
+
+func sumRecurse(root *TreeNode, isLeft bool) (sum int) {
+	if root.Left == nil && root.Right == nil {
+		if isLeft {
+			return root.Val
+		}
+		return
+	}
+	if root.Left != nil {
+		sum += sumRecurse(root.Left, true)
+	}
+	if root.Right != nil {
+		sum += sumRecurse(root.Right, false)
+	}
+	return
+}

+ 17 - 0
easy/405.convert-a-number-to-hexadecimal.go

@@ -0,0 +1,17 @@
+var cvt []rune = []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}
+
+func toHex(num int) string {
+	if num == 0 {
+		return "0"
+	}
+	res := make([]rune, 0)
+	//  Arithmetic shifts for signed integer and logical shifts for unsigned integer.
+	for n := uint32(num); n != 0; n >>= 4 {
+		res = append(res, cvt[n&0xf])
+	}
+	for l, r := 0, len(res)-1; l < r; l, r = l+1, r-1 {
+		res[l], res[r] = res[r], res[l]
+	}
+	return string(res)
+}
+

+ 25 - 0
easy/409.longest-palindrome.go

@@ -0,0 +1,25 @@
+func longestPalindrome(s string) (l int) {
+	freq := make([]int, 256)
+	for _, r := range s {
+		freq[r]++
+	}
+	hasOdd := false
+	for i := 'a'; i <= 'z'; i++ {
+		l += freq[i]
+		if freq[i]&1 == 1 {
+			l--
+			hasOdd = true
+		}
+	}
+	for i := 'A'; i <= 'Z'; i++ {
+		l += freq[i]
+		if freq[i]&1 == 1 {
+			l--
+			hasOdd = true
+		}
+	}
+	if hasOdd {
+		return l + 1
+	}
+	return
+}

+ 14 - 0
easy/412.fizz-buzz.go

@@ -0,0 +1,14 @@
+func fizzBuzz(n int) (res []string) {
+	for i := 1; i <= n; i++ {
+		if fizz, buzz := i%3 == 0, i%5 == 0; fizz && buzz {
+			res = append(res, "FizzBuzz")
+		} else if fizz {
+			res = append(res, "Fizz")
+		} else if buzz {
+			res = append(res, "Buzz")
+		} else {
+			res = append(res, strconv.Itoa(i))
+		}
+	}
+	return
+}

+ 49 - 0
easy/414.third-maximum-number.go

@@ -0,0 +1,49 @@
+func thirdMax(nums []int) int {
+	max := make([]int, 3)
+	l := 0
+	for _, i := range nums {
+		switch l {
+		case 0:
+			max[0] = i
+			l++
+		case 1:
+			if i == max[0] {
+				continue
+			}
+			if i < max[0] {
+				max[1] = i
+			} else {
+				max[0], max[1] = i, max[0]
+			}
+			l++
+		case 2:
+			if max[0] == i || max[1] == i {
+				continue
+			}
+			if i < max[1] {
+				max[2] = i
+			} else if i < max[0] {
+				max[1], max[2] = i, max[1]
+			} else {
+				max[0], max[1], max[2] = i, max[0], max[1]
+			}
+			l++
+		case 3:
+			if max[0] == i || max[1] == i || max[2] == i || i < max[2] {
+				continue
+			}
+			if i < max[1] {
+				max[2] = i
+			} else if i < max[0] {
+				max[1], max[2] = i, max[1]
+			} else {
+				max[0], max[1], max[2] = i, max[0], max[1]
+			}
+		}
+	}
+	if l < 3 {
+		return max[0]
+	}
+	return max[l-1]
+}
+

+ 30 - 0
easy/415.add-strings.go

@@ -0,0 +1,30 @@
+func addStrings(num1 string, num2 string) string {
+	m, n := len(num1), len(num2)
+	n1, n2 := []rune(num1), []rune(num2)
+	if m < n {
+		m, n = n, m
+		n1, n2 = n2, n1
+	}
+	carry := '0'
+	for i, j := m-1, n-1; 0 <= i; i, j = i-1, j-1 {
+		var b rune
+		if j < 0 {
+			if carry == '0' {
+				break
+			}
+			b = '0'
+		} else {
+			b = n2[j]
+		}
+		n1[i], carry = add(n1[i], b, carry)
+	}
+	if carry != '0' {
+		return "1" + string(n1)
+	}
+	return string(n1)
+}
+
+func add(a, b, c rune) (rune, rune) {
+	num := a + b + c - 144 // 3 * '0'
+	return num%10 + '0', num/10 + '0'
+}

+ 13 - 0
easy/434.number-of-segments-in-a-string.go

@@ -0,0 +1,13 @@
+func countSegments(s string) (cnt int) {
+	isSpace := true
+	for _, r := range s {
+		if r != ' ' && isSpace {
+			cnt++
+			isSpace = false
+		}
+		if r == ' ' {
+			isSpace = true
+		}
+	}
+	return
+}

+ 11 - 0
easy/437.path-sum-iii.go

@@ -0,0 +1,11 @@
+/**
+ * Definition for a binary tree node.
+ * type TreeNode struct {
+ *     Val int
+ *     Left *TreeNode
+ *     Right *TreeNode
+ * }
+ */
+func pathSum(root *TreeNode, sum int) int {
+    
+}