- 博客(54)
- 资源 (3)
- 收藏
- 关注
原创 Java Comparator相关
//重写Comparator。需要明确范型的类,因此只能是类不能是primative类型,比如int[]就不行,需要转化为Integer[]。compare如果是a - b就是升序,compareTo也是一样的。list可以用stream不需要写comparator。 public void sortStringArray(String[] strArr) { A...
2020-04-26 06:54:06 163
原创 数据结构中confusing的地方整理
//时间复杂度//PriorityQueue,peek当然是O(1),offer和poll是O(logn)-你想想把值插进去最慢也是O(n)了如果这样了用PQ做什么//LinkedList,add,addFirst,addLast,remove,removeFirst,removeLast都是O(1),而remove(int index)需要指针走过去所以需要O(n)//Java中Arra...
2020-04-07 04:54:47 191
原创 Bitwise基础相关
//2的30次方1073741824,2的31次方2147483648//int数很容易理解,最大数是31个1,1111111 11111111 11111111 11111111,那么就是2的30次方,加起来就是2的31次方减去1,2147483647//最小的数就是最开始一个1,因为1代表符号位数10000000 00000000 00000000 00000000,-214748364...
2020-04-03 11:29:56 310
原创 Tarjan algorithm to find Strongly connected components
public static void main(String[] args) { OATest oaTest = new OATest(); int n = 4; List<Integer>[] graph = new List[n]; List<Integer> list0 = new Ar...
2020-03-26 12:51:44 180
原创 Java programming cheat sheet
// 1,数组和list之间的转换//1) String[] 转换成 list String[] strArr = {"a", "b"}; List list = Arrays.asList(strArr); System.out.println(list);//1.5) int[] 转换成 list int...
2020-03-26 09:55:14 240
原创 Top K Frequently Mentioned Keywords
//这里意思是如果出现在一个sentence里面只算一次public List<String> solve(int k, String[] keywords, String[] reviews) { Map<String, Integer> hm = new HashMap<>(); for (String sentence : revie...
2020-03-22 10:19:22 381
转载 Leetcode 329, Longest Increasing Path in a Matrix
Native的DFS,TLE:public int longestIncreasingPath(int[][] matrix) { if(matrix==null||matrix.length==0||matrix[0].length==0) return 0; int[] max = new int[1];
2017-01-18 11:41:36 213
转载 Leetcode 322, Coin Change
public int coinChange(int[] coins, int amount) { int[] dp = new int[amount + 1];//12 Arrays.fill(dp, Integer.MAX_VALUE); dp[0] = 0; for (int i = 1; i <= amoun
2017-01-17 13:48:46 210
转载 Leetcode 282, Expression Add Operators
public List addOperators(String num, int target) { List res = new ArrayList(); helper(res, "", target, num, 0, 0); return res; } private void helper(List res, Stri
2017-01-13 12:51:28 208
转载 Leetcode 138,Copy list with random nodes
public RandomListNode copyRandomList(RandomListNode head) { if(head==null) return null; HashMap map = new HashMap(); RandomListNode newhead = new RandomLis
2016-12-29 14:01:15 188
转载 Leetcode 113, Path Sum II
public void pathSumHelper(TreeNode root, int sum, List sumlist, List> pathlist){ if(root==null) return; sumlist.add(root.val); sum = sum-root.val; if(root
2016-12-28 11:31:27 202
原创 Leetcode 116, Populating Next Right Pointers in Each Node
public void connect(TreeLinkNode root) { if(root == null){ return; } if(root.left != null){ root.left.next = root.right; } if(root.right
2016-12-24 00:01:42 164
原创 Leetcode 98,Validate Binary Search Tree
private long min = Long.MIN_VALUE; public boolean isValidBST(TreeNode root) { if (root == null) return true; if (!isValidBST(root.left)){ return false;
2016-12-15 07:19:31 179
转载 Leetcode 132,Palindrome Partitioning 2
public int minCut(String s) { int len = s.length(); int[] D = new int[len + 1]; boolean[][] P = new boolean[len][len]; //the worst case is cutting by each char
2016-12-13 12:21:14 260
原创 Leetcode 213. House Robber II
public int rob(int[] nums) { if(nums==null||nums.length==0) return 0; if(nums.length==1){ return nums[0]; } int temp = nums[0]; nums[0] = 0; int a = helper(n
2016-12-13 05:43:21 301
原创 Leetcode 10, 44 Regular Expression Marching, Wildcard Matching
10, public boolean isMatch(String s, String p) { if (s == null || p == null) { return false; } int m = s.length(); int n = p.length();
2016-12-12 07:28:11 346
原创 Leetcode 97, Interleaving String
public boolean isInterleave(String s1, String s2, String s3) { if(s3.length() != s1.length() + s2.length()) { return false; } int m = s1.length();
2016-12-11 11:15:18 235
原创 Leetcode 512, Mouse in a maze
Rat in maze shortest pathGiven a 2 dimensional array with number 1s and 0s, 1 means wall, 0 means road,find the shortest path from 0 row 0 column to m row n column, if not possible return -1.
2016-12-11 08:06:11 508
原创 Leetcode 70, Climbing stair
DP写法: public int climbStairs(int n) { if(n == 0 || n == 1) return n; int [] dp = new int[n]; dp[0] = 1; dp[1] = 2; for(int i = 2; i< n
2016-12-11 07:27:19 288
转载 Leetcode 503,Add two numbers without using arithmetic operators
int add(int x, int y) { // Iterate till there is no carry while (y != 0) { // carry now contains common set bits of x and y int carry = x & y; //
2016-11-12 10:26:35 318
转载 Leetcode 504,How to replace Space with %20
private static char[] replaceSpaceInString(char[] str, int length) { int spaceCounter = 0; //First lets calculate number of spaces for (int i = 0; i < length; i++) {
2016-11-12 10:07:14 322
转载 Leetcode 169, 229 Majority element
public int majorityElement(int[] nums) { int candidate = nums[0], count = 0; for(int i = 1; i < nums.length; i++){ if(candidate == nums[i]){ count++;
2016-10-30 09:05:02 202
转载 Leetcode 109, Convert Sorted List to Binary Search Tree
package LeetC;public class LTTestNetbeans { /** * @param args the command line arguments */ public static void main(String[] args) { LTTestNetbeans test = new LTTestNetbea
2016-10-24 05:30:45 160
转载 Leetcode 111, Minimum Depth of Binary Tree
public int minDepth(TreeNode root) { if(root == null) return 0; int left = minDepth(root.left); int right = minDepth(root.right); if(root.left == null){
2016-10-23 07:40:14 153
转载 Leetcode 110, Balanced Binary Tree
public boolean isBalanced(TreeNode root) { if (getHeight(root) == -1){ return false; } return true; } public int getHeight(TreeNode root) { if (root == null) return 0; int left
2016-10-23 07:22:14 167
转载 Leetcode 33. Search in Rotated Sorted Array
Three cases:0 1 2 3 4 5 6 72 3 4 5 6 7 0 1 6 7 0 1 2 3 4 5 int search(int A[], int target) { int left = 0, right = A.length - 1; while (left <= right) { int mid = (left + right)
2016-10-20 02:10:28 149
转载 Leetcode 118, Pascal's triangle
public static List> generate(int numRows) { List> res = new ArrayList>(); if (numRows == 0) { return res; } for (int i = 0; i < numRows; i++) {
2016-10-16 05:50:32 165
转载 Leetcode 82, remove duplicates from sorted list 2
//2 pointers public ListNode deleteDuplicates(ListNode head) { if(head == null) return head; ListNode dummy = new ListNode(0); dummy.next = head; ListN
2016-10-15 13:21:55 249
转载 Leetcode 34, Search for a Range
public int[] searchRange(int[] A, int target) { int[] res = {-1, -1}; if (A == null || A.length == 0) { return res; } int start = 0; int end = A.len
2016-10-10 07:59:33 202
转载 Leetcode 20. Valid Parentheses - javascript
/** * @param {string} s * @return {boolean} */var isValid = function(s) { var stack = []; for(var i = 0; i < s.length; i++){ var c = s.charAt(i); if(c == "(" ||
2016-07-22 07:39:14 408
转载 Leetcode 6. ZigZag Conversion
public String convert(String s, int n) { //corner case if(s == null || "".equals(s) || n <= 0){ return ""; } if(n == 1){ return s;
2016-07-21 09:50:29 176
转载 Leetcode 7. Reverse Integer
public int reverse(int x) { boolean flag = true; long result = 0; if(x < 0){ flag = false; } x = Math.abs(x); while(x > 0
2016-07-21 08:51:22 186
转载 Leetcode 8. String to Integer (atoi)
public int myAtoi(String str) { if(str == null || str.length() == 0){ return 0; } //space, + -, non num, out of bound boolean fla
2016-07-21 08:50:30 192
转载 Leetcode 75. Sort Colors
public void sortColors(int[] nums) { //conor case if(nums == null || nums.length < 1){ return; } //two pointers int slow = 0; int f
2016-07-20 08:48:21 194
转载 Leetcode 33, 81 Search in Rotated Sorted Array I, II
public int search(int [] A,int target){ if(A==null||A.length==0) return -1; int low = 0; int high = A.length-1; while(low <= high){ int mid = (
2016-07-20 03:06:44 254
原创 Leetcode 34, Search for a Range
public int[] searchRange(int[] nums, int target) { if(nums == null || nums.length == 0){ return null; } int[] result = {-1, -1}; //binary search, 2 times
2016-07-13 14:27:23 194
转载 Leetcode 157,158, Read N Characters Given Read4
public int read(char[] buf, int n) { char[] buffer = new char[4]; int total = 0; boolean eof = false; //end of file while(!eof && total < n){ int coun
2016-06-11 01:46:18 835
转载 Leetcode 153, 154 Find Minimum in Rotated Sorted Array I, II
参考:http://bangbingsyb.blogspot.com/2014/11/leecode-find-minimum-in-rotated-sorted.html
2016-06-10 07:59:49 314
转载 Leetcode 38,Count and say
//思路:两重循环,第一重循环循环个数,第二重循环判断已生成字符,或者增加数量,或者把数字和字符加上。//答案用这个http://huntfor.iteye.com/blog/2059877(以下只是把ij换了下位置)。public static String countAndSay(int n) { if (n <= 0) { return null; } String str =
2016-05-31 14:01:10 220
转载 Leetcode 28, Strstr
//思路:双指针,因为两个string肯定二个循环。遍历整个长字符(减去短字符)作为起点,双指针长字符和短字符逐一比较。//答案综合https://leetcode.com/discuss/95153/java-easy-to-understand-solutions(双指针比较清晰)和爱做饭第一个(答案返回string过时了,//第二个没必要)。public int strStr
2016-05-31 13:56:53 233
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人