/leetcode-go

leetcode by golang

Primary LanguageGo

leetcode-go

leetcode by golang

[TOC]

剑指offer系列

package main

import (
	"fmt"
	"regexp"
)

func replaceSpace(s string) string {
	reg := regexp.MustCompile(`\s`)
	return reg.ReplaceAllString(s, "%20")
}

func main() {
	s := "We are happy."
	r := replaceSpace(s)
	fmt.Println(r)
}
package main

import "fmt"

func reverseLeftWords(s string, n int) string {
	return s[n:] + s[:n]
}

func main() {

	s := "abcdefg"
	k := 2
	r := reverseLeftWords(s, k)
	fmt.Println(r)
}
#参考的网上的题解

func isNumber(s string) bool {
	s = strings.ToLower(strings.TrimSpace(s))
	if strings.ContainsAny(s, " bopx") {
		return false
	}
	if i := strings.IndexAny(s, "e"); i >= 0 {
		n1, _ := fmt.Sscan(s[:i], new(big.Float), new(string))
		n2, _ := fmt.Sscanf(s[i+1:], "%d%s", new(big.Int), new(string)) // %d 强制以十进制方式读入
		return n1 == 1 && n2 == 1
	}
	n, _ := fmt.Sscan(s, new(big.Float), new(string))
	return n == 1
}
func reversePrint(head *ListNode) []int {
	if head == nil {
		return nil
	}
	return append(reversePrint(head.Next), head.Val)

}
func reverseWords(s string) string {
    s = strings.Trim(s, " ")
    words := strings.Fields(s)
    i, j := 0, len(words) - 1
    for i < j {
        words[i], words[j] = words[j], words[i]
        i++
        j--    
    }
    return strings.Join(words, " ")
}