自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(236)
  • 收藏
  • 关注

原创 max sub sum II

第一题:给一个array,给一个数字n,找出n个连续数字which和最大。第二题:给一个array,给一个数字n,找出3个不overlap的n个连续数字使得和最大,比如说给 1 3 7 7 2 1 1 4 8 8 6 1 1 9,结果就是1 3 (7 7) 2 1 1 (4 8) (8 6) 1 1 9,输出他们的和。import java.util.*;public class Ma

2016-07-31 23:36:53 414

原创 remove comment

public class RemoveComments { public static void main(String[] args) { String str = "e//b/*c*/d\nefee"; System.out.print(removeComments(str)); } static String removeComments(String prgm) {

2016-07-31 23:07:47 471

原创 strstrII

1. leetcode原题,strstr,唯一区别就是参数是char array,不让用string方法做(当然也包括stringbuilder),返回在hackstack里第一次匹配needle的substring的第一个字符下标。2. 大意就是把第一个haystack参数变成一个二维数组,然后找needle在haystack第一次出现的位置,同样的不能用string相关方法,还是返回把ha

2016-07-31 22:07:07 369

原创 Generate Min Heap

1. 给一个数组。要你变成二叉树,必须满足条件:1. 满足minHeap性质2. 树的中序遍历和数组顺序一致2. followup: 给定了上述生成的树,要插入一个新节点,同样要满足上述条件。followup:如果要插入很多节点,有没有方法提速?TreeNode generateTree(int[] a, int left, int right) { if (left > ri

2016-07-31 21:22:02 271

原创 Sparse Vector Poduct

public Map sparseVector(Map idx_val_a, Map idx_val_b) { Map ret = new HashMap<>(); for (Integer idx : idx_val_a.keySet()) { if (idx_val_b.containsKey(idx)) { ret.put(idx, idx_val_a.get(idx)

2016-07-31 21:00:59 298

原创 reverse list (recursive)

/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode reve

2016-07-31 20:55:14 376

原创 find min num tree path

给个Tree 不一定是平衡的, 要求 把所有路径排序后 按字符串那样的比较大小方法 找出最小的路径 时间要求线性的。 比如     5     /  \  10  3 / \    /1 7 8路径有 5 10 1 ; 5 10 7 ; 5 3 8排序后 1 5 10 ; 5 7 10 ; 3 5 8;所以按字符串类型排序 为 1 5 10 public static

2016-07-31 19:49:21 438

原创 task schedule I follow up

如果cooldown很小,可认为是常数,如何做到,O(n)时间,O(1)空间?public String schedule11(int [] array, int recover){ Queue queue = new LinkedList<>(); StringBuilder sb = new StringBuilder(); int tota

2016-07-31 19:23:48 452

原创 Find Celebrity II

给一堆object,随便存在什么datastructure里面,有一个function可以用来判断两个object的相对大小,但不可传递,a>b,b>c 不能推出a>c。找出这堆objects中的其中一个,大于所有其他的objects。import java.util.LinkedList;public class FindCelebrityII { int compare(Objec

2016-07-31 18:25:38 273

原创 common substring >= k

public class CommonSubstringK { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(longestCommonSubstring("leetcode", "codyabc", 3)); System.out.pri

2016-07-31 17:50:18 222

原创 Print Matrix Diagonally

public class PrintDiagonally { public static void main(String[] args) { int[][] matrix = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } }; printDiagonally(matrix); }

2016-07-31 16:20:17 320

原创 get all Palindromes of a string

public static void main(String[] args) { AllPalindromes a = new AllPalindromes(); for (String str: a.getAllPalindrome("accbrrbccai2")){ System.out.println(str); };

2016-07-30 19:20:05 257

原创 subarray sum

public static void main(String[] args) { // TODO Auto-generated method stub int[] array1 = { 2, 3, 3, 5, 6 }; int[] res = getSubarraySum(array1, 8); for (int i : res) {

2016-07-30 05:52:27 235

原创 BFS print BT all path from root to leaf

public class BFS_RootToLeaf { public List getPath(TreeNode root) { List res = new LinkedList<>(); if (root == null) { return res; } Queue queue = new Li

2016-07-30 04:28:33 232

原创 Move Char

给一个字符串和一个输入的字符,要求移走给定的字符 public static void main(String[] args) { System.out.println(moveStr("abcebaibbw", 'b')); System.out.println(moveStr("bbbbbbbbbb", 'b')); System.o

2016-07-29 09:16:35 422

原创 Natural Comparator

注意corner cases:自然string comparator。不知道的搜下。就是string 比较的时候考虑里面数字的大小,比如 abc9 ab9  因为char比digit重要。public class NaturalComparator implements Comparator {    public static void main(String[] args)

2016-07-29 08:18:56 1258

原创 Max Interval intersection

interval [startTime, stoptime)   ----integral  time stamps给这样的一串区间 I1, I2......In  找出 一个 time stamp  出现在interval的次数最多。startTime . 鐗涗汉浜戦泦,涓€浜╀笁鍒嗗湴. more info on 1point3acres.comexample:  

2016-07-29 07:14:05 413

原创 Task Schedule II

参考:点击打开链接followup是tasks是无序的.一开始是有序的,比如说1, 1, 2, 1,一定要先执行第一个task1,然后等task1恢复,再执行第2个task1,再执行task2..... followup是无序的,就是不用按给的顺序执行,也就是可以先执行task1,然后task1还没恢复时,先执行task2, etc......这次得记录每个task的freque

2016-07-29 05:29:34 310

原创 就是现在

把一切的痛惜,惋惜和珍惜都放到现在,就是现在!

2016-07-28 06:47:30 401

原创 只要好好努力!!!!!!!

我们一定能有我们的蓉儿的,只要好好努力!!!只要好好努力,我们就一定能有我们的蓉儿的!!!只要好好努力,我们就一定能有我们的蓉儿的!!!只要好好努力,我们就一定能有我们的蓉儿的!!!只要好好努力!!!!!!!

2016-07-28 06:26:56 277

原创 Line Reflection

这道题我们要看清了,是平行于Y轴,而不是X轴。我知道我们很累,但也要看清楚题啊!!!public class Solution { public boolean isReflected(int[][] points) { int max = Integer.MIN_VALUE, min = Integer.MAX_VALUE; if (points ==

2016-07-27 15:14:47 313

原创 一定能有我们的蓉儿的!

好好努力,抓紧这最后的一个月,全力以赴,我们就一定能取得成功,一定能有我们的蓉儿的!Work hard,Good luck!

2016-07-27 14:43:15 310

原创 Design Tic-Tac-Toe

巩固矩阵的操作,尤其是对角线的操作。之后处理follow uppublic class TicTacToe { int[][] matrix; /** Initialize your data structure here. */ public TicTacToe(int n) { matrix = new int[n][n]; }

2016-07-27 04:19:15 357

原创 Top K Frequent Elements

先贴出用了heap的做法,稍后要学会用桶排序来做。public class Solution { public List topKFrequent(int[] nums, int k) { List res = new LinkedList<>(); if (nums == null || nums.length == 0) {

2016-07-26 17:36:12 211

原创 Counting Bits

先贴出一种自己马上写出的,更好的做法之后补上 public int[] countBits(int num) { if (num == 0) { return new int[]{0}; } int[] res = new int[num + 1]; for (int i = 1; i <= num;

2016-07-26 16:35:41 196

原创 Reconstruct Itinerary

一道好题,要常做常新。当中犯了两个错误:1. 对于[["JFK","KUL"],["JFK","NRT"],["NRT","JFK"]],会有这样的错误: ["JFK","KUL","NRT","JFK"]2. 对于sjc,是没有queue的,所以必须判断queue为空的情况public class Solution { List res = new LinkedList<>(

2016-07-26 16:00:01 354

原创 Verify Preorder Serialization of a Binary Tree

参考:点击打开链接对于二叉树,我们把空的地方也作为叶子节点(如题目中的#),那么有所有的非空节点提供2个出度和1个入度(根除外)所有的空节点但提供0个出度和1个入度我们在遍历的时候,计算diff = outdegree – indegree. 当一个节点出现的时候,diff – 1,因为它提供一个入度;当节点不是#的时候,diff+2(提供两个出度) 如果序列式合法的,

2016-07-26 15:01:57 207

原创 Odd Even Linked List

两根指针/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNod

2016-07-26 14:41:31 278

原创 Wiggle Sort II

再接着想0(n)的解法。public class Solution { public void wiggleSort(int[] nums) { if (nums == null || nums.length == 0) { return; } Arrays.sort(nums); int[] re

2016-07-26 14:15:40 249

原创 Maximum Product of Word Lengths

一道位操作的好题。当中的两个操作很好。public class Solution { public int maxProduct(String[] words) { if (words == null || words.length == 0) { return 0; } int[] elements = ne

2016-07-25 18:06:23 237

原创 等待队列

更新要看的:Range Sum Query - Mutable

2016-07-25 14:00:59 223

原创 Range Sum Query 2D - Immutable

这道题一定要注意下标,一开始自己做的全都没有下标-1这一操作。public class NumMatrix { int[][] sum; public NumMatrix(int[][] matrix) { if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {

2016-07-25 04:11:57 245

原创 Binary Tree Longest Consecutive Sequence

要有好消息/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solut

2016-07-24 18:09:03 300

原创 Flip Game II

这道题不作要求public class Solution { public boolean canWin(String s) { if (s == null || s.length() == 0) { return false; } return helper(s.toCharArray()); }

2016-07-24 17:21:08 250

原创 Ugly Number II

参考:点击打开链接根据提示中的信息,我们知道丑陋数序列可以拆分为下面3个子列表:(1) 1×2, 2×2, 3×2, 4×2, 5×2, …(2) 1×3, 2×3, 3×3, 4×3, 5×3, …(3) 1×5, 2×5, 3×5, 4×5, 5×5, …仔细观察上述三个列表,我们可以发现每个子列表都是一个丑陋数分别乘以2,3,5,而要求的丑陋数就是从已经生

2016-07-24 16:43:38 228

原创 Ugly Number

参考:点击打开链接检测一个数是否为丑陋数,所谓丑陋数就是其质数因子只能是2,3,5。那么最直接的办法就是不停的除以这些质数,如果剩余的数字是1的话就是丑陋数了public class Solution { public boolean isUgly(int num) { while (num >= 2) { if (num % 2 == 0)

2016-07-24 16:15:20 249

原创 Factorial Trailing Zeroes

参考:点击打开链接public class Solution { public int trailingZeroes(int n) { int sum= 0; while (n > 0) { sum += n/5; n = n/5; } return sum; }}

2016-07-24 15:32:20 213

原创 Power of Four

这个不错,红红火火恍恍惚惚哼哼哈嘿public class Solution { public boolean isPowerOfFour(int num) { int count1 = 0; int count0 = 0; while (num > 0) { count1 += num & 1;

2016-07-24 15:09:44 199

原创 Power of Three

这个掌握最基本的迭代就好。public class Solution { public boolean isPowerOfThree(int n) { if (n == 0) { return false; } return n == Math.pow(3, Math.round(Math.log(n)/Math.

2016-07-24 15:01:21 226

原创 Power of Two

参考:点击打开链接那么我们来观察下2的次方数的二进制写法的特点:1     2       4         8         16   ....1    10    100    1000    10000 ....那么我们很容易看出来2的次方数都只有一个1,剩下的都是0,所以我们的解题思路就有了,我们只要每次判断最低位是否为1,然后向右移位,最后统计1的

2016-07-24 14:30:24 220

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除