classSolution{ publicint[] getLeastNumbers(int[] arr, int k) { if (k==0) returnnewint[]{}; if (arr.length<=k) return arr; getPivot(arr, 0, arr.length-1, k-1); int[] res = newint[k]; for (int i=0; i<k; i++) { res[i] = arr[i]; } return res; }
publicvoidgetPivot(int[] arr, int l, int r, int k){ int p = partition(arr, l, r); if (p<k) getPivot(arr, p+1, r, k); elseif (p>k) getPivot(arr, l, p-1, k); elsereturn; }
//快速排序的partition函数,参考《算法》第四版的写法 publicintpartition(int[] arr, int lo, int hi){ int i = lo, j = hi+1; int pivot = arr[lo]; while (true) { while (arr[++i]<pivot) if (i==hi) break; while (arr[--j]>pivot) if (j==lo) break; if (i>=j) break; swap(arr, i, j); } swap(arr, lo, j); return j; }
publicvoidswap(int[] arr, int i, int j){ int t = arr[i]; arr[i] = arr[j]; arr[j] = t; } }
public Double GetMedian(){ if (count%2==1) returnnew Double (maxHeap.peek()); elsereturnnew Double (minHeap.peek()+maxHeap.peek())/2; } }
连续子数组的最大和
思路:如果前面数之和为负数,它对最大子数组和没有帮助,用当前元素的值作为新的和。
1 2 3 4 5 6 7 8 9 10 11
classSolution{ publicintFindGreatestSumOfSubArray(int[] array){ int sum = Integer.MIN_VALUE, max = array[0], n = array.length; for (int i=0; i<n; i++) { if (sum < 0) sum = array[i]; else sum += array[i]; max = Math.max(max, sum); } return max; } }
classSolution{ publicintNumberOf1Between1AndN_Solution(int n){ int res = 0; int i = 1, before = 0, behind = 0, current = 0; while (n/i!= 0) { before = n/(i*10); behind = n-(n/i)*i; current = (n/i)%10; if (current==0) res += i*before; elseif (current==1) res += i*before+behind+1; else res += i*(before+1); i *= 10; } return res; } }
classSolution{ publicintlengthOfLongestSubstring(String s){ if (s.length()==0) return0; HashMap<Character, Integer> map = new HashMap<>(); int left = -1, right = 0; int res = 0; while (right<s.length()) { char c = s.charAt(right); if (map.containsKey(c)) { left = Math.max(left, map.get(c)); } map.put(c, right); res = Math.max(res, right-left); right++; } return res; } }
publicclassSolution{ private HashMap<Character, Integer> map = new HashMap<>(); private StringBuilder sb = new StringBuilder(""); //Insert one char from stringstream publicvoidInsert(char ch){ if (map.containsKey(ch)) map.put(ch, map.get(ch)+1); else map.put(ch, 1); sb.append(ch); } //return the first appearence once char in current stringstream publiccharFirstAppearingOnce(){ for (int i=0; i<sb.length(); i++) { if (map.get(sb.charAt(i))==1) return sb.charAt(i); } return'#'; } }
publicintmergeSort(int[] nums, int left, int right, int[] copy){ if (left>=right) return0; int mid = left + (right-left)/2; int l = mergeSort(nums, left, mid, copy); int r = mergeSort(nums, mid+1, right, copy); return l+r+merge(nums, left, right, mid, copy); }
publicintmerge(int[] nums, int left, int right, int mid, int[] copy){ for (int i=left; i<=right; i++) { copy[i] = nums[i]; } int res = 0; int i=left, j=mid+1, k=left; while (i<=mid && j<=right) { if (copy[i]<=copy[j]) { nums[k] = copy[i]; i++; } else { res += mid-i+1;//比归并排序仅多了这一步 nums[k] = copy[j]; j++; } k++; } while (i<=mid) nums[k++] = copy[i++]; while (j<=right) nums[k++] = copy[j++]; return res; } }