面试笔试杂项积累-leetcode 331-335

至此2016-2-15,全部333道题除去锁题和不想做的= =。就都做完了。。。

331.331-Verify Preorder Serialization of a Binary Tree-Difficulty: Medium

One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #.

     _9_
    /   \
   3     2
  / \   / \
 4   1  #  6
/ \ / \   / \
# # # #   # #

For example, the above binary tree can be serialized to the string "9,3,4,#,#,1,#,#,2,#,6,#,#", where # represents a null node.

Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree. Find an algorithm without reconstructing the tree.

Each comma separated value in the string must be either an integer or a character '#' representing null pointer.

You may assume that the input format is always valid, for example it could never contain two consecutive commas such as "1,,3".

Example 1:
"9,3,4,#,#,1,#,#,2,#,6,#,#"
Return true

Example 2:
"1,#"
Return false

Example 3:
"9,#,#,1"
Return false

思路

判断先序遍历能否组成一棵树

运用正则表达式,然后一点一点“砍叶”,因为不全的地方包括左右子节点都会用#代替,那么1-9,#,#就是一个叶节点,我们把尾端每个叶节点都用#代替,下面一层叶节点就没了,反复几次,知道没有1-9,#,#叶节点了,如果最后剩的是#空的根,就说明节点全部砍光,就是一个二叉树,否则不是

public class Solution {
    public bool IsValidSerialization(string preorder) {
        var t = String.Empty;
        preorder = System.Text.RegularExpressions.Regex.Replace(preorder, "[0-9]+", "0");
        while (preorder != t)
        {
            t = preorder;
            preorder = preorder.Replace("0,#,#", "#");
        }
        return preorder == "#";
    }
}

332.332-Reconstruct Itinerary-Difficulty: Medium

Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK. Thus, the itinerary must begin with JFK.

Note:

  1. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string. For example, the itinerary ["JFK", "LGA"] has a smaller lexical order than ["JFK", "LGB"].
  2. All airports are represented by three capital letters (IATA code).
  3. You may assume all tickets form at least one valid itinerary.

Example 1:
tickets = [["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]]
Return ["JFK", "MUC", "LHR", "SFO", "SJC"].

Example 2:
tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
Return ["JFK","ATL","JFK","SFO","ATL","SFO"].
Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"]. But it is larger in lexical order.

思路

最简路径

博主开始的代码可以求出最简,但是顺序不对,难道这个不是有多种可能性的吗。。。不知道原因
参考:

https://leetcode.com/discuss/86271/c%23-recursive-solution


public class Solution {
    private IDictionary<string, List<string>> m_routes;
    private int m_count;

    public IList<string> FindItinerary(string[,] tickets)
    {
        m_routes = new Dictionary<string, List<string>>();
        m_count = tickets.GetLength(0);
        for (int i = 0; i < m_count; i++)
        {
            var s = tickets[i, 0];
            var d = tickets[i, 1];

            if (!m_routes.ContainsKey(s))
                m_routes.Add(s, new List<string>());

            m_routes[s].Add(d);
        }

        foreach (var list in m_routes.Values)
            list.Sort();

        return GetPath("JFK");
    }

    private IList<string> GetPath(string s)
    {
        if (m_routes.ContainsKey(s) && m_routes[s].Count > 0)
        {
            var children = m_routes[s];
            for (int i = 0; i < children.Count; i++)
            {
                var child = children[i];
                children.RemoveAt(i);
                m_count--;

                var best = GetPath(child);
                children.Insert(i, child);
                m_count++;

                if (best != null)
                {
                    best.Insert(0, s);
                    return best;
                }
            }

            return null;
        }

        if (m_count == 0)
            return new List<string> {s};

        return null;
    }
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值