feat(redo): 添加新的练习代码和解决方案

- 新增 LeetCode 69、135、376、763题目的解决方案
- 添加面试题练习代码
- 新建 SQL 查询练习题 stu,列 name, score, course,查询每一科分数大于 60 的学生姓名
This commit is contained in:
whaifree 2024-10-22 19:38:49 +08:00
parent d9e64a81a6
commit 72843c9027
6 changed files with 376 additions and 0 deletions

View File

@ -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));
}
}

View File

@ -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();
}
}
}

View File

@ -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]);
}
}
}

View File

@ -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;
}
}
}

View File

@ -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<Integer> ans = new Solution().partitionLabels(s);
System.out.println(ans);
}
class Solution {
public List<Integer> partitionLabels(String s) {
Map<Character, Integer> lastIndex = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
lastIndex.put(s.charAt(i), i);
}
List<Integer> 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;
}
}
}

24
sql/looking/241022_1.sql Normal file
View File

@ -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;