06 2014 档案
摘要:DFS 深度优先BFS 广度优先DFS或者BFS都是在联通区域内遍历节点的方法用在二叉树上DFS有preOreder,inOrder,postOrder,BFS就是层次遍历。在二叉树上的节点,只有两个选择,left 和right,即,对于每一个节点,in 有1个, out 有两个,有向图在矩阵的节点...
阅读全文
摘要:Given a 2D board containing'X'and'O', capture all regions surrounded by'X'.A region is captured by flipping all'O's into'X's in that surrounded region...
阅读全文
摘要:Given two binary strings, return their sum (also a binary string).For example,a ="11"b ="1"Return"100". 1 class Solution 2 { 3 public: 4 ...
阅读全文
摘要:Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at...
阅读全文
摘要:Given an array of integers, every element appearsthreetimes except for one. Find that single one.Note:Your algorithm should have a linear runtime comp...
阅读全文
摘要:Given an array of integers, every element appearstwiceexcept for one. Find that single one.Note:Your algorithm should have a linear runtime complexity...
阅读全文
摘要:Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.方法:在inorder中寻找...
阅读全文
摘要:Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.方法一:最先想到的就是递归,注...
阅读全文
摘要:Write an efficient algorithm that searches for a value in anmxnmatrix. This matrix has the following properties:Integers in each row are sorted from l...
阅读全文
摘要:Given an array withnobjects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, wh...
阅读全文
摘要:Givennnon-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.Fo...
阅读全文
摘要:Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character'.'.You may assume that there will be on...
阅读全文
摘要:Determine if a Sudoku is valid, according to:Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with ...
阅读全文
摘要:There areNchildren standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requi...
阅读全文
摘要:The set[1,2,3,…,n]contains a total ofn! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie...
阅读全文
摘要:http://stackoverflow.com/questions/200384/constant-amortized-time分摊常量时间:Amortised time explained in simple terms:If you do an operation say a million ...
阅读全文
摘要:当所给问题是从n个元素的集合S中找出满足某种性质的子集时,解空间为子集树。例如:0-1背包问题当所给问题是从n个元素的集合S中找出满足某种性质的排列时,解空间为排列树。例如:旅行售货员问题回溯法搜索子集树算法描述为:void backtrack(int t){ if(t>n) output...
阅读全文
摘要:概念全排列的生成算法有很多种,有递归遍例,也有循环移位法等等。C++/STL中定义的next_permutation和prev_permutation函数则是非常灵活且高效的一种方法,它被广泛的应用于为指定序列生成不同的排列。本文将详细的介绍prev_permutation函数的内部算法。按照STL...
阅读全文
摘要:Reverse a linked list from positionmton. Do it in-place and in one-pass.For example:Given1->2->3->4->5->NULL,m= 2 andn= 4,return1->4->3->2->5->NULL.No...
阅读全文
摘要:Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the ord...
阅读全文
摘要:STL中的每个算法都非常精妙, ForwardIterlower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, last)中的第一个大于等于值val的位置。 ForwardIter uppe...
阅读全文
摘要:Given an arraySofnintegers, are there elementsa,b,c, anddinSsuch thata+b+c+d= target? Find all unique quadruplets in the array which gives the sum of ...
阅读全文
摘要:Given an arraySofnintegers, find three integers inSsuch that the sum is closest to a given number, target. Return the sum of the three integers. You m...
阅读全文
摘要:Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all uniquetriplets in the array which gives the sum of z...
阅读全文
摘要:Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two nu...
阅读全文
摘要:1.map, multimap, set, multisetg++ 中 map, multimap, set, multiset 由红黑树实现map: bits/stl_map.hmultimap: bits/stl_multimap.hset: bits/stl_set.hmultiset: bi...
阅读全文
摘要:There areNgas stations along a circular route, where the amount of gas at stationiisgas[i].You have a car with an unlimited gas tank and it costscost[...
阅读全文
摘要:Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given[100, 4, 200, 1, 3, 2],The longest ...
阅读全文
摘要:There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be ...
阅读全文
摘要:Follow up for "Search in Rotated Sorted Array":What ifduplicatesare allowed?Would this affect the run-time complexity? How and why?Write a function to...
阅读全文
摘要:二分 : 判断条件 当a[left] = A[low]) ,low~mid 二分,mid~high 递归第二种 / / / / / / 条件:(A[mid] A[mid])12 return binarySearch(A,...
阅读全文
摘要:Given an unsorted integer array, find the first missing positive integer.For example, Given [1,2,0] return 3, and [3,4,-1,1] return 2.Your algorithm s...
阅读全文
摘要:本文参考基数排序http://hxraid.iteye.com/blog/648532桶排序 http://hxraid.iteye.com/blog/647759基于比较的排序http://hxraid.iteye.com/blog/646760
阅读全文
摘要:Follow up for ”Remove Duplicates”: What if duplicates are allowed at most twice?For example, Given sorted array A = [1,1,1,2,2,3],Your function should...
阅读全文
摘要:Given a sorted array, remove the duplicates in place such that each element appear only onceand return the new length.Do not allocate extra space for ...
阅读全文
摘要:1 迭代,重要关系 p->right = s.top(); 1 class flat{ 2 public: 3 void flatten(TreeNode* root) { 4 if (root == NULL) return; 5 ...
阅读全文
摘要:Given a binary tree, return thebottom-up level ordertraversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For exa...
阅读全文
摘要:Given a binary tree, return thelevel ordertraversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree{3,9,2...
阅读全文
摘要: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 le...
阅读全文
摘要:Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest le...
阅读全文
摘要:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ ...
阅读全文
摘要:1最容易想到的递归调用 1 bool isSameTree(TreeNode *r1, TreeNode *r2) 2 { 3 if(r1 == NULL && r2 == NULL) return true; //终止条件 4 if(r1 == NULL || r2 == NULL...
阅读全文
摘要:前中后遍历 递归版 1 /* Recursive solution */ 2 class Solution { 3 public: 4 vector preorderTraversal(TreeNode *root) { 5 6 vector resul...
阅读全文
摘要:题目的关键是要让新链表和原有链表发送关联,可以通过这种关联来设置新链表的random pointer思路:将新链表的元素插入到原有链表元素的后面,如下图所示,就可以根据原有链表的radom->next 就是新链表的random指针所以分3步骤:1 新元素插入2 设置新链表的random3 拆分大链表...
阅读全文
摘要:第一种方法,暴力求解,从当前向左右两个方向扫描比自己小的,然后计算面积,时间复杂度O(n^2)code如下,但是在LeetCode上回超时。 1 class Solution { 2 public: 3 int largestRectangleArea(vector &height) { 4...
阅读全文
摘要:第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以spacecomplexity 是O(n) 1 // 使用栈,时间复杂度 O(n),空间复杂度 O(n) 2 class Solution { 3 publ...
阅读全文