From 72843c90274a971d369c4522056001bc463d5bd2 Mon Sep 17 00:00:00 2001 From: whaifree Date: Tue, 22 Oct 2024 19:38:49 +0800 Subject: [PATCH] =?UTF-8?q?feat(redo):=20=E6=B7=BB=E5=8A=A0=E6=96=B0?= =?UTF-8?q?=E7=9A=84=E7=BB=83=E4=B9=A0=E4=BB=A3=E7=A0=81=E5=92=8C=E8=A7=A3?= =?UTF-8?q?=E5=86=B3=E6=96=B9=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 LeetCode 69、135、376、763题目的解决方案 - 添加面试题练习代码 - 新建 SQL 查询练习题 stu,列 name, score, course,查询每一科分数大于 60 的学生姓名 --- .../java/cn/whaifree/interview/jr/p1.java | 59 ++++++++ .../redo/redo_all_241016/LeetCode135.java | 54 ++++++++ .../redo/redo_all_241016/LeetCode376.java | 127 ++++++++++++++++++ .../redo/redo_all_241016/LeetCode69.java | 64 +++++++++ .../redo/redo_all_241016/LeetCode763.java | 48 +++++++ sql/looking/241022_1.sql | 24 ++++ 6 files changed, 376 insertions(+) create mode 100644 ForJdk17/src/main/java/cn/whaifree/interview/jr/p1.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode135.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode376.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode69.java create mode 100644 ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode763.java create mode 100644 sql/looking/241022_1.sql diff --git a/ForJdk17/src/main/java/cn/whaifree/interview/jr/p1.java b/ForJdk17/src/main/java/cn/whaifree/interview/jr/p1.java new file mode 100644 index 0000000..94a3f43 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/interview/jr/p1.java @@ -0,0 +1,59 @@ +package cn.whaifree.interview.jr; + +import java.util.Scanner; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/22 12:11 + * @注释 + */ +public class p1 { + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + + int people = in.nextInt(); + int gzNum = in.nextInt(); + int[] nums = new int[people]; + for (int i = 0; i < people; i++) { + nums[i] = in.nextInt(); + } + + int[] gzNums = new int[gzNum]; + + for (int i = 0; i < nums.length; i++) { + int want = nums[i]; + gzNums[want - 1]++; + } + + int res = 0; + for (int num : gzNums) { + int needSum = num / 2; + if (num % 2 == 1) { + res += (needSum + 1); + } else { + res += (needSum); + } + } + System.out.println(res); + } +} + + +class p2{ + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + // 注意 hasNext 和 hasNextLine 的区别 + int v0 = in.nextInt(); // 初始速度 + int x = in.nextInt(); // v=v0+t*x + int y = in.nextInt(); // 总里程 + // t1 = y / v = y / (v0+t*x) + // t = 2 / (t) t + + // 速度t 2 2/t=2.8284271 t = 2/更好8 = 根号2/2 + double t = Math.sqrt(2) / 2; + System.out.println(y / (v0 + t * x)); + + } +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode135.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode135.java new file mode 100644 index 0000000..07c0305 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode135.java @@ -0,0 +1,54 @@ +package cn.whaifree.redo.redo_all_241016; + +import org.junit.Test; + +import java.util.Arrays; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/22 16:55 + * @注释 + */ +public class LeetCode135 { + + @Test + public void test() { + int[] ratings = {1, 0, 2}; + // 1 2 3 4 3 2 1 + int result = new Solution().candy(ratings); + System.out.println(result); + } + + + class Solution { + /** + * 先全部分配1 + * + * 1 0 2 + * 1 1 1 + * 2 1 1 + * 2 1 2 + * + * @param ratings + * @return + */ + public int candy(int[] ratings) { + int[] dis = new int[ratings.length]; + Arrays.fill(dis, 1); + for (int i = 0; i < ratings.length - 1; i++) { + if (ratings[i + 1] > ratings[i]) { + dis[i + 1] = dis[i] + 1; + } + } + for (int j = ratings.length - 2; j >= 0; j--) { + if (ratings[j] > ratings[j + 1]) { + dis[j] = Math.max(dis[j], dis[j + 1] + 1); // 保留之前的 + } + } + System.out.println(Arrays.toString(dis)); + + return Arrays.stream(dis).sum(); + } + } +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode376.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode376.java new file mode 100644 index 0000000..fee5086 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode376.java @@ -0,0 +1,127 @@ +package cn.whaifree.redo.redo_all_241016; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/22 13:57 + * @注释 + */ +public class LeetCode376 { + + @Test + public void test() { + int[] nums = {1,1,7,4,9,2,5}; + Solution solution = new Solution(); + int res = solution.wiggleMaxLength(nums); + System.out.println(res); + } + + + class Solution { + + public int wiggleMaxLength(int[] nums) { + if (nums.length <= 1) { + return nums.length; + } + + int pre = 0; // 0是可以进入的 + + int right = 1; + int res = 1; // 注意第一个 + while (right < nums.length) { + int now = nums[right] - nums[right - 1]; + if (now < 0 && pre >= 0) { // 如果本次<0,上个区间>0 + res++; + pre = now; + right++; + } else if (now > 0 && pre <= 0) { // 如果本次>0,上个区间<0 + res++; + pre = now; + right++; + } else { + // 如果上一个区间和本区间都是递增或者递减 + right++; + } + } + return res; + } + +// public int wiggleMaxLength(int[] nums) { +// if (nums.length <= 1) { +// return nums.length; +// } +// +// boolean up = nums[1] - nums[0] > 0; +// +// int left = 1; +// int right = 2; +// int res = nums[1] - nums[0] != 0 ? 2 : 1; // 注意第一个 +// while (right < nums.length) { +// int now = nums[right] - nums[left]; +// if (now < 0 && up) { // 如果本次<0,上个区间>0 +// res++; +// up = !up; +// left++; +// right++; +// }else if (now > 0 && !up) { // 如果本次>0,上个区间<0 +// res++; +// up = !up; +// left++; +// right++; +// }else { +// // 如果上一个区间和本区间都是递增或者递减 +// right++; +// } +// } +// return res; +// } + + + } + + @Test + public void test1() { + int[] nums = {0,0}; + Solution1 solution1 = new Solution1 + (); + System.out.println(solution1.wiggleMaxLength(nums)); + } + + class Solution1 { + /** + * up[i] 表示0-i最长上升摆动序列的长度(可以不包括最后一个元素,只要这个序列的最后一个摆动是上升的) + * up[i-1] 不是摇摆 + * max( up[i-1], down[i-1] +1) nums[i] < nums[i-1] 表示递减,则使用前面的最长序列+1 + * 之前的序列可能更长,或者本down+1 + * down[i] 表示0-i最长下降摆动序列 + * down[i-1] + * max ( down[i-1], up[i-1]+1) + * @param nums + * @return + */ + public int wiggleMaxLength(int[] nums) { + if (nums.length <= 1) { + return nums.length; + } + int[] down = new int[nums.length]; + int[] up = new int[nums.length]; + down[0] = 1; + up[0] = 1; + for (int i = 1; i < nums.length; i++) { + if (nums[i] > nums[i - 1]) { // 递增 + down[i] = Math.max(down[i - 1], up[i - 1] + 1); + up[i] = up[i - 1]; + } else if (nums[i] < nums[i - 1]) { + up[i] = Math.max(up[i - 1], down[i - 1] + 1); + down[i] = down[i - 1]; + }else { + up[i] = up[i - 1]; + down[i] = down[i - 1]; + } + } + return Math.max(down[nums.length - 1], up[nums.length - 1]); + } + } +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode69.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode69.java new file mode 100644 index 0000000..53a4617 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode69.java @@ -0,0 +1,64 @@ +package cn.whaifree.redo.redo_all_241016; + +import org.junit.Test; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/22 17:18 + * @注释 + */ +public class LeetCode69 { + @Test + public void test() { + Solution1 solution = new Solution1(); + int i = solution.mySqrt(8); + System.out.println(i); + } + + class Solution1 { + public int mySqrt(int x) { + int left = 0; + int right = x; + while (left <= right) { + int mid = (left + right) / 2; + long pro = (long) mid * mid; + if (pro > x) { + right = mid - 1; + }else if (pro < x){ + left = mid + 1; + }else { + return mid; + } + } + return right; + } + } + + class Solution { + /** + * + * x^1/2 =k + * k^2 = x + * logk x = 2 + * ln k + * ln x = 2 + * + * ln k = 2 ln x + * e(ln k) = e2lnx = x2 + * + * @param x + * @return + */ + public int mySqrt(int x) { + double log = Math.log(x); + log /= 2; + int ans = (int) Math.exp(log); + if ((long) (ans + 1) * (ans + 1) <= x) { + return ans + 1; + } + return ans; + + } + } +} diff --git a/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode763.java b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode763.java new file mode 100644 index 0000000..f831c90 --- /dev/null +++ b/ForJdk17/src/main/java/cn/whaifree/redo/redo_all_241016/LeetCode763.java @@ -0,0 +1,48 @@ +package cn.whaifree.redo.redo_all_241016; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @version 1.0 + * @Author whai文海 + * @Date 2024/10/22 17:10 + * @注释 + */ +public class LeetCode763 +{ + + @Test + public void test() { + String s = "ababcbacadefegdehijhklij"; + List ans = new Solution().partitionLabels(s); + System.out.println(ans); + } + + class Solution { + public List partitionLabels(String s) { + Map lastIndex = new HashMap<>(); + for (int i = 0; i < s.length(); i++) { + lastIndex.put(s.charAt(i), i); + } + List ans = new ArrayList<>(); + // 在便利的过程中获取最远处 + char[] charArray = s.toCharArray(); + int left = 0; + int most = 0; + for (int i = 0; i < charArray.length; i++) { + char c = charArray[i]; + most = Math.max(most, lastIndex.get(c)); + if (most == i) { + ans.add(i - left + 1); + left = i + 1; + } + } + return ans; + } + } +} diff --git a/sql/looking/241022_1.sql b/sql/looking/241022_1.sql new file mode 100644 index 0000000..26fbe5b --- /dev/null +++ b/sql/looking/241022_1.sql @@ -0,0 +1,24 @@ +-- stu,列 name, score, course,查询每一科分数大于 60 的学生姓名 + + +-- 创建表 +create table stu( + id int primary key auto_increment, + name varchar(20), + score int, + course varchar(20) +); +-- 生成一些数据 +insert into stu(name, score, course) +values ('张三', 70, '语文'), + ('李四', 80, '语文'), + ('王五', 60, '语文'), + ('赵六', 90, '语文'); + +SELECT DISTINCT stu.name FROM stu where stu.name + not in( SELECT stu.name FROM stu WHERE stu.score<60); + +SELECT name +FROM stu +GROUP BY name +HAVING MIN(score) > 60;