Golang每日一练(leetDay0090) 运算优先级、有效字母异位词

目录

241. 为运算表达式设计优先级 Different Ways to Add Parentheses  

242. 有效的字母异位词 Valid Anagram  

每日一练刷题专栏 

Rust每日一练 专栏

Golang每日一练 专栏

Python每日一练 专栏

C/C++每日一练 专栏

Java每日一练 专栏


241. 为运算表达式设计优先级 Different Ways to Add Parentheses

给你一个由数字和运算符组成的字符串 expression ,按不同优先级组合数字和运算符,计算并返回所有可能组合的结果。你可以 按任意顺序 返回答案。

示例 1:

输入:expression = "2-1-1"
输出:[0,2]
解释:
((2-1)-1) = 0 
(2-(1-1)) = 2

示例 2:

输入:expression = "2*3-4*5"
输出:[-34,-14,-10,-10,10]
解释:
(2*(3-(4*5))) = -34 
((2*3)-(4*5)) = -14 
((2*(3-4))*5) = -10 
(2*((3-4)*5)) = -10 
(((2*3)-4)*5) = 10

提示:

  • 1 <= expression.length <= 20
  • expression 由数字和算符 '+''-' 和 '*' 组成。
  • 输入表达式中的所有整数值在范围 [0, 99] 

代码1:

package main

import (
	"fmt"
	"strconv"
)

func diffWaysToCompute(expression string) []int {
	var res []int
	for i := 0; i < len(expression); i++ {
		if expression[i] == '+' || expression[i] == '-' || expression[i] == '*' {
			leftRes := diffWaysToCompute(expression[:i])
			rightRes := diffWaysToCompute(expression[i+1:])
			for _, a := range leftRes {
				for _, b := range rightRes {
					switch expression[i] {
					case '+':
						res = append(res, a+b)
					case '-':
						res = append(res, a-b)
					case '*':
						res = append(res, a*b)
					}
				}
			}
		}
	}
	if len(res) == 0 {
		num, _ := strconv.Atoi(expression)
		res = append(res, num)
	}
	return res
}

func main() {
	expression := "2-1-1"
	fmt.Println(diffWaysToCompute(expression))
	expression = "2*3-4*5"
	fmt.Println(diffWaysToCompute(expression))
}

输出:

[2 0]
[-34 -10 -14 -10 10]

代码2:

package main

import (
	"fmt"
	"strconv"
)

func diffWaysToCompute(expression string) []int {
    memo := make(map[string][]int)
    var dfs func(string) []int
    dfs = func(exp string) []int {
        if res, ok := memo[exp]; ok {
            return res
        }
        var res []int
        for i := 0; i < len(exp); i++ {
            if exp[i] == '+' || exp[i] == '-' || exp[i] == '*' {
                leftRes := dfs(exp[:i])
                rightRes := dfs(exp[i+1:])
                for _, a := range leftRes {
                    for _, b := range rightRes {
                        switch exp[i] {
                        case '+':
                            res = append(res, a+b)
                        case '-':
                            res = append(res, a-b)
                        case '*':
                            res = append(res, a*b)
                        }
                    }
                }
            }
        }
        if len(res) == 0 {
            num, _ := strconv.Atoi(exp)
            res = append(res, num)
        }
        memo[exp] = res
        return res
    }
    return dfs(expression)
}

func main() {
	expression := "2-1-1"
	fmt.Println(diffWaysToCompute(expression))
	expression = "2*3-4*5"
	fmt.Println(diffWaysToCompute(expression))
}

输出:

[2 0]
[-34 -10 -14 -10 10]

代码3:

package main

import "fmt"

func diffWaysToCompute(expression string) []int {
	nums := make([]int, 0) // 记录数字
	ops := make([]byte, 0) // 记录运算符
	num := 0
	for i := 0; i < len(expression); i++ {
		if expression[i] == '+' || expression[i] == '-' || expression[i] == '*' {
			ops = append(ops, expression[i])
			nums = append(nums, num)
			num = 0
		} else {
			num = num*10 + int(expression[i]-'0')
		}
	}
	nums = append(nums, num)
	n := len(nums)
	dp := make([][][]int, n)
	for i := 0; i < n; i++ {
		dp[i] = make([][]int, n)
		for j := 0; j < n; j++ {
			dp[i][j] = make([]int, 0)
		}
	}
	for i := 0; i < n; i++ {
		dp[i][i] = append(dp[i][i], nums[i])
	}
	for len := 2; len <= n; len++ {
		for i := 0; i <= n-len; i++ {
			j := i + len - 1
			for k := i; k < j; k++ {
				leftRes := dp[i][k]
				rightRes := dp[k+1][j]
				for _, a := range leftRes {
					for _, b := range rightRes {
						switch ops[k] {
						case '+':
							dp[i][j] = append(dp[i][j], a+b)
						case '-':
							dp[i][j] = append(dp[i][j], a-b)
						case '*':
							dp[i][j] = append(dp[i][j], a*b)
						}
					}
				}
			}
		}
	}
	return dp[0][n-1]
}

func main() {
	expression := "2-1-1"
	fmt.Println(diffWaysToCompute(expression))
	expression = "2*3-4*5"
	fmt.Println(diffWaysToCompute(expression))
}

输出:

[2 0]
[-34 -10 -14 -10 10]


242. 有效的字母异位词 Valid Anagram

给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。

注意:若 s 和 t 中每个字符出现的次数都相同,则称 s 和 t 互为字母异位词。

示例 1:

输入: s = "anagram", t = "nagaram"
输出: true

示例 2:

输入: s = "rat", t = "car"
输出: false

提示:

  • 1 <= s.length, t.length <= 5 * 10^4
  • s 和 t 仅包含小写字母

进阶: 如果输入字符串包含 unicode 字符怎么办?你能否调整你的解法来应对这种情况?

代码1:

package main

import "fmt"

func isAnagram(s string, t string) bool {
	if len(s) != len(t) {
		return false
	}
	table := make(map[rune]int, 26)
	for _, c := range s {
		table[c]++
	}
	for _, c := range t {
		table[c]--
		if table[c] < 0 {
			return false
		}
	}
	return true
}

func main() {
	s := "anagram"
	t := "nagaram"
	fmt.Println(isAnagram(s, t))
	s = "rat"
	t = "car"
	fmt.Println(isAnagram(s, t))
}

代码2:

package main

import "fmt"

func isAnagram(s string, t string) bool {
	if len(s) != len(t) {
		return false
	}
	table := [26]int{}
	for i := 0; i < len(s); i++ {
		table[s[i]-'a']++
	}
	for i := 0; i < len(t); i++ {
		table[t[i]-'a']--
		if table[t[i]-'a'] < 0 {
			return false
		}
	}
	return true
}

func main() {
	s := "anagram"
	t := "nagaram"
	fmt.Println(isAnagram(s, t))
	s = "rat"
	t = "car"
	fmt.Println(isAnagram(s, t))
}

 代码3:

package main

import "fmt"

import "sort"

type byteSlice []byte

func (b byteSlice) Len() int { return len(b) }

func (b byteSlice) Less(i, j int) bool { return b[i] < b[j] }

func (b byteSlice) Swap(i, j int) { b[i], b[j] = b[j], b[i] }

func isAnagram(s string, t string) bool {
    if len(s) != len(t) {
        return false
    }
    sArr := []byte(s)
    tArr := []byte(t)
    sort.Sort(byteSlice(sArr))
    sort.Sort(byteSlice(tArr))
    for i := 0; i < len(sArr); i++ {
        if sArr[i] != tArr[i] {
            return false
        }
    }
    return true
}

func main() {
	s := "anagram"
	t := "nagaram"
	fmt.Println(isAnagram(s, t))
	s = "rat"
	t = "car"
	fmt.Println(isAnagram(s, t))
}

输出:

true
false


每日一练刷题专栏 

持续,努力奋斗做强刷题搬运工!

点赞,你的认可是我坚持的动力! 

 收藏,你的青睐是我努力的方向! 

评论,你的意见是我进步的财富!  

 主页:https://hannyang.blog.csdn.net/ 

Rust每日一练 专栏

(2023.5.16~)更新中...

Golang每日一练 专栏

(2023.3.11~)更新中...

Python每日一练 专栏

(2023.2.18~2023.5.18)暂停更

C/C++每日一练 专栏

(2023.2.18~2023.5.18)暂停更

Java每日一练 专栏

(2023.3.11~2023.5.18)暂停更

你可能感兴趣的:(#,Go,Leetcode,刷题专栏,golang,leetcode)