- 博客(16)
- 收藏
- 关注
原创 <leetcode> Single Number II
Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without u
2014-09-29 11:25:02
402
原创 <leetcode>Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Fin
2014-09-19 04:23:37
393
原创 <leetcode> Gas station
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
2014-09-19 03:33:40
688
原创 <leetcode> Permutations
Given a collection of numbers, return all possible permutations.For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].思路同
2014-09-18 05:44:44
447
原创 <leetcode> 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 numb
2014-09-17 06:53:30
382
原创 <leetcode>Combination
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]
2014-09-17 06:45:10
397
原创 <leetcode> Inorder, Preorder Traversal
非递归的binary tree traversal:Inorder:从根开始,首先yan zhe
2014-09-04 03:24:40
426
原创 <leetcode> Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] ha
2014-09-01 03:45:08
372
原创 <leetcode> Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a link
2014-08-20 23:55:57
360
原创 <leetcode>Remove Duplicates from Sorted List
public ListNode deleteDuplicates(ListNode head) { if(head==null){ return null; } if(head.next==null){ return head; } ListNode start=head
2014-08-19 23:16:04
328
原创 <leetcode> Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL.
2014-08-19 12:42:32
339
原创 判断BinaryTree是否为BinarySearchTree的O(N)算法
判断一个BinaryTree是否为BinarySearchTree 最直接的方法就是对于每个节点,遍历其所有子结点,判断是否左子树节点均小于根,并且右子树节点大于根。其复杂度为O(N^2),并且该方法需要用到非递归遍历来比较每一个节点和根,代码相对复杂。根据BinarySearchTree的定义,根节点永远大于左子树,永远小于右子树。因此对于每个根的左子树节点,都限定了其最大值为根节点
2014-03-05 15:19:30
756
原创 BinaryTree的高度求解及平衡树判断
BinaryTree 的高度指其根到叶子结点路径中最长的距离 比如如下binarytree: 2 3 1 4 2 3 5 6 7
2014-02-27 08:56:29
953
原创 对于栈的排序
ctci中stack一章的一道题对一个stack排序,只允许使用一个额外栈,不能使用其他数据结构。算法如下:类似于插入排序,首先先从初试栈中pop出一个元素,然后将其插入到新栈的适当位置。插入时,用peek()判断新栈当前的top和pop出来的这个元素的关系,如果时out of order,将当前这个top元素重新push回初始栈中。然后继续向下peek,直到找
2014-02-27 06:44:17
676
原创 MergeSort 求逆序数
逆序数,定义为一对元素,其大小关系不符合顺序。比如a[0]=1;a[1]=0,这两个元素就为一对逆序。求逆序数最直接的方法就是一组一组的判断,给一个长度为n的数组,使用两个for循环判断每一组数是不是逆序。因此,该方法复杂度为O(N^2).若要使用更快的算法,可基于merge sort来求。mergesort通过把数组一层一层的分割,在合并的方法对数组排序。在每
2014-02-24 09:52:45
990
原创 FasterRunner/SlowerRunner算法检测linkedlist中的loop
本技术博的第一文,为笔者在学习data structure and algorithm过程中的一些心得与总结,纯属复习。Ctci中linkedlist一节最后一道题:Detect the loop of a given linkde list and return to the first node of the list.Faster/Slower算法概述:
2014-02-24 04:24:04
828
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人