6 커밋 b13b4c54b2 ... f5ed394806

작성자 SHA1 메시지 날짜
  tangs f5ed394806 add z第一个只出现一次的字符 2 년 전
  tangs 7c0b018183 add 旋转数组的最小数字 2 년 전
  tangs baa2c66881 add 0~n-1中缺失的数字 2 년 전
  tangs a11b61fe26 add 在排序数组中查找数字 I 2 년 전
  tangs 4c5601e224 add 数组中重复的数字 2 년 전
  tangs 17fde99dfd add 左旋转字符串 2 년 전

+ 48 - 0
leetcode/剑指offer/0~n-1中缺失的数字/main.go

@@ -0,0 +1,48 @@
+package main
+
+import (
+	"bufio"
+	"fmt"
+	"os"
+	"strconv"
+	"strings"
+)
+
+func main() {
+	var input = bufio.NewScanner(os.Stdin)
+	var output int
+	var digits []int
+	for input.Scan() {
+		line := input.Text()
+
+		digits = []int{}
+		values := strings.Split(line, " ")
+
+		for _, v := range values {
+			d, _ := strconv.Atoi(v)
+			digits = append(digits, d)
+		}
+
+		output = missingNumber(digits)
+		fmt.Println(output)
+	}
+}
+
+func missingNumber(nums []int) int {
+	var length = len(nums)
+	if length-1 == nums[length-1] {
+		return length
+	}
+	var left, right = 0, len(nums) - 1
+	var mid int
+	for left < right {
+		mid = (left + right) / 2
+		if nums[mid] > mid {
+			right = mid
+		} else {
+			left = mid + 1
+		}
+	}
+
+	return left
+}

+ 45 - 0
leetcode/剑指offer/在排序数组中查找数字 I/main.go

@@ -0,0 +1,45 @@
+package main
+
+import (
+	"bufio"
+	"fmt"
+	"os"
+	"strconv"
+	"strings"
+)
+
+func main() {
+	input := bufio.NewScanner(os.Stdin)
+	var output int
+	var digits []int
+	var target int
+	for input.Scan() {
+		line := input.Text()
+
+		digits = []int{}
+		values := strings.Split(line, " ")
+		length := len(values)
+
+		for index, v := range values {
+			d, _ := strconv.Atoi(v)
+			if index == length-1 {
+				target = d
+				break
+			}
+			digits = append(digits, d)
+		}
+
+		output = search(digits, target)
+		fmt.Println(output)
+	}
+}
+func search(nums []int, target int) int {
+	var count int
+	for _, v := range nums {
+		if v == target {
+			count++
+		}
+	}
+
+	return count
+}

+ 72 - 0
leetcode/剑指offer/在排序数组中查找数字 I/main_optimize.go

@@ -0,0 +1,72 @@
+package main
+
+import (
+	"bufio"
+	"fmt"
+	"os"
+	"strconv"
+	"strings"
+)
+
+func main() {
+	input := bufio.NewScanner(os.Stdin)
+	var output int
+	var digits []int
+	var target int
+	for input.Scan() {
+		line := input.Text()
+
+		digits = []int{}
+		values := strings.Split(line, " ")
+		length := len(values)
+
+		for index, v := range values {
+			d, _ := strconv.Atoi(v)
+			if index == length-1 {
+				target = d
+				break
+			}
+			digits = append(digits, d)
+		}
+
+		fmt.Println("start ---", digits, target)
+		output = search(digits, target)
+		fmt.Println(output)
+	}
+}
+func search(nums []int, target int) int {
+	if len(nums) == 0 {
+		return 0
+	}
+	var leftIdx = binarySearchLeft(nums, target)
+	var rightIdx = binarySearchLeft(nums, target+1)
+
+	if nums[leftIdx] != target {
+		return 0
+	}
+	if leftIdx == rightIdx {
+		return 1
+	}
+	if nums[rightIdx] == target {
+		rightIdx += 1
+	}
+	return rightIdx - leftIdx
+}
+
+// left最后都会和right相等,nums[right]永远大于等于target。
+// 就会使得nums[left]永远小于等于target.for结束的时候left和right相等,
+// 所以只需要判断nums[left] ?= target, 就能判断nums中是否存在target
+func binarySearchLeft(nums []int, target int) int {
+	var left, right = 0, len(nums) - 1
+	var mid int
+	for left < right {
+		mid = (left + right) / 2
+		if nums[mid] >= target {
+			right = mid
+		} else {
+			left = mid + 1
+		}
+	}
+
+	return left
+}

+ 31 - 0
leetcode/剑指offer/左旋转字符串/main.go

@@ -0,0 +1,31 @@
+package main
+
+import (
+	"bufio"
+	"fmt"
+	"os"
+	"strconv"
+	"strings"
+)
+
+func main() {
+	input := bufio.NewScanner(os.Stdin)
+	var output string
+	for input.Scan() {
+		line := input.Text()
+
+		values := strings.Split(line, " ")
+		num, _ := strconv.Atoi(values[1])
+
+		output = reverseLeftWords(values[0], num)
+		fmt.Println(output)
+	}
+}
+
+func reverseLeftWords(s string, n int) string {
+	if len(s) <= n {
+		return s
+	}
+	var prefix = s[0:n]
+	return s[n:] + prefix
+}

+ 41 - 0
leetcode/剑指offer/数组中重复的数字/main.go

@@ -0,0 +1,41 @@
+package main
+
+import (
+	"bufio"
+	"fmt"
+	"os"
+	"strconv"
+	"strings"
+)
+
+func main() {
+	input := bufio.NewScanner(os.Stdin)
+	var output int
+	var digits []int
+	for input.Scan() {
+		line := input.Text()
+
+		digits = []int{}
+		values := strings.Split(line, " ")
+		for _, v := range values {
+			d, _ := strconv.Atoi(v)
+			digits = append(digits, d)
+		}
+
+		output = findRepeatNumber(digits)
+		fmt.Println(output)
+	}
+}
+
+func findRepeatNumber(nums []int) int {
+	var record = map[int]bool{}
+	for _, v := range nums {
+		if _, ok := record[v]; !ok {
+			record[v] = true
+			continue
+		}
+		return v
+	}
+
+	return 0
+}

+ 46 - 0
leetcode/剑指offer/旋转数组的最小数字/main.go

@@ -0,0 +1,46 @@
+package main
+
+import (
+	"bufio"
+	"fmt"
+	"os"
+	"strconv"
+	"strings"
+)
+
+func main() {
+	var input = bufio.NewScanner(os.Stdin)
+	var output int
+	var digits []int
+	for input.Scan() {
+		line := input.Text()
+
+		digits = []int{}
+		values := strings.Split(line, " ")
+
+		for _, v := range values {
+			d, _ := strconv.Atoi(v)
+			digits = append(digits, d)
+		}
+
+		output = minArray(digits)
+		fmt.Println(output)
+	}
+}
+
+func minArray(numbers []int) int {
+	var left, right = 0, len(numbers) - 1
+	var mid int
+	for left < right {
+		mid = (left + right) / 2
+		if numbers[right] > numbers[mid] {
+			right = mid
+		} else if numbers[right] < numbers[mid] {
+			left = mid + 1
+		} else {
+			right--
+		}
+	}
+
+	return numbers[left]
+}

+ 34 - 0
leetcode/剑指offer/第一个只出现一次的字符/main.go

@@ -0,0 +1,34 @@
+package main
+
+import (
+	"bufio"
+	"fmt"
+	"os"
+)
+
+func main() {
+	var input = bufio.NewScanner(os.Stdin)
+	var output byte
+	for input.Scan() {
+		line := input.Text()
+
+		output = firstUniqChar(line)
+		fmt.Println(string(output))
+	}
+}
+
+func firstUniqChar(s string) byte {
+	var record = [26]int{}
+	for i := 0; i < len(s); i++ {
+		var key = s[i] - 'a'
+		record[key]++
+	}
+	for i := 0; i < len(s); i++ {
+		var key = s[i] - 'a'
+		if record[key] == 1 {
+			return s[i]
+		}
+	}
+
+	return ' '
+}