Leetcode第十周周赛Smarking Algorithm Contest

来源:互联网 发布:hadoop处理结构化数据 编辑:程序博客网 时间:2024/05/16 10:31

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   1Return 3. The paths that sum to 8 are:1.  5 -> 32.  5 -> 2 -> 13. -3 -> 11

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

var res intfunc 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: 2Output:10Explanation: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 intvar num,ans [20]intfunc 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
原创粉丝点击