自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(48)
  • 收藏
  • 关注

原创 Linked List Cycle

Given a linked list, determine if it has a cycle in it.public class Solution { /** * @param head: The first node of linked list. * @return: True if it has a cycle, or false */ pu

2017-03-03 13:52:43 328

原创 Copy List with Random Pointer

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.public class Solution { /** *

2017-03-03 08:57:58 239

原创 Insert into a Cyclic Sorted List

Given a node from a cyclic linked list which has been sorted, write a function to insert a value into the list such that it remains a cyclic sorted list. The given node can be any single node in the li

2017-03-02 21:19:02 465

原创 Merge Two Sorted Lists

Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list should be made by splicing together the nodes of the two lists and sorted in ascending order.public cla

2017-03-02 21:16:15 501

原创 Insert into a Cyclic Sorted List

Given a node from a cyclic linked list which has been sorted, write a function to insert a value into the list such that it remains a cyclic sorted list. The given node can be any single node in the li

2017-03-02 21:15:53 610

原创 Insert into a Cyclic Sorted List

Given a node from a cyclic linked list which has been sorted, write a function to insert a value into the list such that it remains a cyclic sorted list. The given node can be any single node in the li

2017-03-02 21:15:21 453

原创 Permutations II

Given a list of numbers with duplicate number in it. Find all unique permutations.class Solution { /** * @param nums: A list of integers. * @return: A list of unique permutations. */

2017-02-28 22:34:40 205

原创 Permutations

Given a list of numbers, return all possible permutations.class Solution { /** * @param nums: A list of integers. * @return: A list of permutations. */ public List<List<Integer>>

2017-02-28 22:33:54 385

原创 Palindrome Partitioning

Given a string s, partition s such that every substring of the partition is a palindrome.Return all possible palindrome partitioning of s.public class Solution { /** * @param s: A string

2017-02-27 21:55:22 197

原创 Subsets II

Given a list of numbers that may has duplicate numbers, return all possible subsetsclass Solution { /** * @param nums: A set of numbers. * @return: A list of lists. All valid subsets.

2017-02-27 21:54:45 180

原创 Combination Sum II

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may only be used once in the combination.pu

2017-02-27 21:52:02 217

原创 Combination Sum

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C unlimited number of t

2017-02-27 21:49:50 194

原创 Combination Sum

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C unlimited number of t

2017-02-27 21:47:38 274

原创 Build Post Office II

Given a 2D grid, each cell is either a wall 2, an house 1 or empty 0 (the number zero, one, two), find a place to build a post office so that the sum of the distance from the post office to all the hou

2017-02-26 22:58:47 970

原创 Knight Shortest Path

Given a knight in a chessboard (a binary matrix with 0 as empty and 1 as barrier) with a source position, find the shortest path to a destination position, return the length of the route. Return -1 i

2017-02-26 17:12:11 956

原创 Zombie in Matrix

Given a 2D grid, each cell is either a wall 2, a zombie 1 or people 0 (the number zero, one, two).Zombies can turn the nearest people(up/down/left/right) into zombies every day, but can not through wal

2017-02-26 16:21:56 338

原创 岛屿的个数

给一个01矩阵,求不同的岛屿的个数。0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛。我们只考虑上下左右为相邻。class Coordinate { int x; int y; public Coordinate(int x, int y) { this.x = x; this.y = y; }}public class

2017-02-26 15:23:14 246

原创 拓扑排序

给定一个有向图,图节点的拓扑排序被定义为:对于每条有向边A–> B,则A必须排在B之前   拓扑排序的第一个节点可以是任何在图中没有其他节点指向它的节点   找到给定图的任一拓扑排序/** * Definition for Directed graph. * class DirectedGraphNode { * int label; * ArrayList<Direct

2017-02-26 11:38:09 184

原创 Search Graph Nodes

Given a undirected graph, a node and a target, return the nearest node to given node which value of it is target, return NULL if you can’t find.There is a mapping store the nodes’ values in the given p

2017-02-26 11:37:13 348

原创 安排课程

你需要去上n门九章的课才能获得offer,这些课被标号为 0 到 n-1 。 有一些课程需要“前置课程”,比如如果你要上课程0,你需要先学课程1,我们用一个匹配来表示他们: [0,1]给你课程的总数量和一些前置课程的需求,返回你为了学完所有课程所安排的学习顺序。可能会有多个正确的顺序,你只要返回一种就可以了。如果不可能完成所有课程,返回一个空数组。样例 给定 n = 2, prerequisit

2017-02-26 11:36:02 453

原创 Clone Graph

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.How we serialize an undirected graph:Nodes are labeled uniquely.We use # as a separator for each node, and

2017-02-25 10:27:07 182

原创 Validate Binary Search Tree

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

2017-02-24 22:40:58 169

原创 Lowest Common Ancestor III

Given the root and two nodes in a Binary Tree. Find the lowest common ancestor(LCA) of the two nodes. The lowest common ancestor is the node with largest depth which is the ancestor of both nodes. Re

2017-02-24 22:37:32 309

原创 Binary Tree Longest Consecutive Sequence

Given a binary tree, find the length of the longest consecutive sequence path.The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections

2017-02-24 22:33:30 263

原创 Graph Valid Tree

Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges make up a valid tree.分析: 首先明确树的定义:全连通图(所有节点连通)无回路 (

2017-02-24 22:31:01 226

原创 Binary Tree Serialization

Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a file is called ‘serialization’ and reading back from the file to reconstruct the exact same binary t

2017-02-24 22:27:50 239

原创 Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).分析:二叉树的层序遍历,使用广度优先搜索(BFS),BFS要使用的数据结构为队列。public ArrayList<ArrayList<Integer>> levelO

2017-02-24 22:26:48 193

原创 Binary Tree Path Sum

Given a binary tree, find all paths that sum of the nodes in the path equals to a given number target.A valid path is from root node to any of the leaf nodes.寻找所有的路径,DFS解决之。public class Solution {

2017-02-09 18:06:25 195

原创 Flatten Binary Tree to Linked List

Flatten a binary tree to a fake “linked list” in pre-order traversal.Here we use the right pointer in TreeNode as the next pointer in ListNode.public class Solution { /** * @param root: a Tree

2017-02-09 11:35:59 183

原创 Balanced Binary Tree

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 depth of the two subtrees of every node never differ by

2017-02-09 10:35:52 464

原创 Subtree with Maximum Average

Given a binary tree, find the subtree with maximum average. Return the root of the subtree.此题需要返回的值不止一个,因此使用了一个包含返回值的内部类,通过ResultType来返回参数。高阶版的traverse + divide conquer,需要好好理解一下。public class Solution {

2017-02-07 23:18:10 1238

原创 Minimum Subtree

Given a binary tree, find the subtree with minimum sum. Return the root of the subtree.traverse + divide & conquerpublic class Solution { /** * @param root the root of binary tree * @ret

2017-02-07 17:41:41 525

原创 Binary Tree Paths

Given a binary tree, return all root-to-leaf paths. 分治法: 在此题中,conquer分为两种情况,一种为处理中间节点,一种为处理叶子节点。难点在于将两种情况分开处理。public class Solution { /** * @param root the root of the binary tree * @re

2017-02-07 16:57:26 167

原创 Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.可以使用分治法和遍历法来解答。 分治:public class Solution {

2017-02-07 15:42:18 119

原创 二叉树的前序、中序、后序遍历

分别使用traverse、divide & conquer、non-recursion来实现。 traverse使用全局遍历,不需要返回值。 divide & conquer 两步走:first :divide second:conquer non-recursion:借助栈来实现。其中后序遍历与先右后左的前序遍历互为逆序,借助这一性质来简化操作。 traverse:前序遍历public

2017-02-07 15:02:20 201

原创 书籍复印

给出一个数组A包含n个元素,表示n本书以及各自的页数。现在有个k个人复印书籍,每个人只能复印连续一段编号的书,比如A[1],A[2]由第一个人复印,但是不能A[1],A[3]由第一个人复印,求最少需要的时间复印所有书。神奇的二分法:public int copyBooks(int[] pages, int k) { if (pages == null || pages.length

2017-01-25 16:37:49 483

原创 搜索旋转排序数组

假设有一个排序的按未知的旋转轴旋转的数组(比如,0 1 2 4 5 6 7 可能成为4 5 6 7 0 1 2)。给定一个目标值进行搜索,如果在数组中找到目标值返回数组中的索引位置,否则返回-1。你可以假设数组中不存在重复的元素。 分析:特殊的数组搜索某一元素问题,二分法。这一题的难点在于如何寻找二分的判断条件,可以结合中学数学思想,进行分段讨论。以数组首元素进行分段,然后每一段可以再进行一次讨论

2017-01-25 15:15:32 239

原创 第一个错误的代码版本

代码库的版本号是从 1 到 n 的整数。某一天,有人提交了错误版本的代码,因此造成自身及之后版本的代码在单元测试中均出错。请找出第一个错误的版本号。你可以通过 isBadVersion 的接口来判断版本号 version 是否在单元测试中出错,具体接口详情和调用方法请见代码的注释部分。分析:典型的二分查找问题,查找数组中满足某个条件的第一个值的下标问题。public int findFirstBad

2017-01-24 21:52:18 223

原创 寻找峰值

你给出一个整数数组(size为n),其具有以下特点:相邻位置的数字是不同的 A[0] < A[1] 并且 A[n - 2] > A[n - 1] 假定P是峰值的位置则满足A[P] > A[P-1]且A[P] > A[P+1],返回数组中任意一个峰值的位置。注意事项数组可能包含多个峰值,只需找到其中的任何一个即可.分析:在数组中查找某个元素,典型的二分查找问题,此题添加了限制条件,寻找数组中的峰值

2017-01-24 21:44:25 263

原创 寻找旋转数组中的最小值

假设一个旋转排序的数组其起始位置是未知的(比如0 1 2 4 5 6 7 可能变成是4 5 6 7 0 1 2)。你需要找到其中最小的元素。你可以假设数组中不存在重复的元素。分析:寻找数组中某一类特定的值,很容易联想到二分法。本题中数组是旋转的,而且是排序的,还是比较有规律,可以分情况讨论mid的落点情况:mid处于递增序列中,大于前数,小于后数mid处在临界点,即小于两边的数public i

2017-01-24 21:40:52 227

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除