- 博客(18)
- 收藏
- 关注
原创 找硬币
【Leetcode】322. Coin Change1、You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amou...
2018-04-11 20:16:24
526
原创 背包问题
// 0/1背包class Solution{ public int getMaxValue(int []w, int []v, int n, int c) { int [][]dp = new int[n+1][c+1]; for(int i=1; i<=n; i++) { for(int j=1;...
2018-04-11 20:16:12
188
原创 两个有序数组中位数
public class Solution { public double findMedianSortedArrays(int A[], int B[]) { if(A==null || A.length==0) return (B[B.length/2] + B[(B.length-1)/2])/2.0; if(B==null || ...
2018-04-11 20:16:02
217
原创 最长递增子序列
public class Solution{ public int getHeight(int []nums, int n) { int []dp = new int[n]; int len = 1; dp[0] = nums[0]; for(int i=1; i<n; i++) {...
2018-04-11 20:15:48
134
原创 LRU(Least Recently Used) cache.
class LRUCache { Map<Integer, DlinkedNode> map; DlinkedNode head; DlinkedNode tail; int capacity; int count; public LRUCache(int cap) { map = new HashM...
2018-04-11 20:15:38
306
原创 最长公共子序列
class Solution{ public int[][] LCSLength(char []a, char []b) { int lenA = a.length; int lenB = b.length; int [][]dp = new int[lenA+1][lenB+1]; int [][]flag = n...
2018-04-11 20:15:29
148
原创 二叉树遍历(递归/非递归)
前序遍历public class Solution { public ArrayList<Integer> preorderTraversal(TreeNode root) { ArrayList<Integer> res = new ArrayList<>(); preorder(root, res); ...
2018-04-11 20:15:20
172
原创 直方图/矩形中的最大矩形
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.For example, given the following matrix:public class Solution { public int ma...
2018-04-11 20:15:10
336
原创 最近公共祖先
二叉树public class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(root==null || p==root || q==root) return root; TreeNode left = low...
2018-04-11 20:15:01
133
原创 求点集凸包
class Solution { public List<Point> outerTrees(Point []points) { int firstIndex = 0; Point firstP = points[0]; for(int i=0; i<points.length; i++) {...
2018-04-11 20:14:52
444
原创 排序
冒泡排序class BubbleSort{ public static void bubbleSort(int []nums, int n) { for(int i=0; i<n-1; i++) { for(int j=0; j<n-i-1; j++) { ...
2018-04-11 20:14:44
149
原创 第k大的数
class Solution{ public int findKthLargest(int []nums, int k) { shuffle(nums); k = nums.length - k; int lo = 0; int hi = nums.length - 1; while (lo <...
2018-04-11 20:14:35
131
原创 从右边看二叉树组成的序列
public class Solution { public List<Integer> rightSideView(TreeNode root) { List<Integer> res = new ArrayList<>(); rightSideViewNew(root, res, 0); r...
2018-04-11 20:14:23
350
原创 randM()到randN()
1、rand5()到rand7()class Rand5To7{ public int rand5() { return (int) (Math.random() * 5) - 1; } public int rand5To7() { int num = 0; do{ num = (r...
2018-04-11 20:14:10
1222
原创 素数
小于N的素数个数,欧拉筛选public class Solution { public int countPrimes(int n) { boolean []flag = new int[n]; int res = 0; for(int i=2; i<n; i++) { if(flag...
2018-04-11 20:14:00
131
原创 KMP算法
public class KMP { public static int kmp(String str, String patten) { char []chS = str.toCharArray(); char []chP = patten.toCharArray(); int n = chS.length; in...
2018-04-11 20:13:40
147
原创 字典序
1、给定整数n和m, 将1到n的这n个整数按字典序排列之后, 求其中的第m个数。 对于n=11, m=4, 按字典序排列依次为1, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 因此第4个数是2.long res = getAnswer(n, 1, m-1);class Solution{ public static long getAnswer(long n, l...
2018-04-11 20:13:31
641
原创 数组中两两异或大于m的个数
给定整数m以及n各数字A1,A2,..An,将数列A中所有元素两两异或,共能得到n(n-1)/2个结果,请求出这些结果中大于m的有多少个1. aDigit=1, mDigit=1时,字典中第k位为0,异或结果为1,需要继续搜索更低位,第k位为1,异或结果为0,小于mDigit,不用理会; 2. aDigit=0, mDigit=1时,字典中第k位为1,异或结果为1,需要继续搜索更低位,第k位...
2018-04-11 20:13:22
1253
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人