![](https://img-blog.csdnimg.cn/20201014180756922.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
文章平均质量分 53
Wu Liuqi
这个作者很懒,什么都没留下…
展开
-
每日LeetCode--1
1、给定一个 32 位有符号整数,将整数中的数字进行反转注意:假设我们的环境只能存储 32 位有符号整数,其数值范围是 [−231, 231 − 1]。根据这个假设,如果反转后的整数溢出,则返回 0。class Solution { public int reverse(int x) { int rev = 0; while (x != 0)...原创 2018-08-19 16:00:02 · 120 阅读 · 0 评论 -
排序算法
1、归并排序public class Main{ public static void main(String[] args) {//归并排序 int[] arr = {4,5,2,1,3,5,6,1,2}; Main.sort(arr, 0, arr.length-1); for (int i = 0; i < arr.length; i++) { Syst...原创 2018-08-26 16:19:57 · 113 阅读 · 0 评论 -
每日LeetCode——第三大数
414、给定一个非空数组,返回此数组中第三大的数。如果不存在,则返回数组中最大的数。要求算法时间复杂度必须是O(n)。思路已经注释比较清楚啦,还有不理解的,或者更好的可以评论。 public int thirdMax(int[] nums) { Arrays.sort(nums); int thirdMax = Integer.MAX_VALUE; int i = nums...原创 2018-08-23 22:37:01 · 267 阅读 · 0 评论 -
每日LeetCode——数组去重
1、给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度。不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。public int removeElement(int[] nums, int val) { //...原创 2018-08-21 10:50:42 · 2435 阅读 · 0 评论 -
每日LeetCode——股票购买
121、给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润。注意你不能在买入股票前卖出股票。 public int maxProfit(int[] prices) { int max = 0; for (int i = 0; i < prices.le...原创 2018-08-24 13:26:17 · 586 阅读 · 1 评论 -
每日LeetCode——3sum、4sum*(三数之和,四数之和)
1、给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。这道题最简单的思想就是嵌套循环,但是时间复杂度O(n^3)时间复杂度太大,会超时。只能选择一个更优的解法,就是把三个数的和变成两个数的和。 public List<List<Integer>>...原创 2018-08-21 19:18:07 · 168 阅读 · 0 评论 -
每日LeetCode——最小子序列
53、最大子序和给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和一、这道题首先想到的一定是遍历,不过复杂度O(n^2)第一个循环选定开始节点,里面嵌套循环 决定结束节点 ,一直比较着,直到把所有元素都比较一遍,最后每次更新额结果,都放在max里面。 int max = Integer.MIN_VALUE;//兩重遍歷 for ...原创 2018-10-05 15:46:04 · 2456 阅读 · 0 评论 -
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
关键点是 重合点 和 斜率不存在的点(斜率不存在的点完全可以利用乘法弥补)也就是交叉相乘法。/** * Definition for a point. * class Point { * int x; * int y; * Point() { x = 0; y = 0; } * Point(int a, int b) { x = a; y =...转载 2019-04-26 11:23:45 · 213 阅读 · 0 评论