20. Valid Parentheses
Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘ and ‘]’, determine if the input string is valid.
The brackets must close in the correct order, “()” and “()[]{}” are all valid but “(]” and “([)]” are not.
解题思路:
括号匹配
|
|
150. Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are +, -, *, /. Each operand may be an integer or another expression.
Examples:
|
|
解题思路:
后缀表达式,遇到数字入栈,遇到标点符号出栈两个数并计算。
注意,计算结果之后,需要重新入栈
|
|
71. Simplify Path
Given an absolute path for a file (Unix-style), simplify it.
Example:
|
|
解题思路:
当遇到 “..”,需要栈的特性,弹出最后加入的元素
返回的字符串需要FIFO,需要队列的特性
使用LinkedList为最佳
|
|
144. Binary Tree Preorder Traversal
二叉树先序遍历
递归版
|
|
迭代版:
|
|
94. Binary Tree Inorder Traversal
二叉树中序遍历
递归版
|
|
迭代版
|
|
145. Binary Tree Postorder Traversal
二叉树后续遍历
递归版
|
|
迭代版:
二叉树先序遍历,中左右
二叉树后续遍历,左右中 == Reverse(中右左)
|
|
341. Flatten Nested List Iterator
Given a nested list of integers, implement an iterator to flatten it.
Each element is either an integer, or a list – whose elements may also be integers or other lists.
Example 1:
|
|
Example 2:
|
|
|
|
return its level order traversal as:
|
|
解题思路:
二叉树层序遍历
|
|
107. Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree [3,9,20,null,null,15,7],
|
|
return its bottom-up level order traversal as:
|
|
解题思路:
与上一道题目是一样的,只是List
|
|
103. Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between).
For example:
Given binary tree [3,9,20,null,null,15,7],
|
|
return its zigzag level order traversal as:
|
|
解题思路:
之字形打印二叉树
|
|
199. Binary Tree Right Side View
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
For example:
Given the following binary tree,
|
|
You should return [1, 3, 4].
解题思路:
层序遍历
找二叉树每层的最后一个节点
|
|
279. Perfect Squares
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, …) which sum to n.
For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9.
解题思路:
图论问题,广度优先搜索问题
寻找满足X = v1 + v2 + v3 + ….最少的v的个数
示例12
|
|
127. Word Ladder
Given two words (beginWord and endWord), and a dictionary’s word list, find the length of shortest transformation sequence from beginWord to endWord, such that:
- Only one letter can be changed at a time.
- Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
Example:
|
|
Note:
- Return 0 if there is no such transformation sequence.
- All words have the same length.
- All words contain only lowercase alphabetic characters.
- You may assume no duplicates in the word list.
- You may assume beginWord and endWord are non-empty and are not the same.
解题思路:
广度优先搜索
|
|
126. Word Ladder II
All conditions are as same as the last question,
unless the return
|
|
解题思路:
深度优先搜索
|
347. Top K Frequent Elements
Given a non-empty array of integers, return the k most frequent elements.
Example:
Given [1,1,1,2,2,3] and k = 2, return [1,2].
Note:
- You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
- Your algorithm’s time complexity must be better than O(n log n), where n is the array’s size.
解题思路:
HashMap统计数字的频率
最小堆,堆顶最小元素被过滤
|
|
23. Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
解题思路:
本来是想着把 所有的节点,依次装入优先队列,然后重新组装成链表
结果是会超时的
下面解法的思路是,最小堆每次只有lists.size()个,每次比较的是每个链表的头节点;
|
|