# computer-tests **Repository Path**: wenpingkk/computer-tests ## Basic Information - **Project Name**: computer-tests - **Description**: Here is the leetcode task code; just for my practice; - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-05-14 - **Last Updated**: 2025-12-01 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### 比较器的使用 ```java public int compare(Integer o1, Integer o2) { int d1 = Math.abs(o1 - highValue); int d2 = Math.abs(o2 - highValue); if (d1 == d2) { return o1 - o2; } else { return d1 - d2; } } ``` 在compare方法中,该方法返回正数,表示需要反转当前的两个对象【o1是前面的对象,o2是后面的对象】 上面的代码段,就可以解释的通了: 如果d1 == d2,如果两个元素和highValue的差值的绝对值相等,返回o1-o2;如果o1>o2则需要反转当前的元素,这样o2就到前面了[实现了从小到达的顺序排序],如果o1 参考的博客:https://zhuanlan.zhihu.com/p/54004622 ## Task 005 1.使用while循环来实现元素互换; ```java while (left end) { array[i] = strArray[i]; }else { array[i] = strArray[normalEnd--]; } } ``` 股票 https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/solutions/136684/121-mai-mai-gu-piao-de-zui-jia-shi-ji-by-leetcode-/ 方案1:暴力解法,2次循环; 方案2:一次循环;当前的元素去减去最低的价格;关键在于获取最低的价格; ```java /** * 一次循环;关键点在于,获取最低的价值,然后从后面的元素中,获取最大的差值 * * @param prices 数组 * @return 最大的利润 */ public int maxProfit(int[] prices) { int maxValues = 0; int minPrice = Integer.MAX_VALUE; for (int i = 0; i < prices.length; i++) { if (prices[i] < minPrice) { minPrice = prices[i]; } else if (prices[i] - minPrice > maxValues) { maxValues = Math.max(prices[i] - minPrice, maxValues); } } return maxValues; } ``` 最大连续子串的和 https://leetcode.cn/problems/maximum-subarray/solutions/228009/zui-da-zi-xu-he-by-leetcode-solution/ ```java /** * pre这个元素是关键的,在新的循环中,它要么是新的循环中的第一个元素,要么是自己和当前元素的和;pre = Math.max(nums[i], pre + nums[i]); * @param nums 数组 * @return 最大的连续子串的和 */ public int maxSubArray2(int[] nums) { if (nums.length == 1) { return nums[0]; } int pre =0; int maxValue = nums[0]; // 一次for循环,nums[i] = for (int i = 1; i < nums.length; i++) { pre = Math.max(nums[i], pre + nums[i]); maxValue = Math.max(maxValue, pre); } return maxValue; } ``` ## 动态规划 - TODO 20230604