找工作系列之-刷题记录
文章平均质量分 62
shayeo
这个作者很懒,什么都没留下…
展开
-
归并排序链表
/* * Merge two sorted linked lists and return it as a new list. * The new list should be made by splicing together the nodes of the first two lists. */public class MergeTwoSortedList { cl...原创 2013-08-29 10:54:48 · 95 阅读 · 0 评论 -
[leetcode]Gas Station
Gas Station AC Rate: 296/1493 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 co...原创 2013-09-30 16:40:49 · 77 阅读 · 0 评论 -
[leetcode]Search in Rotated Sorted Array
放了个国庆,最近几天效率低下,我忏悔-。-Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found...原创 2013-10-08 23:18:21 · 75 阅读 · 0 评论 -
[leetcode]Container With Most Water 和Largest Rectangle in Histogram
这两题看起来有点像,但是实际上是完全不一样的,区别在于:The "Container With Most Water" solution will allow the water to rise above intermediate positions. With the "largest rectangle" problem, the rectangle cannot rise abov...原创 2013-10-13 22:59:27 · 103 阅读 · 0 评论 -
[leetcode] Copy List with Random Pointer
剑指offer上面的题目代码: public RandomListNode copyRandomList(RandomListNode head) { // Note: The Solution object is instantiated only once and is reused by each test case. cloneNode(...原创 2013-10-14 16:28:06 · 67 阅读 · 0 评论 -
[leetcode]Clone Graph
看到克隆图,自然想到遍历所有节点的算法,DFS/BFS改造下就可以了本题中map用来保存已复制的节点(关系没有复制),同时也起到一个标记节点已访问过的作用。 /** * Definition for undirected graph. * class UndirectedGraphNode { * int label; * ArrayList<Un...原创 2013-10-14 17:10:38 · 85 阅读 · 0 评论 -
[leetcode]Candy
Candy AC Rate: 1460/9941 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 requi...原创 2013-10-15 11:54:41 · 81 阅读 · 0 评论 -
矩阵旋转
没什么高端算法..注意下标,思维清晰就可以了。 //矩阵顺时针旋转90度,如果是in-place,则矩阵只能为方阵//如果可以利用其它存储空间,则矩阵可以是m*n的,转换之后为n*m的//i'(转换后行) = j(原列); j'(转换后列)= m-1-i(原行);//我们这里写就地逆置的public class ClockWiseTransferMatrixIn...原创 2013-10-22 19:59:37 · 132 阅读 · 0 评论 -
[leetcode]Symmetric Tree
//判断二叉树是否镜面对称,即判断左右子树是否互为镜像public class Solution { public boolean isSymmetric(TreeNode root) { // Note: The Solution object is instantiated only once and is reused by each test case....原创 2013-10-23 11:06:07 · 107 阅读 · 0 评论 -
[leetcode] Jump Game
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 ...原创 2013-09-30 12:00:55 · 175 阅读 · 0 评论 -
[leetcode] edit Distance
算法思想 动态规划。假设字符下标从1开始。c[i][j] 表示word1[1~i] 到word2[1~j]的最短编辑距离。则有递推式:c[i][j] = min(c[i-1][j],c[i][j-1],c[i-1][j-1])+1; if(i >= 1 && j >= 1 && word[i] != word[j]) ...原创 2013-09-17 22:28:03 · 59 阅读 · 0 评论 -
[leetcode]Best Time to Buy and Sell Stock
Best Time to Buy and Sell StockSay you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy on...原创 2013-09-09 11:50:35 · 81 阅读 · 0 评论 -
[leetcode]RemoveDuplicatedInsortedArray
Search in Rotated Sorted Array Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another arra...原创 2013-09-10 17:01:39 · 81 阅读 · 0 评论 -
[leetcode]nextPermutation
思路如下:假设当前给定permutation序列为 a1 a2 a3...an.从序列尾部a[n]开始向头部扫描,找到第一个相邻升序序列 a[i-1]< a[i] ; 因为是扫描到的第一个相邻升序,则此时有a[i]>=a[i+1]>=a[i+2]...>=a[n].从a[n]逆序遍历至a[i] 找到第一个大于a[i-1]的值a[j] ,交换a[i-1]和a...原创 2013-09-15 10:59:11 · 77 阅读 · 0 评论 -
[leetcode]removeNthFromend
public ListNode removeNthFromEnd(ListNode head, int n) { // Start typing your Java solution below // DO NOT write main() function ListNode fast = head; ListNode slo...原创 2013-09-15 11:17:41 · 191 阅读 · 0 评论 -
[leetcode]PatitionList
还是和removeDuplicateinSortedArr类似的思路 循环不变式 package oj.leetcode;import data.ListNode;/* * 从loop invariant的角度去考虑这个问题。也就能在考虑问题的时候,完成了对算法在数学意义上的证明。假设,你已经有了一个lessList,一个geList。然后,就是不断扫描...原创 2013-09-15 11:20:53 · 65 阅读 · 0 评论 -
[leetcode]Combination sum
典型的DFS吧 这一类还有permutation啊,求组合啊,之类的~~~代码:public class Solution { private ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); public Array...原创 2013-09-15 15:42:56 · 55 阅读 · 0 评论 -
[leetcode]Unique Binary Search Trees
Unique Binary Search TreesGiven 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's. 1 3 ...原创 2013-09-16 11:41:15 · 64 阅读 · 0 评论 -
[leetcode]k路排序
K路排序每次相当于从K个数中选择最小的一个,加入结果数组,然后把对应的index+1,加入新的数去比较。对于简单的2路或者3路排序,从这2个或者3个中选择最小的一个是比较方便的,但是如果要从K个数中选择最小,而且这K个数会不断更新,那想一想,这里面最完美的数据结构就是小顶堆呀!堆顶永远是最小的元素,将其取出,其所在数组的index++,将之后所在数组的后一元素加入队列,再次进行比较!j...原创 2013-09-16 16:19:20 · 150 阅读 · 0 评论 -
[leetcode]Regular Expression Matching
Implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire inp...原创 2013-10-23 23:06:35 · 107 阅读 · 0 评论