Prechádzať zdrojové kódy

add 左旋转字符串

tangs 2 rokov pred
rodič
commit
17fde99dfd

+ 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
+}