当前位置: 首页 > news >正文

Java每日一练(20230324)

目录

1. 链表插入排序  🌟🌟

2. 最接近的三数之和  🌟🌟

3. 寻找旋转排序数组中的最小值  🌟🌟

🌟 每日一练刷题专栏 🌟

Golang每日一练 专栏

Python每日一练 专栏

C/C++每日一练 专栏

Java每日一练 专栏


1. 链表插入排序

对链表进行插入排序。


插入排序的动画演示如上。从第一个元素开始,该链表可以被认为已经部分排序(用黑色表示)。
每次迭代时,从输入数据中移除一个元素(用红色表示),并原地将其插入到已排好序的链表中。

插入排序算法:

  1. 插入排序是迭代的,每次只移动一个元素,直到所有元素可以形成一个有序的输出列表。
  2. 每次迭代中,插入排序只从输入数据中移除一个待排序的元素,找到它在序列中适当的位置,并将其插入。
  3. 重复直到所有输入数据插入完为止。

示例 1:

输入: 4->2->1->3
输出: 1->2->3->4

示例 2:

输入: -1->5->3->4->0
输出: -1->0->3->4->5

出处:

https://edu.csdn.net/practice/23630781

代码:

import java.util.Arrays;
public class insertionSortList {public static class ListNode {int val;ListNode next;ListNode(int x) { val = x; }}   public static ListNode createLinkedList(int[] nums) {if (nums == null || nums.length == 0) {return null;}ListNode head = new ListNode(nums[0]);ListNode cur = head;for (int i = 1; i < nums.length; i++) {cur.next = new ListNode(nums[i]);cur = cur.next;}return head;}public static void printLinkedList(ListNode head) {ListNode cur = head;while (cur != null) {System.out.print(cur.val + "->");cur = cur.next;}System.out.println("null");}public static class Solution {public ListNode insertionSortList(ListNode head) {if (head == null)return head;ListNode res = new ListNode(head.val);ListNode left = head.next;while ((left != null)) {ListNode cur = left;left = left.next;if (cur.val <= res.val) {cur.next = res;res = cur;continue;}ListNode p = res;ListNode last = p;while (p != null && p.val < cur.val) {last = p;p = p.next;}last.next = cur;last.next.next = p;}return res;}}public static void main(String[] args) {Solution s = new Solution();int[] nums = {4,2,1,3};ListNode head = createLinkedList(nums);printLinkedList(head);head = s.insertionSortList(head);printLinkedList(head);int[] nums2 = {-1,5,3,4,0};head = createLinkedList(nums2);printLinkedList(head);head = s.insertionSortList(head);printLinkedList(head);}
}

输出:

4->2->1->3->null
1->2->3->4->null
-1->5->3->4->0->null
-1->0->3->4->5->null


2. 最接近的三数之和

给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案。

示例:

输入:nums = [-1,2,1,-4], target = 1
输出:2
解释:与 target 最接近的和是 2 (-1 + 2 + 1 = 2) 。

提示:

  • 3 <= nums.length <= 10^3
  • -10^3 <= nums[i] <= 10^3
  • -10^4 <= target <= 10^4

出处:

https://edu.csdn.net/practice/23630782

代码:

import java.util.Arrays;
public class threeSumClosest {public static class Solution {public int oneSumCloset(int[] nums, int i, int j, int start, int end, int target) {if (start == i || start == j)start = start + 1;if (end == i || end == j)end = end - 1;if (start == end) {return nums[start];} else if (end == start + 1 || end == start - 1) {if (Math.abs(nums[end] - target) > Math.abs(nums[start] - target)) {return nums[start];} else {return nums[end];}} else {int middle = (int) Math.floor((start + end) / 2);if (nums[middle] > target) {end = middle;} else {start = middle;}return oneSumCloset(nums, i, j, start, end, target);}}public int threeSumClosest(int[] nums, int target) {Arrays.sort(nums);int minValue = 0;boolean hasMin = false;for (int i = 0; i < nums.length - 2; i++) {for (int j = i + 1; j < nums.length - 1; j++) {int twoSum = nums[i] + nums[j];int rest = target - twoSum;int restClost = oneSumCloset(nums, i, j, j + 1, nums.length - 1, rest);int newValue = restClost + twoSum;;if (!hasMin) {minValue = newValue;hasMin = true;} else {int d1 = Math.abs(minValue - target);int d2 = Math.abs(newValue - target);if (d1 > d2) {minValue = newValue;}}}}return minValue;}}public static void main(String[] args) {Solution s = new Solution();int[] nums = {-1,2,1,-4};System.out.println(s.threeSumClosest(nums, 1));}
}

输出:

2


3. 寻找旋转排序数组中的最小值

已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组。例如,原数组 nums = [0,1,2,4,5,6,7] 在变化后可能得到:

  • 若旋转 4 次,则可以得到 [4,5,6,7,0,1,2]
  • 若旋转 7 次,则可以得到 [0,1,2,4,5,6,7]

注意,数组 [a[0], a[1], a[2], ..., a[n-1]] 旋转一次 的结果为数组 [a[n-1], a[0], a[1], a[2], ..., a[n-2]] 。

给你一个元素值 互不相同 的数组 nums ,它原来是一个升序排列的数组,并按上述情形进行了多次旋转。请你找出并返回数组中的 最小元素 。

示例 1:

输入:nums = [3,4,5,1,2]
输出:1
解释:原数组为 [1,2,3,4,5] ,旋转 3 次得到输入数组。

示例 2:

输入:nums = [4,5,6,7,0,1,2]
输出:0
解释:原数组为 [0,1,2,4,5,6,7] ,旋转 4 次得到输入数组。

示例 3:

输入:nums = [11,13,15,17]
输出:11
解释:原数组为 [11,13,15,17] ,旋转 4 次得到输入数组。

提示:

  • n == nums.length
  • 1 <= n <= 5000
  • -5000 <= nums[i] <= 5000
  • nums 中的所有整数 互不相同
  • nums 原来是一个升序排序的数组,并进行了 1 至 n 次旋转

出处:

https://edu.csdn.net/practice/23630783

代码:

public class findMin {public static class Solution {public int findMin(int[] nums) {int low = 0, high = nums.length - 1, mid = 0;while (low <= high) {mid = (low + high) / 2;if (nums[mid] > nums[high])low = mid + 1;else if (nums[mid] < nums[high])high = mid;elsehigh--;}return nums[low];}}public static void main(String[] args) {Solution s = new Solution();int[] nums = {3,4,5,1,2};System.out.println(s.findMin(nums));int[] nums1 = {4,5,6,7,0,1,2};System.out.println(s.findMin(nums1));int[] nums2 = {11,13,15,17};System.out.println(s.findMin(nums2));}
}

输出:

1
0
11


🌟 每日一练刷题专栏 🌟

持续,努力奋斗做强刷题搬运工!

👍 点赞,你的认可是我坚持的动力! 

🌟 收藏,你的青睐是我努力的方向! 

评论,你的意见是我进步的财富!  

Golang每日一练 专栏

Python每日一练 专栏

C/C++每日一练 专栏

Java每日一练 专栏

http://www.lryc.cn/news/42327.html

相关文章:

  • 你掌握了吗?在PCB设计中,又快又准地放置元件
  • springboot学生综合测评系统
  • 【Unity3D】法线贴图和凹凸映射
  • 代码误写到master分支(或其他分支),此时代码还未提交,如何转移到新建分支?
  • java多线程之线程安全(重点,难点)
  • 如何免费使用chatGPT4?无需注册!
  • Android Flutter在点击事件上添加动画效果
  • VSCode嵌入式开发环境搭建
  • 数据结构之栈的使用
  • QMessageBox手动添加按钮并绑定按钮的信号
  • 【C++进阶】位图和布隆过滤器
  • Android开发-Android UI与布局
  • 在不丢失数据的情况下解锁锁定的 Android 手机的 4 种方法
  • 【11】核心易中期刊推荐——人工智能 | 图形图像处理
  • Spring 中的事件发布与监听
  • c++ 一些常识 2
  • 用嘴写代码?继ChatGPT和NewBing之后,微软又开始整活了,Github Copilot X!
  • 3分钟阐述这些年我的 接口自动化测试 职业生涯经验分享
  • 十大Python可视化工具,太强了
  • 五.ElasticSearch的基础+实战
  • Oracle的学习心得和知识总结(十三)|Oracle数据库Real Application Testing之Database Reply实操(一)
  • CAD外部参照如何重新定位?CAD外部参照重定位步骤
  • 11. C#高级进阶
  • 网络编程套接字( TCP协议通讯流程)
  • WPF毛笔字实现过程
  • MHA实现mysql数据库高可用
  • leetcode每日一题:55. 跳跃游戏
  • 【C++】map 和 set
  • 基于SpringBoot的酒店管理系统
  • JAVA框架知识整理