type
Post
status
Published
date
Dec 13, 2022
slug
leetcode
summary
记录刷题代码
tags
LeetCode
category
笔记
icon
password

LeetCode-1. 两数之和

code1
class Solution { public int[] twoSum(int[] nums, int target) { //nums = [2, 7, 11, 15], target = 9 //nums = [2, 7, 11, 15], target = 9 for (int i = 0; i < nums.length; i++) { for (int j = i+1; j < nums.length; j++) { if(nums[i]+nums[j] == target){ return new int[]{i,j}; } } } return null; } }
code2
class Solution { public int[] twoSum(int[] nums, int target) { Map<Integer,Integer> map = new HashMap(); for(int i = 0; i<nums.length;i++){ int s = target - nums[i]; Integer r = map.get(s); if(r != null){ return new int[]{r,i}; } map.put(nums[i],i); } return null; } }

LeetCode-2. 两数相加

code
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { int n = 0; ListNode rs = new ListNode(); ListNode node = rs; ListNode q = l1; ListNode p = l2; while (q != null || p != null) { int i = 0; int j = 0; if (q != null) { i = q.val; } if (p != null) { j = p.val; } int num = i + j + n; n = num / 10; node.next = new ListNode(num % 10); node = node.next; if (q != null) { q = q.next; } if (p != null) { p = p.next; } } if (n > 0) { node.next = new ListNode(n); } return rs.next; } }

LeetCode-3. 无重复字符的最长子串

code
class Solution { public int lengthOfLongestSubstring(String s) { // 哈希集合,记录每个字符是否出现过 Set<Character> occ = new HashSet<Character>(); int n = s.length(); // 右指针,初始值为 -1,相当于我们在字符串的左边界的左侧,还没有开始移动 int rk = -1, ans = 0; for (int i = 0; i < n; ++i) { if (i != 0) { // 左指针向右移动一格,移除一个字符 occ.remove(s.charAt(i - 1)); } while (rk + 1 < n && !occ.contains(s.charAt(rk + 1))) { // 不断地移动右指针 occ.add(s.charAt(rk + 1)); ++rk; } // 第 i 到 rk 个字符是一个极长的无重复字符子串 ans = Math.max(ans, rk - i + 1); } return ans; } }

LeetCode-4. 寻找两个正序数组的中位数

code
class Solution { public double findMedianSortedArrays(int[] nums1, int[] nums2) { int[] arr = new int[nums1.length + nums2.length]; int index = 0; double rs = 0; for (int i : nums1) { arr[index++] = i; } for (int i : nums2) { arr[index++] = i; } Arrays.sort(arr); if (arr.length % 2 == 0) { int i = arr.length / 2; int j = i - 1; rs = (arr[i] + arr[j]) / 2.0; } else { int i = arr.length / 2; rs = arr[i]; } return rs; } }

LeetCode-5. 最长回文子串

code
class Solution { public String longestPalindrome(String s) { int n = s.length(); String res = ""; boolean[][] dp = new boolean[n][n]; for (int i = n - 1; i >= 0; i--) { for (int j = i; j < n; j++) { dp[i][j] = s.charAt(i) == s.charAt(j) && (j - i < 2 || dp[i + 1][j - 1]); //j - i 代表长度减去 1 if (dp[i][j] && j - i + 1 > res.length()) { res = s.substring(i, j + 1); } } } return res; } }
 

LeetCode-9. 回文数

code
class Solution { public boolean isPalindrome(int x) { if( x < 0){ return false; } int div = 1; while(x / div >= 10){ div *= 10; } while(x > 0){ int left = x/div; int right = x%10; if(left != right){ return false; } x = (x % div)/10; div /=100; } return true; } }
 

LeetCode-15. 三数之和

code
class Solution { public List<List<Integer>> threeSum(int[] nums) { Arrays.sort(nums); List<List<Integer>> res = new ArrayList<>(); for(int k = 0; k < nums.length - 2; k++){ if(nums[k] > 0) break; if(k > 0 && nums[k] == nums[k - 1]) continue; int i = k + 1, j = nums.length - 1; while(i < j){ int sum = nums[k] + nums[i] + nums[j]; if(sum < 0){ while(i < j && nums[i] == nums[++i]); } else if (sum > 0) { while(i < j && nums[j] == nums[--j]); } else { res.add(new ArrayList<Integer>(Arrays.asList(nums[k], nums[i], nums[j]))); while(i < j && nums[i] == nums[++i]); while(i < j && nums[j] == nums[--j]); } } } return res; } }

LeetCode-175. 组合两个表

code
# Write your MySQL query statement below select p.FirstName,p.LastName,a.City,a.State from Person p left join Address a on p.PersonId = a.PersonId

LeetCode-180. 连续出现的数字

code
select distinct Num as ConsecutiveNums from ( select Num, case when @prev = Num then @count := @count + 1 when (@prev := Num) is not null then @count := 1 end as CNT from Logs, (select @prev := null,@count := null) as t ) as temp where temp.CNT >= 3

LeetCode-181. 超过经理收入的员工

code
# Write your MySQL query statement below select e.Name as Employee from Employee e left join Employee f on e.ManagerId = f.Id where e.Salary > f.Salary

LeetCode-182. 查找重复的电子邮

code
# Write your MySQL query statement below select Email from Person group by Email having count(Email) >1

LeetCode-183. 从不订购的客户

code
# Write your MySQL query statement below select c.Name as Customers from Customers c left join Orders o on c.Id = o.CustomerId where o.id is null
 
 
Rust学习Jellyfin安装