- 浏览: 136937 次
最新评论
-
likesky3:
看了数据结构书得知并不是迭代和递归的区别,yb君的写法的效果是 ...
Leetcode - Graph Valid Tree -
likesky3:
迭代和递归的区别吧~
Leetcode - Graph Valid Tree -
qb_2008:
还有一种find写法:int find(int p) { i ...
Leetcode - Graph Valid Tree -
qb_2008:
要看懂这些技巧的代码确实比较困难。我是这么看懂的:1. 明白这 ...
Leetcode - Single Num II -
qb_2008:
public int singleNumber2(int[] ...
Leetcode - Single Num II
文章列表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
[分析] 有了Merge 2 sorted lists的铺垫做这题就容易了,自己写了递归版的method1, 翻看历史记录,有迭代版的和heap sort版本,觉得method1 & 2写得更顺手些。感觉迭代方式挺巧妙的,哈哈
public class Solution {
// Method 1: 递归
public ListNode mergeKLists(Li ...
There are N children standing in a line. Each child is assigned a rating value.
You are giving candies to these children subjected to the following requirements:
Each child must have at least one candy.
Children with a higher rating get more candies than their neighbors.
What is the minimum candies ...
Leetcode - Gas Station
- 博客分类:
- Greedy
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].
You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.
Return the ...
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
For example:
Given array A = [2,3,1,1,4]
The ...
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Determine if you are able to reach the last index.
For example:
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], ret ...
Follow up for problem "Populating Next Right Pointers in Each Node".
What if the given tree could be any binary tree? Would your previous solution still work?
Note:
You may only use constant extra space.
For example,
Given the following binary tree,
1
/ \
2 3
...
Two elements of a binary search tree (BST) are swapped by mistake.
Recover the tree without changing its structure.
Note:
A solution using O(n) space is pretty straight forward. Could you devise a constant space solution?
[分析] 正常的BST,其中序遍历序列是一个递增序列,假设A,B节点互换了位置,则在中序遍历中A比其后元素大,B比前面元素小,可据此性质找出AB节点并调 ...
Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
The left subtree of a node contains only nodes with keys less than the node's key.
The right subtree of a node contains only nodes with keys greater than the node's key.
Both the left and r ...
[分析] Majority Element II这个扩展版可以沿用Majority Element 中的Moore Voting解法,大于 N / 3最多有两个,遍历过程中记录两个候选值。需注意的是,遍历结束后的候选值并不一定符合条件,比如input= [1,1,2],因此需要个检查过程。
[ref]
对Moore Voting算法解析
http://blog.csdn.net/joylnwang/article/details/7081575
public class Solution {
public List<Integer> majorityElement( ...
There are a total of n courses you have to take, labeled from 0 to n - 1.
Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]
Given the total number of courses and a list of prerequisite pairs, return the ordering of ...
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.
OJ's undirected graph serialization:
Nodes are labeled uniquely.
We use # as a separator for each node, and , as a separator for node label and each neighbor of the node.
As an example, consider the seri ...
https://leetcode.com/problems/course-schedule/
[分析] 典型的拓扑排序应用。先根据课程的先修关系构造一个graph,graph中的节点数==课程数,先修关系pair代表graph中的一条边,注意测试case中先修关系有重复,因此仅当创建新边时才更新相应节点的indegree。
public class Solution {
// method 2: 二维数组表示有向图
public boolean canFinish(int numCourses, int[][] prerequisites) {
if ...
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Example 1:
11110
11010
11000
00000
Answer: ...
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:
Only one letter can be changed at a time
Each intermediate word must exist in the dictionary
For example,
Given:
start = "hit"
end = "cog"
dict = [" ...
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformation sequence from beginWord to endWord, such that:
Only one letter can be changed at a time
Each intermediate word must exist in the dictionary
For example,
Given:
start = "hit"
end = "c ...