面试笔试杂项积累-leetcode 331-335
来源:互联网 发布:饮料铺货软件 编辑:程序博客网 时间:2024/06/15 19:56
至此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 fromJFK
. Thus, the itinerary must begin with JFK
.
Note:
- 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"]
. - All airports are represented by three capital letters (IATA code).
- 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; }}
- 面试笔试杂项积累-leetcode 331-335
- 面试笔试杂项积累-leetcode 1-5
- 面试笔试杂项积累-leetcode 6-10
- 面试笔试杂项积累-leetcode 11-15
- 面试笔试杂项积累-leetcode 16-20
- 面试笔试杂项积累-leetcode 21-25
- 面试笔试杂项积累-leetcode 26-30
- 面试笔试杂项积累-leetcode 31-35
- 面试笔试杂项积累-leetcode 36-40
- 面试笔试杂项积累-leetcode 41-45
- 面试笔试杂项积累-leetcode 46-50
- 面试笔试杂项积累-leetcode 51-55
- 面试笔试杂项积累-leetcode 56-60
- 面试笔试杂项积累-leetcode 61-65
- 面试笔试杂项积累-leetcode 66-70
- 面试笔试杂项积累-leetcode 71-75
- 面试笔试杂项积累-leetcode 76-80
- 面试笔试杂项积累-leetcode 81-85
- Ubuntu下curl命令使用
- python连接数据库
- Android的TextView中文字添加删除线,下划线
- iOS 【UIKit-UIPageControl利用delegate定位圆点位置 之 四舍五入小技巧】
- 30行代码实现Javascript中的MVC
- 面试笔试杂项积累-leetcode 331-335
- 阿里2015校招前端方向的一道面试题
- Linux内核编码风格
- notepad++更换添加主题
- 反射实现AOP动态代理
- Android Handler机制
- Git代码管理的使用流程
- Qualcomm Flight--首套四轴飞行器集成开发平台问世
- 欢迎使用CSDN-markdown编辑器