`
ouqi
  • 浏览: 41321 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论
文章列表
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 input string (not partial). The function prototype should be: bool isMatch(const char *s, const char ...
//判断二叉树是否镜面对称,即判断左右子树是否互为镜像 public class Solution { public boolean isSymmetric(TreeNode root) { // Note: The Solution object is instantiated only once and is reused by each test case. if(root == null) return true; return isSymmetric(root.left,root.right); ...
  没什么高端算法..注意下标,思维清晰就可以了。   //矩阵顺时针旋转90度,如果是in-place,则矩阵只能为方阵 //如果可以利用其它存储空间,则矩阵可以是m*n的,转换之后为n*m的 //i'(转换后行) = j(原列); j'(转换后列)= m-1-i(原行); //我们这里写就地逆置的 public class ClockWiseTransferMatrixInplace { public void transfer(int a[][]){ if(a == null||a[0]== null) return ; if(a.length!= a[ ...
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 requirements: Each child must have at least one candy. Children with a higher rating get more candies t ...
看到克隆图,自然想到遍历所有节点的算法,DFS/BFS改造下就可以了 本题中map用来保存已复制的节点(关系没有复制),同时也起到一个标记节点已访问过的作用。 /** * Definition for undirected graph. * class UndirectedGraphNode { * int label; * ArrayList<UndirectedGraphNode> neighbors; * UndirectedGraphNode(int x) { label = x; neighbors = new Arra ...
剑指offer上面的题目 代码: public RandomListNode copyRandomList(RandomListNode head) { // Note: The Solution object is instantiated only once and is reused by each test case. cloneNode(head); cloneRandom(head); return patitionClone(head); } public void c ...
这两题看起来有点像,但是实际上是完全不一样的,区别在于: The "Container With Most Water" solution will allow the water to rise above intermediate positions. With the "largest rectangle" problem, the rectangle cannot rise above intermediate bars. 也就是说Container With Most Water只考虑左右边界,[i,j]范围内的Area = min(h ...
放了个国庆,最近几天效率低下,我忏悔-。- 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 in the array return its index, otherwise return -1. You ma ...
本文转载自 http://blog.csdn.net/hackbuteer1/article/details/8022138   这个问题可以分为三种情况来考虑:情况一:root未知,但是每个节点都有parent指针此时可以分别从两个节点开始,沿着parent指针走向根节点,得到两个链表,然后求两个链表的第一个公共节点,这个方法很简单,不需要详细解释的。情况二:节点只有左、右指针,没有parent指针,root已知思路:有两种情况,一是要找的这两个节点(a, b),在要遍历的节点(root)的两侧,那么这个节点就是这两个节点的最近公共父节点;二是两个节点在同一侧,则 root->le ...
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 costs 
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 you are able to reach the last index. For example:A = [2,3,1,1,4], return true. A = 
算法思想 动态规划。 假设字符下标从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])            c[i-1][j-1];     if(i >= 1 && j >= 1 && word[i] == word[j])           当i == ...
K路排序每次相当于从K个数中选择最小的一个,加入结果数组,然后把对应的index+1,加入新的数去比较。 对于简单的2路或者3路排序,从这2个或者3个中选择最小的一个是比较方便的,但是如果要从K个数中选择最小,而且这K个 ...
Unique Binary Search Trees 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's. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / ...
典型的DFS吧 这一类还有permutation啊,求组合啊,之类的~~~ 代码: public class Solution { private ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); public ArrayList<ArrayList<Integer>> combinationSum(int[] candidates, int target) { // Start ...
Global site tag (gtag.js) - Google Analytics