2024/09/30 完成hot100
This commit is contained in:
parent
d1c11740b1
commit
07fc2578e1
|
@ -0,0 +1,31 @@
|
|||
package com.cool.hot100.strange_skills_cunning;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
*
|
||||
* @Author: Cool
|
||||
* @Date: 2024/09/30/16:46
|
||||
* @Description: 169. 多数元素
|
||||
* DayNumber 1
|
||||
* Hard 1
|
||||
* Level 2
|
||||
* @link https://leetcode.cn/problems/majority-element/description/?envType=study-plan-v2&envId=top-100-liked
|
||||
*/
|
||||
public class Num169 {
|
||||
public int majorityElement(int[] nums) {
|
||||
int cur=nums[0];
|
||||
int curNum=1;
|
||||
for(int i=1;i<nums.length;i++){
|
||||
if(curNum==0){
|
||||
cur=nums[i];
|
||||
}
|
||||
if(cur==nums[i]){
|
||||
curNum++;
|
||||
}else{
|
||||
curNum--;
|
||||
}
|
||||
}
|
||||
return cur;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.cool.hot100.strange_skills_cunning;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
*
|
||||
* @Author: Cool
|
||||
* @Date: 2024/09/30/20:52
|
||||
* @Description: 287. 寻找重复数
|
||||
* DayNumber 5
|
||||
* Level 6
|
||||
* Hard 2
|
||||
* @link https://leetcode.cn/problems/find-the-duplicate-number/description/?envType=study-plan-v2&envId=top-100-liked
|
||||
*/
|
||||
public class Num287 {
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
package com.cool.hot100.strange_skills_cunning;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
*
|
||||
* @Author: Cool
|
||||
* @Date: 2024/09/30/20:10
|
||||
* @Description: 31. 下一个排列
|
||||
* Level 5
|
||||
* Hard 2
|
||||
* DayNumber 3
|
||||
* @link https://leetcode.cn/problems/next-permutation/description/?envType=study-plan-v2&envId=top-100-liked
|
||||
*/
|
||||
public class Num31 {
|
||||
|
||||
public void nextPermutation(int[] nums) {
|
||||
int m = 0, n = 0;
|
||||
for (int i = nums.length - 2; i >= 0; i--) {
|
||||
if (nums[i] < nums[i + 1]) {
|
||||
m = i;
|
||||
break;
|
||||
}
|
||||
if (i == 0) {
|
||||
reverse(nums, 0, nums.length);
|
||||
return;
|
||||
|
||||
}
|
||||
}
|
||||
for (int i = nums.length - 1; i >= 0; i--) {
|
||||
if (nums[i] > nums[m]) {
|
||||
swap(nums, i, m);
|
||||
reverse(nums, m + 1, nums.length);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void swap(int[] nums, int i, int j) {
|
||||
int temp = nums[i];
|
||||
nums[i] = nums[j];
|
||||
nums[j] = temp;
|
||||
}
|
||||
|
||||
private void reverse(int[] nums, int left, int right) {
|
||||
right--;
|
||||
while (left < right) {
|
||||
swap(nums, left++, right--);
|
||||
}
|
||||
}
|
||||
@Test
|
||||
public void test(){
|
||||
nextPermutation(new int[]{3,1,2});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package com.cool.hot100.strange_skills_cunning;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA.
|
||||
*
|
||||
* @Author: Cool
|
||||
* @Date: 2024/09/30/16:50
|
||||
* @Description: 75. 颜色分类
|
||||
* Level 4
|
||||
* Hard 2
|
||||
* DayNumber 2
|
||||
* @link https://leetcode.cn/problems/sort-colors/description/?envType=study-plan-v2&envId=top-100-liked
|
||||
*/
|
||||
public class Num75 {
|
||||
|
||||
public void sortColors(int[] nums) {
|
||||
int head = 0;
|
||||
int end = nums.length - 1;
|
||||
for (int i = 0; i <= end; i++) {
|
||||
while (i <= end && nums[i] == 2) {
|
||||
int temp = nums[end];
|
||||
nums[end] = nums[i];
|
||||
nums[i] = temp;
|
||||
end--;
|
||||
}
|
||||
if (nums[i] == 0) {
|
||||
int temp = nums[head];
|
||||
nums[head] = nums[i];
|
||||
nums[i] = temp;
|
||||
head++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue