添加了LeetCode第2题、第3题和第989题的解法,并优化了第912题的排序算法。

This commit is contained in:
whaifree 2024-04-21 13:40:48 +08:00
parent 8749e5b188
commit 392278d319
5 changed files with 519 additions and 6 deletions

View File

@ -3,6 +3,7 @@ package cn.whaifree.leetCode.Array;
import org.junit.Test;
import java.util.Arrays;
import java.util.Random;
/**
*
@ -23,17 +24,22 @@ public class LeetCode912_SortArrays {
@Test
public void test() {
int[] nums = {5, 2, 3, 1,647,24,7,2,8,2,8,1,54,13,6,234,45,234,64,745,32,56,44,32,38};
int[] res = new Solution2().sortArray(nums);
System.out.println(Arrays.toString(res));
int[] nums = {5,1,1,2,0,0};
int[] ints = Arrays.copyOf(res, res.length);
Arrays.sort(nums);
System.out.println(Arrays.equals(ints, nums));
// int[] res = new Solution2().sortArray(nums);
// System.out.println(Arrays.toString(res));
//
// int[] ints = Arrays.copyOf(res, res.length);
// Arrays.sort(nums);
// System.out.println(Arrays.equals(ints, nums));
int[] res2 = new MergeSort.Solution().sortArray(nums);
System.out.println(Arrays.toString(res2));
}
class Solution {
/**
* 冒泡
@ -133,3 +139,150 @@ public class LeetCode912_SortArrays {
}
}
class MergeSort{
static class Solution {
// 归并排序
public int[] sortArray(int[] nums) {
if (nums.length <= 1) {
return nums;
}
int mid = nums.length / 2;
int[] left = Arrays.copyOfRange(nums, 0, mid);
int[] right = Arrays.copyOfRange(nums, mid, nums.length);
left = sortArray(left);
right = sortArray(right);
return merge(left, right);
}
public int[] merge(int[] left, int[] right) {
int[] res = new int[left.length + right.length];
int leftIndex = 0;
int rightIndex = 0;
int index = 0;
while (leftIndex < left.length && rightIndex < right.length) {
if (left[leftIndex] < right[rightIndex]) {
res[index] = left[leftIndex];
leftIndex++;
}else {
res[index] = right[rightIndex];
rightIndex++;
}
index++;
}
while (leftIndex < left.length) {
res[index] = left[leftIndex];
leftIndex++;
index++;
}
while (rightIndex < right.length) {
res[index] = right[rightIndex];
rightIndex++;
index++;
}
return res;
}
}
}
class QuickSort{
/**
* 快速排序
*/
static class Solution4 {
public int[] sortArray(int[] nums) {
quickSort(nums, 0, nums.length-1);
return nums;
}
public int[] quickSort(int[] nums, int left,int right){
//如果左指针大于右指针怎退出循环
if(left > right){
return null;
}
//! 随机挑选一个幸运儿
int q = new Random().nextInt(right - left + 1) + left;
swap(nums, right, q);
int base = nums[left];
int i = left;
int j = right;
while(i != j){
//从右往左遍历当右指针指向的元素大于等于基数时j--右指针持续向左移动
while(nums[j]>=base && i < j){
j--;
}
//从左往右遍历当左指针指向的元素小于等于基数时i++左指针持续向右移动
while(nums[i]<=base && i < j){
i++;
}
//当左右两个指针停下来时交换两个元素
swap(nums, i, j);
}
swap(nums,i,left);
quickSort(nums,left, i-1);
quickSort(nums,i+1,right);
return nums;
}
public void swap(int[] nums, int index1, int indexB) {
int tmp = nums[index1];
nums[index1] = nums[indexB];
nums[indexB] = tmp;
}
}
class Solution11 {
public int[] sortArray(int[] nums) {
quick_sort(nums, 0, nums.length - 1);
return nums;
}
private void quick_sort(int[] nums, int left, int right) {
if (left >= right) {
return;
}
int i = left;
int j = right;
//! 随机挑选一个幸运儿
int q = new Random().nextInt(right - left + 1) + left;
swap(nums, right, q);
while (i < j) {
while (nums[i] <= nums[right] && i < j) {
i++;
}
while (nums[right] >= nums[left] && i < j) {
j--;
}
swap(nums, i, j);
}
swap(nums, i, right);
quick_sort(nums, left, i - 1);
quick_sort(nums, i + 1, right);
}
private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}

View File

@ -0,0 +1,98 @@
package cn.whaifree.leetCode.Array;
import org.junit.Test;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/4/20 12:41
* @注释
*/
public class LeetCode989 {
@Test
public void test() throws IOException {
// 创建一个RandomAccessFile对象用于读取指定路径下的文件
RandomAccessFile reader = new RandomAccessFile("D:\\Downloads\\a8d88f60-603e-405f-b67b-6774dd14a507.jpg", "r");
// 获取文件通道
FileChannel channel = reader.getChannel();
// 分配读取指定文件大小的缓冲区
ByteBuffer buffer = ByteBuffer.allocate((int) reader.length());
// 从文件通道中读取数据到缓冲区
channel.read(buffer);
// 创建一个RandomAccessFile对象用于将缓冲区的数据写入到新的文件
RandomAccessFile writer = new RandomAccessFile("D:\\Downloads\\new_file.jpg", "rw");
// 获取文件通道
FileChannel newChannel = writer.getChannel();
// 将缓冲区的数据写入到新的文件
newChannel.write(buffer);
// 关闭文件通道
channel.close();
newChannel.close();
byte[] array = ByteBuffer.allocate(8).putDouble(1.0).array();
System.out.println(Arrays.toString(array));
int[] num = {9,9,9,9,9,9,9,9,9,9};
int k = 1;
Solution solution = new Solution();
List<Integer> res = solution.addToArrayForm(num, k);
System.out.println(res);
}
class Solution {
public List<Integer> addToArrayForm(int[] num, int k) {
int indexA = num.length - 1;
int m = k;
boolean flag = false;
LinkedList<Integer> res = new LinkedList<>();
while (indexA >= 0 && m > 0) {
int sum = num[indexA] + m % 10;
sum = flag ? sum + 1 : sum;
flag = sum >= 10;
res.addFirst(sum % 10);
indexA--;
m /= 10;
}
if (indexA < 0 && m <= 0 && flag) {
res.addFirst(1);
return res;
}
while (indexA >= 0) {
int sum = num[indexA];
sum = flag ? sum + 1 : sum;
flag = sum >= 10;
res.addFirst(sum % 10);
indexA--;
}
while (m>0) {
int sum = m % 10;
sum = flag ? sum + 1 : sum;
flag = sum >= 10;
res.addFirst(sum % 10);
m /= 10;
}
if (flag) {
res.addFirst(1);
}
return res;
}
}
}

View File

@ -0,0 +1,121 @@
package cn.whaifree.leetCode.LinkedList;
import cn.whaifree.leetCode.model.ListNode;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/4/20 11:42
* @注释
*/
public class LeetCode2 {
@Test
public void test() {
new Solution1().addTwoNumbers(
/*[9,9,9,9,9,9,9], l2 = [9,9,9,9]*/
ListNode.listNodeFromArray(new int[]{9, 9, 9, 9, 9, 9, 9})
, ListNode.listNodeFromArray(new int[]{9, 9, 9, 9})
).printList();
}
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
return circle(l1, l2, false);
}
/**
*
* @param l1
* @param l2
* @param in 是否进1
* @return
*/
public ListNode circle(ListNode l1, ListNode l2, boolean in) {
if (l1 == null && l2 == null && in) {
return new ListNode(1);
}
if (l1 == null) {
// l2 调用另一个递归方法不断去判断是否需要在当前节点进1
return judgeIn(l2, in);
} else if (l2 == null) {
return judgeIn(l1, in);
}
int val = 0;
int sum = l1.val + l2.val;
if (in) {
val = sum + 1;
}else {
val = sum;
}
ListNode res = new ListNode(val % 10);
res.next = circle(l1.next, l2.next, val >= 10);
return res;
}
public ListNode judgeIn(ListNode node, boolean in) {
if (node == null) {
if (in) {
return new ListNode(1);
}
return null;
}
int val = 0;
if (in) {
val = node.val + 1;
}else {
val = node.val;
}
ListNode res = new ListNode(val % 10);
res.next = judgeIn(node.next, val >= 10);
return res;
}
}
class Solution1 {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode indexA = l1;
ListNode indexB = l2;
boolean flagIn = false;
while (indexA != null && indexB != null) {
int sum = indexA.val + indexB.val;
if (flagIn) {
sum += 1;
}
indexA.val = sum % 10;
if (sum >= 10) {
flagIn = true;
}else {
flagIn = false;
}
if (indexA.next == null && indexB.next == null && flagIn) {
indexA.next = new ListNode(1);
break;
}
if (indexA.next == null && indexB.next != null) {
indexA.next = new ListNode(0);
}
if (indexB.next == null && indexA.next != null) {
indexB.next = new ListNode(0);
}
indexB = indexB.next;
indexA = indexA.next;
}
return l1;
}
}
}

View File

@ -0,0 +1,91 @@
package cn.whaifree.leetCode.String;
import org.junit.Test;
import java.util.Set;
import java.util.TreeSet;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/4/21 11:30
* @注释
*/
public class LeetCode3 {
@Test
public void test() {
String s = "abcabcbb";
System.out.println(new Solution1().lengthOfLongestSubstring(s));
}
class Solution {
public int lengthOfLongestSubstring(String s) {
if (s.length() == 0) {
return 0;
}
// s 由英文字母数字符号和空格组成
Set<Character> set = new TreeSet<>(); // 保持原来的顺序
int right = 0;
int left = 0;
int maxLength = 0;
while (right < s.length()) {
char c = s.charAt(right);
if (set.contains(c)) {
maxLength = Math.max(maxLength, right - left);
while (set.contains(c)) {
set.remove(s.charAt(left));
left++;
}
}
set.add(c);
right++;
}
return Math.max(set.size(), maxLength);
}
}
class Solution1 {
/**
* <img src="http://42.192.130.83:9000/picgo/imgs/image-20240421115141359.png">
* @param s
* @return
*/
public int lengthOfLongestSubstring(String s) {
if (s.length() == 0) {
return 0;
}
boolean[] map = new boolean[95];
// s 由英文字母数字符号和空格组成
// Set<Character> set = new TreeSet<>(); // 保持原来的顺序
int right = 0;
int left = 0;
int maxLength = 0;
while (right < s.length()) {
char c = s.charAt(right);
if (map[c - 32]) {
maxLength = Math.max(maxLength, right - left);
while (map[c - 32]) {
map[s.charAt(left) - 32] = false;
left++;
}
}
map[c - 32] = true;
right++;
}
int mapSize= 0;
for (boolean b : map) {
if (b) {
mapSize++;
}
}
return Math.max(mapSize, maxLength);
}
}
}

View File

@ -0,0 +1,50 @@
package cn.whaifree.test;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/4/20 16:31
* @注释
*/
public class DePattern {
public static void main(String[] args) throws FileNotFoundException {
// 创建一个 FileInputStream 对象用于读取文件
FileInputStream fileInputStream = new FileInputStream("file");
// BufferedInputStream 装饰器类
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
new RobotDecorator(new FirstRobot()).doMoreThing();
}
}
interface Robot {
public void doMoreThing();
}
class FirstRobot implements Robot {
public void doMoreThing()
{
System.out.println("初始机器人的功能:唱歌");
}
}
class RobotDecorator {
private Robot robot;
public RobotDecorator(Robot robot)
{
this.robot = robot;
}
public void doMoreThing()
{
System.out.println("装饰器增加功能:跳舞");
robot.doMoreThing();
}
}