自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

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

原创 String.split()

there are 12 characters with special meanings: the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, th...

2018-09-05 22:16:12 102

原创 PriorityQueue

 PriorityQueue<Map.Entry<String,Integer>> pq=new PriorityQueue<>(//pq的类型这里Map.Entry<K,V>应该就是指map中的一个元素的类型            (a,b) -> a.getValue()==b.getValue() ? b.getKey().compar...

2018-09-04 03:24:51 306

原创 Map.Entry

 PriorityQueue<Map.Entry<String,Integer>> pq=new PriorityQueue<>(            (a,b) -> a.getValue()==b.getValue() ? b.getKey().compareTo(a.getKey()) : a.getValue()-b.getValue());...

2018-09-04 00:24:50 139

原创 binary seach二分查找

对于sorted array可以用Index查找而对于unsorted可以用range 看似暴力,在low and high range较小或者其他情况下,是非常节约时间的

2018-09-03 22:53:56 109

原创 763. Partition Labels

int last = 0; int start = 0; for(int i = 0; i < S.length(); i++){ last = Math.max(last, map[S.charAt(i)-'a']); if(last == i){ list.add(last ...

2018-08-07 13:42:07 74

原创 map排序

Map<Interval,Integer> map=new TreeMap<Interval,Integer>(                new Comparator<Interval>(){                    public int compare(Interval i1,Interval i2){                ...

2018-08-01 19:58:41 107

原创 378. Kth Smallest Element in a Sorted Matrix

public class Solution { public int kthSmallest(int[][] matrix, int k) { int lo = matrix[0][0], hi = matrix[matrix.length - 1][matrix[0].length - 1] + 1;//[lo, hi) while(lo < hi...

2018-07-31 22:07:09 59

原创 Queue数据结构

队列是先进先出原则queue.poll() 就是从队列中删除第一个元素add()和offer()都是向队列中添加一个元素在二叉树按level遍历时很有用

2018-07-20 14:26:47 158

原创 binary search tree

二叉搜索树与二叉树不同二叉搜索树 中序遍历 得到的序列递增

2018-07-19 14:27:00 169

原创 异或运算

1. a ⊕ a = 02. a ⊕ b = b ⊕ a3. a ⊕b ⊕ c = a ⊕ (b ⊕ c) = (a ⊕ b) ⊕ c;4. d = a ⊕ b ⊕ c 可以推出 a = d ⊕ b ⊕ c.5. a ⊕ b ⊕ a = b.6.若x是二进制数0101,y是二进制数1011;则x⊕y=1110编程语言中异或符号为 ^=0 ^= a 得到的结果是 a...

2018-07-17 14:56:02 318

原创 List排序

list排序public static void main(String[] args) { List<String> list= new ArrayList<>();     list.add("11:23");     list.add("22:34");     list.add("22:04");     Collections.sort(list, new Co...

2018-05-30 10:21:20 79

原创 tips

Collections.max(count.entrySet(), Map.Entry.comparingByValue()).getKey();这个方法很有用 要记住!Integer.valueOf(char)这样得到的结果只是char的ascii码用char-'0'就好了

2018-05-25 12:37:47 115

原创 730. Count Different Palindromic Subsequences

substring的动态规划思想when s.charAt(i) != s.charAt(j):dp[i][j] = dp[i][j] = dp[i][j - 1] + dp[i + 1][j] - dp[i + 1][j - 1];When s.charAt(i) == s.charAt(j):the situation get much more complex and I fix a lot...

2018-05-23 17:44:14 176

原创 764. Largest Plus Sign

public static int orderOfLargestPlusSign(int N, int[][] mines) {     int[][] grid = new int[N][N];              for (int i = 0; i < N; i++) {         Arrays.fill(grid[i], N);     }              for...

2018-05-23 16:36:26 68

原创 java语言小知识

java区分大小写 K和k是不一样的变量三维数组定义 int[][][] a=new int[m][n][p];

2018-05-09 15:42:04 79

原创 646. Maximum Length of Pair Chain

二维数组的排序 Arrays.sort(pairs, (p1, p2) -> (p1[1] - p2[1]));class Solution {    public int findLongestChain(int[][] pairs) {        int re=0,form=Integer.MIN_VALUE;        Arrays.sort(pairs, (p1, p2) -...

2018-04-22 15:16:46 71

原创 712. Minimum ASCII Delete Sum for Two Strings

discussion中的解答是非常典型的动态规划其时间复杂度为O(mn)其实我所写的递归程序时间复杂度最多也不过是O(mn)但是我所写的程序报了time limit exceed可见递归所耗费的时间远比数组加减来的大...

2018-04-21 21:49:13 88

原创 647. Palindromic Substrings

按照以下算法,i看作中间数,分别向两边扩展,时间复杂度大大降低public class Solution { int count = 0; public int countSubstrings(String s) { if (s == null || s.length() == 0) return 0; for (int ...

2018-04-21 16:03:58 77

原创 338. Counting Bits

public int[] countBits(int num) { int[] f = new int[num + 1]; for (int i=1; i<=num; i++) f[i] = f[i >> 1] + (i & 1); return f;}以上代码充分体现动态规划i>>1 表示右移一位i&1 得到的是i的二进制最...

2018-04-21 14:40:17 84

原创 715. Range Module

使用treemap体验大不同 floorKey()这个函数有妙用class RangeModule { TreeMap<Integer, Integer> map; public RangeModule() { map = new TreeMap<>(); } public void addRange(int lef...

2018-04-20 18:55:49 127

原创 小细节

boolean a=false;打头小写String f="sd"; S大写

2018-04-20 18:54:47 52

原创 34. Search for a Range

查找最先出现的target和最后出现的target// Search for the left one    while (i < j)    {        int mid = (i + j) /2;        if (A[mid] < target) i = mid + 1;        else j = mid;    }    if (A[i]!=target) ret...

2018-04-20 15:48:02 80

原创 笔记3

Long x=43L;double & float value must have a decimal point.%取余只适用于Integer

2018-04-09 18:19:21 75

原创 Data structure- defining class笔记

static field: a single variable shared by a whole class of objects.static method: doesn't implicitly pass object as parameter.一般的方法,object调用时,会传参把object传给this因而static method里面不能出现this...

2018-04-08 17:38:09 83

原创 42. Trapping Rain Water

the solution in discussion always keep trapping from the lower side,从低的那一头开始,后面永远有一个更高的可以盛下之前加上的水class Solution {public: int trap(int A[], int n) { int left=0; int right=n-1; int ...

2018-04-08 11:15:44 76

原创 90. Subsets II

数组[a,b,c,d]我的方法是[],后循环放入[a],[b],[c],[d],然后再是[a,b],[a,c],[a,d]....其实还可以[], 后为[a],再循环是[b],[a,b],其实后者更节约空间List<List<Integer>> re= new ArrayList<List<Integer>>();//注意new之后的<>...

2018-03-30 13:21:31 65

原创 670. Maximum Swap

自己写的方法思路上稍微复杂了些其实可以两个for循环,nums[i]找后面数字的最大值交换discussion里面的方法是用了一个数组buckets,稍稍降低了时间复杂度class Solution { public int maximumSwap(int num) { char[] digits = Integer.toString(num).toCharArray();...

2018-03-29 16:53:26 71

原创 162. Find Peak Element

class Solution {public: int findPeakElement(const vector<int> &num) { for(int i = 1; i < num.size(); i ++) { if(num[i] < num[i-1])//此处只要写一个小于就好了,不需要两个条...

2018-03-29 13:17:56 54

原创 153. Find Minimum in Rotated Sorted Array

?????????????

2018-03-29 12:46:12 67

原创 66. Plus One

这个代码写得很巧妙,避免了很多冗余计算public int[] plusOne(int[] digits) { int n = digits.length; for(int i=n-1; i>=0; i--) { if(digits[i] < 9) { digits[i]++; retur...

2018-03-29 12:44:12 57

原创 380. Insert Delete GetRandom O(1)

几个list的常见方法list.contains ( object o )list.remove( int i / object o )list.add(object o)list.get(int i)得到一个随机数的方法new Random().nextInt( int i )  得到的数0=<a<i      ...

2018-03-21 15:56:35 91

原创 560. Subarray Sum Equals K

引入hashmap可以降低时间复杂度,但是增加了空间复杂度public class Solution { public int subarraySum(int[] nums, int k) { int sum = 0, result = 0; Map<Integer, Integer> preSum = new HashMap<>()...

2018-03-21 11:39:26 70

原创 27. Remove Element

该算法更简洁,且时间复杂度低int removeElement(int A[], int n, int elem) { int begin=0; for(int i=0;i<n;i++) if(A[i]!=elem) A[begin++]=A[i]; return begin;}

2018-03-21 10:47:31 65

原创 718. Maximum Length of Repeated Subarray

用以下的一个dp[i][j]矩阵记录,少了一整个while循环,降低了时间复杂度class Solution { public int findLength(int[] A, int[] B) { if(A == null||B == null) return 0; int m = A.length; int n = B.length; ...

2018-03-20 16:29:20 77

原创 48. Rotate Image

将整个矩阵看作若干个同心的正方形然后每一次将一个正方形旋转到位another method:将顺时针旋转分成先上下旋转 再做对称

2018-03-20 15:00:46 65

原创 747. Largest Number At Least Twice of Others

找一个序列里面第二大的数的方法 int max = Integer.MIN_VALUE + 1; int secondMax = Integer.MIN_VALUE; int index = 0; for(int i = 0; i < nums.length; i++){ if(nums[i] >...

2018-03-18 17:13:21 68

原创 611. Valid Triangle Number

用这个算法比三个for循环号,时间复杂度从n^3到n^2public static int triangleNumber(int[] A) { Arrays.sort(A); int count = 0, n = A.length; for (int i=n-1;i>=2;i--) { int l = 0, r = i-1; while...

2018-03-18 16:54:42 69

原创 729. My Calendar I

???????????treeMap 解法

2018-03-18 16:37:11 121

原创 768. Max Chunks To Make Sorted II

leftMax && rightMin

2018-03-18 14:47:07 142

原创 121. Best Time to Buy and Sell Stock

1.给定一个prices数组,计算出它的差值比如:prices: [5,6,7,4,3,5]得到: dis: [2,-4,2]代码如下:        int n=prices.length;        if(n<=1) //prices数组过小需要判断            return 0;        int[] dis=new int[n];        int more=0...

2018-03-14 20:43:50 74

空空如也

空空如也

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

TA关注的人

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