Leetcode第十周周赛Smarking Algorithm Contest

437. Path Sum III

You are given a binary tree in which each node contains an integer value.

Find the number of paths that sum to a given value.

The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).

The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.

root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8

      10
     /  \
    5   -3
   / \    \
  3   2   11
 / \   \
3  -2   1

Return 3. The paths that sum to 8 are:

1.  5 -> 3
2.  5 -> 2 -> 1
3. -3 -> 11

给你个树,求出所有符合路径上的节点的值的和等于给定值这个条件的路径的个数。路径要求是自上而下,但是不要求从根出发,也不要求以叶子节点作为结尾。
挂在这道题上面了。
给个别人的解法:
将路径分为两种走法,每个节点都有两种走法。第一种情况是以这个节点为起点,开始出发。第二种是继承之前的走法接着走下去。

var res int
func go2(root *TreeNode, need int){
    if root == nil{
        return
    }
    if need == 0{
        res ++
    }
    if root.Left != nil{
        go2(root.Left,need-root.Left.Val)
    }
    if root.Right != nil{
        go2(root.Right,need-root.Right.Val)
    }
}
func go1(root * TreeNode, need int){
    if root == nil{
        return
    }
    go2(root,need-root.Val)
    go1(root.Left,need)
    go1(root.Right,need)
}
func pathSum(root *TreeNode, sum int) int {
    res = 0
    go1(root, sum)
    return res
}

438. Find All Anagrams in a String

Given a string s and a non-empty string p, find all the start indices of p’s anagrams in s.

Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100.

The order of output does not matter.
Example 1:

Input:
s: "cbaebabacd" p: "abc"

Output:
[0, 6]

Explanation:
The substring with start index = 0 is "cba", which is an anagram of "abc".
The substring with start index = 6 is "bac", which is an anagram of "abc".

Example 2:

Input:
s: "abab" p: "ab"

Output:
[0, 1, 2]

Explanation:
The substring with start index = 0 is "ab", which is an anagram of "ab".
The substring with start index = 1 is "ba", which is an anagram of "ab".
The substring with start index = 2 is "ab", which is an anagram of "ab".

给定两个字符串s,p,求出p的所有变形(包括自己)在s中的起点。
解法是统计p中的词频,然后统计s中i到i+len(p)中的字符串的词频。相比较,如果相等则说明命中了。

func check(a,b,c [26]int) bool{
    for i:=0;i<26;i++{
        if a[i]-b[i] != c[i]{
            return false
        }
    }
    return true
}

func findAnagrams(s string, p string) []int {
    if len(s)<len(p){
        return []int{}
    }
    ret := []int{}
    start, end, mark := [26]int{},[26]int{},[26]int{}
    for i:=0;i<len(p);i++{
        mark[p[i]-'a'] ++
    }
    for i:=0;i<len(p);i++{
        end[s[i]-'a'] ++
    }
    if check(end,start,mark){
        ret = append(ret,0)
    }
    for i:=len(p);i<len(s);i++{
        start[s[i-len(p)]-'a'] ++
        end[s[i]-'a']++
        if check(end,start,mark){
            ret = append(ret,i-len(p)+1)
        }
    }
    return ret
}

439. Ternary Expression Parser

Given a string representing arbitrarily nested ternary expressions, calculate the result of the expression. You can always assume that the given expression is valid and only consists of digits 0-9, ?, :, T and F (T and F represent True and False respectively).

Note:

  1. The length of the given string is ≤ 10000.
  2. Each number will contain only one digit.
  3. The conditional expressions group right-to-left (as usual in most languages).
  4. The condition will always be either T or F. That is, the condition will never be a digit.
  5. The result of the expression will always evaluate to either a digit 0-9, T or F.
    Example 1:
Input: "T?2:3"
Output: "2"
Explanation: If true, then result is 2; otherwise result is 3.

Example 2:

Input: "F?1:T?4:5"
Output: "4"
Explanation: The conditional expressions group right-to-left. Using parenthesis, it is read/evaluated as:


             "(F ? 1 : (T ? 4 : 5))"                   "(F ? 1 : (T ? 4 : 5))"
          -> "(F ? 1 : 4)"                 or       -> "(T ? 4 : 5)"
          -> "4"                                    -> "4"

就是C语言中常见的?:语句。
这题可以利用栈来做,也可以利用递归来做,应该也可以用正则表达式来做。
下面是递归的做法:

func parseTernary(e string) string {
    if e[0] != 'T' && e[0]!='F'{
        return e
    }
    c1,c2,i := 0,0,1
    for i=1;i<len(e);i++{
        if e[i] == '?'{
            c1 ++
        }else{
            if e[i] == ':'{
                c2 ++
                if c1 == c2{
                    break;
                }
            }
        }
    }
    if i == len(e){
        return e
    }
    if e[0] == 'T'{
        return parseTernary(e[2:i])
    }
    return parseTernary(e[i+1:])
}

440. K-th Smallest in Lexicographical Order

Given integers n and k, find the lexicographically k-th smallest integer in the range from 1 to n.

Note: 1 ≤ k ≤ n ≤ 109.

Example:

Input:
n: 13   k: 2

Output:
10

Explanation:
The lexicographical order is [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9], so the second smallest number is 10.

求字典序的题。和头条的校招面试题一模一样。

var Len,n,m,ret int
var num,ans [20]int

func valueOfBegin(b int ) int{
    cnt :=0
    for i:=b;i<=Len;i++{
        cnt = cnt *10 + num[Len-i+1]
    }
    return cnt
}
func countofNum(a [20]int,anslen int) int{
    k,cnt :=1,0
    for i:=0;i+anslen <Len;i++{
        cnt +=k
        k*=10
    }
    op :=0
    for i:=1;i<=anslen;i++{
        id := Len-i+1
        if num[id]>a[i]{
            op = -1
            break
        }else if num[id] < a[i]{
            op = 1
            break
        }
    }
    if op == 0{
        if anslen == Len{
            return 1
        }
        return valueOfBegin(anslen+1)+1+cnt
    }
    if op == 1{
        return cnt
    }
        k = 1
        for i:=0;i<Len-anslen;i++{
            k*=10

        }
        return cnt +k
}

func dfs(ans [20]int, cur,m int){
    if m == 1{
        ret = 0
        for i:=1;i<=cur;i++{
            ret = ret *10 + ans[i]
        }
        return
    }
    m --
    for i:=0;i<=9;i++{
        ans[cur+1] = i
        cnt := countofNum(ans,cur+1)
        if cnt >=m{
            dfs(ans,cur+1,m)
            return
        }else{
            m -= cnt
        }
    }
}

func findKthNumber(a int, k int) int {
    Len = 0
    n = a
    m = k
    for n>0{
        Len ++ 
        num[Len] = n%10
        n/=10
    }
    for i:=1;i<=9;i++{
        ans[1] = i
        cnt := countofNum(ans,1)
        if cnt >=m{
            dfs(ans,1,m)
            break
        }else{
            m -= cnt
        }
    }
    return ret
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值