LeetCode每日一题

LeetCode每日一题

✨2024-12-27

📙1. 两数之和

法一:遍历数组

1
2
3
4
5
6
7
8
9
10
public int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length - 1; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] == target) {
return new int[]{i, j};
}
}
}
return null;
}

法二:利用哈希表

numkey

indexvalue

1
2
3
4
5
6
7
8
9
10
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(target - nums[i])) {
return new int[]{map.get(target - nums[i]), i};
}
map.put(nums[i], i);
}
return null;
}

📙175. 组合两个表

法一:左联表

1
2
3
4
# Write your MySQL query statement below
select FirstName,LastName,City,State
from Person left join Address
on Person.PersonId = Address.PersonId;

✨2024-12-28

📙2. 两数相加

法一:合并两数且遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head = new ListNode();
ListNode tail = head;
while (l1 != null && l2 != null) {
ListNode node = new ListNode(l1.val + l2.val);
tail.next = node;
tail = tail.next;
l1 = l1.next;
l2 = l2.next;
}
if (l1 != null) {
tail.next = l1;
}
if (l2 != null) {
tail.next = l2;
}
tail = head.next;
int add = 0,sum = 0;
ListNode tailtmp = head;
while (tail != null) {
sum = tail.val + add;
tail.val = sum % 10;
add = sum / 10;
tailtmp = tail;
tail = tail.next;
}

if (add > 0) {
tailtmp.next = new ListNode(add);
}
head = head.next;
return head;
}

📙176. 第二高薪水

法一:排除最高薪水再次搜索最大薪水

1
2
3
4
5
# Write your MySQL query statement below
select MAX(salary) as `SecondHighestSalary`
from Employee
where salary<(select MAX(salary)
from Employee);

法二:排序后取第二高薪水

1
2
3
4
5
6
# Write your MySQL query statement below
select (select distinct salary
from Employee
order by salary desc
limit 1 offset 1
) as `SecondHighestSalary`;

✨2024-12-29

📙3. 无重复字符的最长字串

法一:通过HashSet判断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public int lengthOfLongestSubstring(String s) {
if (s == null || s.length() == 0) return 0;
HashSet<Character> chars = new HashSet<Character>();
int max = 1, tmp = 0;
for (int l = 0; l < s.length() - 1; l++) {
for (int r = l; r < s.length(); r++) {
if (chars.contains(s.charAt(r))) {
tmp = 0;
chars.clear();
break;
} else {
chars.add(s.charAt(r));
tmp++;
if (tmp > max) {
max = tmp;
}
}
}
}
return max;
}

📙177. 第N高薪水

法一:排序后获取第N高薪水

1
2
3
4
5
6
7
8
9
10
11
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
set N := N-1;
RETURN (
# Write your MySQL query statement below.
select distinct salary
from Employee
order by salary desc
limit 1 offset N
);
END

✨2024-12-30

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

法一:合并并排序后找中位数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int[] nums = new int[nums1.length + nums2.length];
for (int i = 0; i < nums1.length; i++) {
nums[i] = nums1[i];
}
for (int i = 0; i < nums2.length; i++) {
nums[nums1.length + i] = nums2[i];
}
Arrays.sort(nums);
int len = nums.length;
if (len % 2 == 0) {
return (nums[len / 2 - 1] + nums[len / 2]) / 2.0;
} else {
return nums[len / 2];
}
}

📙178. 分数排名

法一:使用dense_rank()排序

1
2
3
# Write your MySQL query statement below
select score, dense_rank() over (order by score desc) as 'rank'
from Scores;

收获:

函数 相同值处理 排名跳跃
ROW_NUMBER() 每行唯一编号
RANK() 相同值相同排名,后续跳过
DENSE_RANK() 相同值相同排名,后续不跳过
NTILE(n) 将数据划分为 n 组

✨2024-12-31

📙5. 最长回文子串

法一:从中间向两边遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public String longestPalindrome(String s) {
int max = 0,start = 0;
int l,r;
for (int i = 0; i < s.length(); i++) {

//aba 单数
l = i;
r = i;
while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
if (r - l + 1 > max) {
max = r - l + 1;
start = l;
}
l--;
r++;
}

//abba 双数
l = i;
r = i + 1;
while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
if (r - l + 1 > max) {
max = r - l + 1;
start = l;
}
l--;
r++;
}
}
return s.substring(start, start + max);
}

📙180. 连续出现的数字

法一:内连接

1
2
3
4
5
6
7
# Write your MySQL query statement below
select distinct l1.num as ConsecutiveNums
from Logs l1,Logs l2,Logs l3
where l1.id-1 = l2.id
and l2.id-1 = l3.id
and l1.num = l2.num
and l2.num = l3.num;

✨2025-1-1

📙6. Z字形变换

法一:根据字形形成的数据创建集合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public String convert(String s, int numRows) {
if (numRows == 1) return s;

int divisor = (numRows - 1) * 2;
ArrayList<StringBuilder> list = new ArrayList<>();
for (int i = 0; i < numRows; i++) {
list.add(new StringBuilder());
}
for (int i = 0; i < s.length(); i++) {
if (i % divisor < numRows) {
list.get(i % divisor).append(s.charAt(i));
} else {
list.get((numRows - 1) - (i % divisor - (numRows - 1))).append(s.charAt(i));
}
}
StringBuilder string = new StringBuilder();
for (int i = 0; i < numRows; i++) {
string.append(list.get(i));
}
return string.toString();
}

📙181. 超过经理收入的员工

法一:连表查询

1
2
3
4
5
# Write your MySQL query statement below
select e1.name as 'Employee'
from Employee e1
join Employee e2 on e1.managerId=e2.id
where e2.salary < e1.salary;

法二:子查询

1
2
3
4
5
6
7
# Write your MySQL query statement below
select name as 'Employee'
from Employee e1
where managerId is not null
and salary > (select salary
from Employee e2
where e1.managerId = e2.id);

✨2025-1-2

📙7. 整数反转

法一:移动尾数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public int reverse(int x) {
int sum = 0, tmp = 0;
while (x != 0) {
tmp = x % 10;
if (sum > 214748364 || (sum == 214748364 && tmp > 7)) {
return 0;
}
if (sum < -214748364 || (sum == -214748364 && tmp < -8)) {
return 0;
}
sum = sum * 10 + tmp;
x /= 10;
}
return sum;
}

📙182. 查找重复的电子邮箱

法一:使用Having,group by

1
2
3
4
5
# Write your MySQL query statement below
select email as Email
from Person
group by email
having COUNT(id) >= 2;

✨2025-1-3

📙8. 字符串转换整数 (atoi)

法一:去空格后遍历,之后判断是否超出范围

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public int myAtoi(String s) {
if (s == null || s.equals("")) {
return 0;
}
s = s.trim();
boolean negative = false;
if (s.length() >= 1 && (s.charAt(0) == '-' || s.charAt(0) == '+')) {
negative = s.charAt(0) == '-';
s = s.substring(1, s.length());
}
if (s.length() < 1 || '0' > s.charAt(0) || s.charAt(0) > '9') {
return 0;
}
int i = 0,result = 0;
while (i < s.length() && '0' <= s.charAt(i) && s.charAt(i) <= '9') {
int tmp = s.charAt(i) - '0';
if (negative) tmp *= -1;
if (result > Integer.MAX_VALUE / 10 || (result == Integer.MAX_VALUE / 10 && tmp > 7)) {
return Integer.MAX_VALUE;
}
if (result < Integer.MIN_VALUE / 10 || (result == Integer.MIN_VALUE / 10 && tmp < -8)) {
return Integer.MIN_VALUE;
}
result = result * 10 + tmp;
i++;
}
return result;
}

📙183. 从不订购的用户

法一:子查询

1
2
3
4
5
# Write your MySQL query statement below
select name as 'Customers'
from Customers
where id not in (select customerId
from Orders);

法二:连表查询

1
2
3
4
5
# Write your MySQL query statement below
select name as 'Customers'
from Customers
left join Orders on Customers.id = Orders.customerId
where Orders.customerId is null

✨2025-1-4

📙9. 回文数

法一:转换为字符数组,左右对比

1
2
3
4
5
6
7
8
9
10
11
12
13
public boolean isPalindrome(int x) {
if (x < 0) return false;
char[] chars = String.valueOf(x).toCharArray();
int left = 0, right = chars.length - 1;
while (left < right) {
if (chars[left] != chars[right]) {
return false;
}
left++;
right--;
}
return true;
}

法二:获取原数的回文数,判断两数是否相同

1
2
3
4
5
6
7
8
9
public boolean isPalindrome(int x) {
if (x < 0) return false;
int sum = 0, tmp = x;
while (tmp != 0) {
sum = sum * 10 + tmp % 10;
tmp = tmp / 10;
}
return sum == x;
}

📙184. 部门工资最高的员工

法一:连表查询

1
2
3
4
5
6
# Write your MySQL query statement below
select Department.name as 'Department',sub_quary.name as 'Employee',salary as 'Salary'
from (select *,dense_rank() over (partition by departmentId order by salary desc) as salaryId
from Employee) as sub_quary
left join Department on Department.id = sub_quary.departmentId
where salaryId = 1;
1
2
3
4
5
6
7
# Write your MySQL query statement below
select Department.name 'Department',Employee.name 'Employee',salary 'Salary'
from Employee,Department
where Employee.departmentId = Department.id
and (Employee.departmentId, salary) in (select departmentId,MAX(salary)
from Employee
group by departmentId);

✨2025-1-5

📙13. 罗马数字转整数

法一:前数大于后数则加,反之则减

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public int romanToInt(String s) {
int pre = getInt(s.charAt(0)), num, sum = 0;
for (int i = 1; i < s.length(); i++) {
num = getInt(s.charAt(i));
if (pre < num) {
sum -= pre;
} else {
sum += pre;
}
pre = num;
}
sum += pre;
return sum;
}

private int getInt(char c) {
switch (c) {
case 'I':
return 1;
case 'V':
return 5;
case 'X':
return 10;
case 'L':
return 50;
case 'C':
return 100;
case 'D':
return 500;
case 'M':
return 1000;
default:
return 0;
}
}

📙185. 部门工资前三高的所有员工

法一:连表查询

1
2
3
4
5
6
# Write your MySQL query statement below
select Department.name as 'Department',sub_quary.name as 'Employee',salary as 'Salary'
from (select *,dense_rank() over (partition by departmentId order by salary desc) as salaryId
from Employee) as sub_quary
left join Department on Department.id = sub_quary.departmentId
where salaryId <= 3;

✨2025-1-6

📙12. 整数转罗马数字

法一:罗列数字,从大到小转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public String intToRoman(int num) {
StringBuilder s = new StringBuilder();
while (true) {
if (num >= 1000) {
s.append("M");
num -= 1000;
} else if (num >= 900) {
s.append("CM");
num -= 900;
} else if (num >= 500) {
s.append("D");
num -= 500;
} else if (num >= 400) {
s.append("CD");
num -= 400;
} else if (num >= 100) {
s.append("C");
num -= 100;
} else if (num >= 90) {
s.append("XC");
num -= 90;
} else if (num >= 50) {
s.append("L");
num -= 50;
} else if (num >= 40) {
s.append("XL");
num -= 40;
} else if (num >= 10) {
s.append("X");
num -= 10;
} else if (num >= 9) {
s.append("IX");
num -= 9;
} else if (num >= 5) {
s.append("V");
num -= 5;
} else if (num >= 4) {
s.append("IV");
num -= 4;
} else if (num >= 1) {
s.append("I");
num -= 1;
} else {
break;
}
}
return s.toString();
}

📙196. 删除重复的电子邮箱

法一:子查询

1
2
3
4
5
6
7
# Write your MySQL query statement below
delete
from Person
where id in (select id
from (select id,row_number() over (partition by email order by id) as mrank
from Person) as sub_quary
where mrank != 1);

✨2025-1-7

📙19. 删除链表的倒数第 N 个结点

法一:遍历找到结点总数,再找倒数第n个节点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode tmp = head;
int len = 0;
while (tmp != null) {
tmp = tmp.next;
len++;
}
tmp = head;
if (len - n - 1 < 0) {
head = head.next;
tmp = head;
} else {
for (int i = 0; i < len - n - 1; i++) {
tmp = tmp.next;
}
tmp.next = tmp.next.next;
}
return head;
}

📙197. 上升的温度

法一:连表查询

1
2
3
4
5
# Write your MySQL query statement below
select d1.id
from Weather d1,Weather d2
where datediff(d1.recordDate, d2.recordDate) = 1
and d1.temperature > d2.temperature;

✨2025-1-8

📙20. 有效的括号

法一:使用栈

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public boolean isValid(String s) {
char[] chars = s.toCharArray();
Stack<Character> stack = new Stack<>();
for (char c : chars) {
if (c == '(' || c == '{' || c == '[') {
stack.push(c);
}
else if (c == ')' || c == '}' || c == ']') {
if (stack.isEmpty()) {
return false;
}
if (c == ')') {
if (stack.peek() == '(') {
stack.pop();
} else {
return false;
}
} else if (c == '}') {
if (stack.peek() == '{') {
stack.pop();
} else {
return false;
}
} else if (c == ']') {
if (stack.peek() == '[') {
stack.pop();
} else {
return false;
}
}
}
}
return stack.isEmpty();
}

📙511. 游戏玩法分析 I

法一:子查询

1
2
3
4
5
# Write your MySQL query statement below
select player_id,event_date as first_login
from (select player_id,event_date,row_number() over (partition by player_id order by event_date) as p_rank
from Activity) as sub_quary
where p_rank = 1;

✨2025-1-9

📙21. 合并两个有序链表

法一:遍历两个链表,依次加入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
if (list1 == null) return list2;
if (list2 == null) return list1;

ListNode head, tmp;
if (list1.val > list2.val) {
head = list2;
tmp = list2;
list2 = list2.next;
} else {
head = list1;
tmp = list1;
list1 = list1.next;
}

while (list1 != null && list2 != null) {
if (list1.val > list2.val) {
tmp.next = list2;
tmp = tmp.next;
list2 = list2.next;
} else {
tmp.next = list1;
tmp = tmp.next;
list1 = list1.next;
}
}
if (list1 != null) {
tmp.next = list1;
}
if (list2 != null) {
tmp.next = list2;
}
return head;
}

📙577. 员工奖金

法一:连表查询

1
2
3
4
5
6
# Write your MySQL query statement below
select name,bonus
from Employee
left join Bonus on Employee.empId = Bonus.empId
where bonus < 1000
or bonus is null;

✨2025-1-10

📙26. 删除有序数组中的重复项

法一:遍历,通过计数改变原有项

1
2
3
4
5
6
7
8
9
10
public int removeDuplicates(int[] nums) {
int count = 1, tmp = nums[0];
for (int i = 0; i < nums.length; i++) {
if (nums[i] != tmp) {
tmp = nums[i];
nums[count++] = nums[i];
}
}
return count;
}

📙584. 寻找用户推荐人

法一:is null

1
2
3
4
5
# Write your MySQL query statement below
select name
from Customer
where referee_id is null
or referee_id != 2;

✨2025-1-11

📙27. 移除元素

法一:遍历删除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public int removeElement(int[] nums, int val) {
int count = 0, len = nums.length;
for (int i = 0; i < len; i++) {
if (nums[i] == val) {
for (int j = i; j < nums.length - 1; j++) {
nums[j] = nums[j + 1];
}
len--;
i--;
} else {
count++;
}
}
return count;
}

📙570. 至少有5名直接下属的经理

法一:连表查询

1
2
3
4
5
6
# Write your MySQL query statement below
select e1.name
from Employee e1
left join Employee e2 on e1.id = e2.managerId
group by e1.id
having COUNT(e1.id) >= 5;

✨2025-1-12

📙28. 找出字符串中第一个匹配项的下标

法一:遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public int strStr(String haystack, String needle) {
if (haystack == null || needle == null || haystack.length() < needle.length()) {
return -1;
}

boolean found;
for (int i = 0; i < haystack.length() - needle.length() + 1; i++) {
found = false;
for (int j = 0, hindex = i; j < needle.length() && hindex < haystack.length(); j++, hindex++) {
if (haystack.charAt(hindex) == needle.charAt(j)) {
found = true;
} else {
found = false;
break;
}
}
if (found) {
return i;
}
}
return -1;
}

优化:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public int strStr(String haystack, String needle) {
int hlen = haystack.length(), nlen = needle.length();
char[] h = haystack.toCharArray(), n = needle.toCharArray();
for (int i = 0; i <= hlen - nlen; i++) {
int hindex = i, nindex = 0;
while (nindex < nlen && h[hindex] == n[nindex]) {
hindex++;
nindex++;
}
if (nindex == nlen) {
return i;
}
}
return -1;
}

📙586. 订单最多的客户

法一:子查询

1
2
3
4
5
6
7
# Write your MySQL query statement below
select customer_number
from (select customer_number,COUNT(order_number) as nums
from Orders
group by customer_number
order by nums desc) as sub_quary
limit 1;

法二:普通查询

1
2
3
4
5
6
# Write your MySQL query statement below
select customer_number
from Orders
group by customer_number
order by COUNT(order_number) desc
limit 1;

✨2025-1-13

📙35. 搜索插入位置

法一:二分查找

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public int searchInsert(int[] nums, int target) {
return search(nums, target, 0, nums.length - 1);
}

public int search(int[] nums, int target, int start, int end) {
int mid = (start + end) / 2;
if (nums[mid] == target) {
return mid;
}
if (start == end) {
if (start == 0) {
if (nums[start] < target){
return 1;
} else {
return 0;
}
} else if (start == nums.length - 1) {
if (nums[start] < target) {
return start + 1;
} else {
return start;
}
} else {
return mid;
}
}
if (nums[mid] < target) {
return search(nums, target, mid + 1, end);
}
return search(nums, target, start, mid);
}

📙595. 大的国家

法一:普通查找

1
2
3
4
5
# Write your MySQL query statement below
select World.name,World.population,World.area
from World
where World.area >= 3000000
or World.population >= 25000000;

✨2025-1-14

📙58. 最后一个单词的长度

法一:遍历计数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public int lengthOfLastWord(String s) {
int len = 0, max = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ' ') {
if (len > 0) {
max = len;
}
len = 0;
} else {
len++;
}
}
if (len > 0) {
max = len;
}
return max;
}

📙596. 超过5名学生的课

法一:普通查找

1
2
3
4
5
# Write your MySQL query statement below
select class
from Courses
group by class
having COUNT(student) >= 5;

✨2025-1-15

📙53. 最大子数组和

法一:动态规划

1
2
3
4
5
6
7
8
public int maxSubArray(int[] nums) {
int preSum = 0, maxSum = nums[0];
for (int num : nums) {
preSum = Math.max(preSum + num, num);
maxSum = Math.max(maxSum, preSum);
}
return maxSum;
}

📙607. 销售员

法一:嵌套查询

1
2
3
4
5
6
7
8
# Write your MySQL query statement below
select name
from SalesPerson
where sales_id not in (select sales_id
from Orders
where com_id in (select com_id
from Company
where name = 'RED'));

✨2025-1-16

📙54. 螺旋矩阵

法一:设置4个标识后按顺序遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public List<Integer> spiralOrder(int[][] matrix) {
ArrayList<Integer> arrayList = new ArrayList<>();
int l = 0, r = matrix[0].length - 1;
int h = 0, t = matrix.length - 1;
while ((l <= r) && (h <= t)) {
if (h <= t) {
for (int i = l; i <= r; i++) {
arrayList.add(matrix[h][i]);
}
h++;
}
if (l <= r) {
for (int i = h; i <= t; i++) {
arrayList.add(matrix[i][r]);
}
r--;
}
if (h <= t) {
for (int i = r; i >= l; i--) {
arrayList.add(matrix[t][i]);
}
t--;
}
if (l <= r) {
for (int i = t; i >= h; i--) {
arrayList.add(matrix[i][l]);
}
l++;
}
}
return arrayList;
}

📙610. 判断三角形

法一:使用if判断

1
2
3
# Write your MySQL query statement below
select *,if(((x + y) > z and (x + z) > y and (y + z) > x),'Yes','No') as triangle
from Triangle;

✨2025-1-17

📙59. 螺旋矩阵 II

法一:同螺旋矩阵,设置标识后遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public int[][] generateMatrix(int n) {
int num = 1;
int[][] matrix = new int[n][n];
int l = 0, r = n - 1;
int h = 0, t = n - 1;
while ((l <= r) && (h <= t)) {
for (int i = l; i <= r; i++) {
matrix[h][i] = num++;
}
h++;
for (int i = h; i <= t; i++) {
matrix[i][r] = num++;
}
r--;
for (int i = r; i >= l; i--) {
matrix[t][i] = num++;
}
t--;
for (int i = t; i >= h; i--) {
matrix[i][l] = num++;
}
l++;
}
return matrix;
}

📙619. 只出现一次的最大数字

法一:子查询

1
2
3
4
5
6
7
8
# Write your MySQL query statement below
select (select num
from MyNumbers
group by num
having COUNT(*) = 1
order by num desc
limit 1
) num;

✨2025-1-18

📙61. 旋转链表

法一:将单向连表变为螺旋链表再切断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public ListNode rotateRight(ListNode head, int k) {
if (head == null || k == 0) {
return head;
}

int len = 0;
ListNode tmp = head;
while (tmp != null) {
tmp = tmp.next;
len++;
}

tmp = head;
while (tmp.next != null) {
tmp = tmp.next;
}
tmp.next = head;

k = k % len;
tmp = head;
for (int i = 0; i < len - k - 1; i++) {
tmp = tmp.next;
}
head = tmp.next;
tmp.next = null;

return head;
}

📙620. 有趣的电影

法一:普通查找

1
2
3
4
5
6
# Write your MySQL query statement below
select *
from cinema
where description != 'boring'
and id % 2 != 0
order by rating desc;

✨2025-1-19

📙66. 加一

法一:加一后遍历,如果最高位加一则新建数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public int[] plusOne(int[] digits) {
digits[digits.length - 1]++;
for (int i = digits.length - 1; i > 0; i--) {
if (digits[i] >= 10) {
digits[i] %= 10;
digits[i - 1] += 1;
}
}
if (digits[0] >= 10) {
digits[0] %= 10;
int[] ints = new int[digits.length + 1];
ints[0] = 1;
for (int i = 0; i < digits.length; i++) {
ints[i + 1] = digits[i];
}
return ints;
}
return digits;
}

📙627. 变更性别

法一:使用条件判断语句

1
2
3
4
5
6
# Write your MySQL query statement below
update Salary
set sex = (case sex
when 'm' then 'f'
else 'm'
end);

✨2025-1-20

📙67. 二进制求和

法一:首部不够则添0,遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
public String addBinary(String a, String b) {
StringBuilder str = new StringBuilder();
int carry = 0;
for (int i = a.length() - 1, j = b.length() - 1; i >= 0 || j >= 0; i--, j--) {
int sum = carry;
sum += i >= 0 ? a.charAt(i) - '0' : 0;
sum += j >= 0 ? b.charAt(j) - '0' : 0;
str.append(sum % 2);
carry = sum / 2;
}
str.append(carry == 1 ? carry : "");
return str.reverse().toString();
}

📙1050. 合作过至少三次的演员和导演

法一:通过 group 普通查找

1
2
3
4
5
# Write your MySQL query statement below
select actor_id,director_id
from ActorDirector
group by actor_id,director_id
having COUNT(*) >= 3;

✨2025-1-21

📙69. x 的平方根

法一:袖珍计算器算法

1
2
3
4
5
6
7
8
public int mySqrt(int x) {
if (x == 0 || x == 1) {
return x;
}

int result = (int) Math.exp(0.5 * Math.log(x));
return (long) (result + 1) * (result + 1) <= x ? result + 1 : result;
}

📙1068. 产品销售分析 I

法一:连表查询

1
2
3
4
# Write your MySQL query statement below
select product_name,Sales.year,price
from Sales
left join Product on Sales.product_id = Product.product_id;

✨2025-1-22

📙73. 矩阵置零

法一:遍历存位置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public void setZeroes(int[][] matrix) {
ArrayList<ArrayList<Integer>> list = new ArrayList<>();
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] == 0) {
ArrayList<Integer> subList = new ArrayList<>();
subList.add(i);
subList.add(j);
list.add(subList);
}
}
}
for (int z = 0; z < list.size(); z++) {
ArrayList<Integer> integers = list.get(z);
int row = integers.get(0);
int col = integers.get(1);
for (int i = 0; i < matrix[0].length; i++) {
matrix[row][i] = 0;
}
for (int i = 0; i < matrix.length; i++) {
matrix[i][col] = 0;
}
}
}

优化空间复杂度:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void setZeroes(int[][] matrix) {
int m = matrix.length, n = matrix[0].length;
boolean[] row = new boolean[m];
boolean[] col = new boolean[n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == 0) {
row[i] = col[j] = true;
}
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (row[i] || col[j]) {
matrix[i][j] = 0;
}
}
}
}

优化遍历方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public void setZeroes(int[][] matrix) {
int colLen = matrix.length, rowLen = matrix[0].length;
boolean col0Flag = false, row0Flag = false;
for (int i = 0; i < colLen; i++) {
if (matrix[i][0] == 0) {
col0Flag = true;
}
}
for (int j = 0; j < rowLen; j++) {
if (matrix[0][j] == 0) {
row0Flag = true;
}
}
for (int i = 1; i < colLen; i++) {
for (int j = 1; j < rowLen; j++) {
if (matrix[i][j] == 0) {
matrix[i][0] = matrix[0][j] = 0;
}
}
}
for (int i = 1; i < colLen; i++) {
for (int j = 1; j < rowLen; j++) {
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
matrix[i][j] = 0;
}
}
}
if (col0Flag) {
for (int i = 0; i < colLen; i++) {
matrix[i][0] = 0;
}
}
if (row0Flag) {
for (int i = 0; i < rowLen; i++) {
matrix[0][i] = 0;
}
}
}

📙1075. 项目员工 I

法一:通过聚合函数计算

1
2
3
4
5
# Write your MySQL query statement below
select project_id,ROUND(AVG(experience_years), 2) average_years
from Project
left join Employee on Project.employee_id = Employee.employee_id
group by project_id;