dengxinyi 6 years ago
parent
commit
729cf5b04b

+ 17 - 0
easy/485.max-consecutive-ones.go

@@ -0,0 +1,17 @@
+func findMaxConsecutiveOnes(nums []int) (max int) {
+	cnt := 0
+	for _, i := range nums {
+		if i == 1 {
+			cnt++
+		} else {
+			if max < cnt {
+				max = cnt
+			}
+			cnt = 0
+		}
+	}
+	if max < cnt {
+		max = cnt
+	}
+	return
+}

+ 12 - 0
easy/492.construct-the-rectangle.go

@@ -0,0 +1,12 @@
+func constructRectangle(area int) []int {
+	sqrt := int(math.Sqrt(float64(area)))
+	if sqrt*sqrt == area {
+		return []int{sqrt, sqrt}
+	}
+	for i := sqrt; i <= area/2; i++ {
+		if area%i == 0 && area/i <= i {
+			return []int{i, area / i}
+		}
+	}
+	return []int{area, 1}
+}

+ 21 - 0
easy/496.next-greater-element-i.go

@@ -0,0 +1,21 @@
+func nextGreaterElement(findNums []int, nums []int) []int {
+	stack := make([]int, 0)
+	next := make(map[int]int)
+	for _, i := range nums {
+		for l := len(stack); l != 0 && stack[l-1] < i; l = len(stack) {
+			top := stack[l-1]
+			stack = stack[:l-1]
+			next[top] = i
+		}
+		stack = append(stack, i)
+	}
+	res := make([]int, len(findNums))
+	for i := range res {
+		if val, ok := next[findNums[i]]; ok {
+			res[i] = val
+		} else {
+			res[i] = -1
+		}
+	}
+	return res
+}

+ 33 - 0
easy/500.keyboard-row.go

@@ -0,0 +1,33 @@
+var rows map[rune]int = map[rune]int{
+	'A': 1, 'S': 1, 'D': 1, 'F': 1, 'G': 1, 'H': 1, 'J': 1, 'K': 1, 'L': 1,
+	'Z': 2, 'X': 2, 'C': 2, 'V': 2, 'B': 2, 'N': 2, 'M': 2,
+}
+
+func findWords(words []string) (res []string) {
+	for i := range words {
+		if isInOneRow(words[i]) {
+			res = append(res, words[i])
+		}
+	}
+	return
+}
+
+func isInOneRow(s string) bool {
+	res := 0
+	for _, r := range s {
+		ch := toUpper(r)
+		if val, ok := rows[ch]; ok {
+			res |= val
+		} else {
+			res |= 4
+		}
+	}
+	return res == 1 || res == 2 || res == 4
+}
+
+func toUpper(r rune) rune {
+	if 'a' <= r {
+		return rune(r - 32)
+	}
+	return r
+}