随笔分类 - Java
摘要:分别计算左右边界 o(N) o(N) class Solution { public int trap(int[] height) { /* 分别考虑每一个高度为底的情况,当前高度所能盛水量等于左侧边界和右侧边界高度中最小值减去当前元素高度 */ int size = height.length;
阅读全文
摘要:1.问题拆分成两个简单的实现 O(N) O(N) 将「相邻的孩子中,评分高的孩子必须获得更多的糖果」这句话拆分为两个规则,分别处理。 左规则:当 ratings[i−1]<ratings[i] 时,i 号学生的糖果数量将比 i−1 号孩子的糖果数量多。 右规则:当 ratings[i]>rating
阅读全文
摘要:1.暴力解法 O(n^2) class Solution { public int canCompleteCircuit(int[] gas, int[] cost) { int length = gas.length; //把当前i节点作为起始节点 for(int i = 0; i < lengt
阅读全文
摘要:就用除法 class Solution { public int[] productExceptSelf(int[] nums) { int[] answer = new int[nums.length]; int sum = 1; int zeroNum = 0; for (int i = 0;
阅读全文
摘要:先来个大的 class RandomizedSet { private HashSet<Integer> hashSet; public RandomizedSet() { hashSet = new HashSet<Integer>(); } public boolean insert(int v
阅读全文
摘要:排序,i代表至少发表的论文数量 时间复杂度:O(nlogn) 空间复杂度:O(logn) class Solution { public int hIndex(int[] citations) { // 0 1 3 5 6 int length = citations.length; Arrays.
阅读全文
摘要:class Solution { public int jump(int[] nums) { if (nums.length <= 1) return 0; //记录每次起跳所能跳到的最远的距离 int farestIndex = 0; int maxIndex = 0; int start = 0
阅读全文
摘要:用一个变量存放当前所能到达的最远的下标位置 class Solution { public boolean canJump(int[] nums) { int farestIndex = 0;// 记录当前最远能到达的下标 for (int i = 0; i <= farestIndex && i
阅读全文
摘要:动态规划,五种状态,关键是找出状态转移式 class Solution { public int maxProfit(int[] prices) { int buy1 = -prices[0]; int sell1 = 0; int buy2 = -prices[0]; int sell2 = 0;
阅读全文
摘要:将所有的递增段的增加值叠加起来 class Solution { public int maxProfit(int[] prices) { if (prices.length <= 1){ return 0; } int pre = 0; int p = 1; int maxPro = 0; int
阅读全文
摘要:从右往左遍历 class Solution { public int maxProfit(int[] prices) { Map<Integer, Integer> priceMap = new HashMap<Integer, Integer>(); Integer leftMaxNum = nu
阅读全文
摘要:class Solution { public void rotate(int[] nums, int k) { if (k > nums.length){ k = k % nums.length; } reverse(nums, nums.length - k, nums.length-1); r
阅读全文
摘要:快排 class Solution { public int majorityElement(int[] nums) { QuickSort(nums, 0, nums.length - 1); return nums[nums.length / 2]; } int partition(int[]
阅读全文
摘要:向前移动元素需要k的值,所以移动需要放在最后面。 class Solution { public int removeDuplicates(int[] nums) { if (nums.length < 1) return 0; int curNum = nums[0]; int k = 0; in
阅读全文
摘要:解 快慢指针 class Solution { public int removeDuplicates(int[] nums) { int pre = nums[0]; int place = 1; for (int i = 1; i < nums.length; i++){ if (nums[i]
阅读全文
摘要:解 快慢指针 class Solution { public int removeElement(int[] nums, int val) { //快慢指针 int place = 0; //实际插入位置 for (int i = 0; i < nums.length; i++){ if ( num
阅读全文
摘要:解 class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int[] nums3 = new int[m]; int p = 0; //指向nums3 int q = 0; //指向nums3 int
阅读全文
浙公网安备 33010602011771号