摘要: 这个算法只是检查能不能找到符合要求的path,但是不保证找到的那一条path是最短的。注意把已经访问过的点设置成3,这样访问过的点就不会被重复访问了。 如果想要找出shortest path的长度,可以用如下的方法: 阅读全文
posted @ 2016-12-23 06:31 lettuan 阅读(188) 评论(0) 推荐(0) 编辑
摘要: Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted f 阅读全文
posted @ 2016-12-23 05:51 lettuan 阅读(271) 评论(0) 推荐(0) 编辑
摘要: Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix. Note that it is th 阅读全文
posted @ 2016-12-23 00:56 lettuan 阅读(526) 评论(0) 推荐(0) 编辑
摘要: You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define a pair (u,v) which consists of one element from th 阅读全文
posted @ 2016-12-22 01:39 lettuan 阅读(290) 评论(0) 推荐(0) 编辑
摘要: Example Given [3,10,1000,-99,4,100] and k = 3. Return [1000, 100, 10]. 解法有以下几种: 1. bubble sort k times. 复杂度O(nk) 2. 使用临时数组。O((n-k)k) 3. sort。O(NLogN) 阅读全文
posted @ 2016-12-21 14:58 lettuan 阅读(140) 评论(0) 推荐(0) 编辑
摘要: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia 阅读全文
posted @ 2016-12-19 12:35 lettuan 阅读(195) 评论(0) 推荐(0) 编辑
摘要: 由于BST的性质,所以右子树或者左子树中Node的值是连续的: 左子树 = [1, i -1], root = i, 右子树 = [i + 1, n]。使用一个递归函数构造这个BST。其中返回值应该是所有的Unique BST的root node。 阅读全文
posted @ 2016-12-19 11:46 lettuan 阅读(217) 评论(0) 推荐(0) 编辑
摘要: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example,Given n = 3, there are a total of 5 unique BST' 阅读全文
posted @ 2016-12-19 11:41 lettuan 阅读(158) 评论(0) 推荐(0) 编辑
摘要: 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 阅读全文
posted @ 2016-12-19 09:59 lettuan 阅读(155) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the dept 阅读全文
posted @ 2016-12-19 09:53 lettuan 阅读(122) 评论(0) 推荐(0) 编辑