Java每日一练(20230320)
创始人
2025-05-31 03:11:18
0

目录

1. 两数之和  🌟🌟

2. 盛最多水的容器  🌟🌟

3. 反转链表 II  🌟

🌟 每日一练刷题专栏 🌟

Golang每日一练 专栏

Python每日一练 专栏

C/C++每日一练 专栏

Java每日一练 专栏


1. 两数之和

给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 的那 两个 整数,并返回它们的数组下标。

你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。

你可以按任意顺序返回答案。

示例 1:

输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。

示例 2:

输入:nums = [3,2,4], target = 6
输出:[1,2]

示例 3:

输入:nums = [3,3], target = 6
输出:[0,1]

提示:

  • 2 <= nums.length <= 10^3
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= target <= 10^9
  • 只会存在一个有效答案
import java.util.*;
public class twoSum {public static class Solution {public static int[] twoSum(int[] nums, int target) {Map cache = new HashMap<>();for (int i = 0; i < nums.length; i++) {int distance = target - nums[i];if (cache.containsKey(distance)) {return new int[] { cache.get(distance), i };} else {cache.put(nums[i], i);}}return new int[] {};}}public static void main(String[] args) {Solution s = new Solution();int nums[] = {2,7,11,5};int res[] = s.twoSum(nums, 9);for (int i = 0; i < res.length; i++) {System.out.print(res[i] + " ");}System.out.println();int nums2[] = {3,2,4};int res2[] = s.twoSum(nums2, 6);for (int i = 0; i < res2.length; i++) {System.out.print(res2[i] + " ");}System.out.println();int nums3[] = {3,3};int res3[] = s.twoSum(nums3, 6);for (int i = 0; i < res3.length; i++) {System.out.print(res3[i] + " ");}System.out.println();}
}

输出:

0 1 
1 2 
0 1 


2. 盛最多水的容器

给你 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) 。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。

说明:你不能倾斜容器。

示例 1:

输入:[1,8,6,2,5,4,8,3,7]
输出:49 
解释:图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。

示例 2:

输入:height = [1,1]
输出:1

示例 3:

输入:height = [4,3,2,1,4]
输出:16

示例 4:

输入:height = [1,2,1]
输出:2

提示:

  • n = height.length
  • 2 <= n <= 3 * 10^4
  • 0 <= height[i] <= 3 * 10^4

代码:

import java.util.*;
public class maxArea {public static class Solution {public int maxArea(int[] height) {int N = height.length;int i = 0;int j = N - 1;int max = 0;while (i < j) {int c = (j - i) * Math.min(height[i], height[j]);if (c > max) {max = c;}if (height[i] > height[j]) {j--;} else {i++;}}return max;}}public static void main(String[] args) {Solution s = new Solution();int height[] = {1,8,6,2,5,4,8,3,7};System.out.println(s.maxArea(height));int height2[] = {1,1};System.out.println(s.maxArea(height2));int height3[] = {4,3,2,1,4};System.out.println(s.maxArea(height3));int height4[] = {1,2,1};System.out.println(s.maxArea(height4));}
}

输出:

49
1
16
2


3. 反转链表 II

给你单链表的头指针 head 和两个整数 left 和 right ,其中 left <= right 。请你反转从位置 left 到位置 right 的链表节点,返回 反转后的链表 。

示例 1:

输入:head = [1,2,3,4,5], left = 2, right = 4
输出:[1,4,3,2,5]

示例 2:

输入:head = [5], left = 1, right = 1
输出:[5]

提示:

  • 链表中节点数目为 n
  • 1 <= n <= 500
  • -500 <= Node.val <= 500
  • 1 <= left <= right <= n

代码:

class Solution {public ListNode reverseBetween(ListNode head, int m, int n) {ListNode dummy = new ListNode(0);dummy.next = head;ListNode pre = dummy;for (int i = 1; i < m; i++) {pre = pre.next;}head = pre.next;for (int i = m; i < n; i++) {ListNode nex = head.next;head.next = nex.next;nex.next = pre.next;pre.next = nex;}return dummy.next;}
}

递归法:

class Solution {
    public ListNode reverseBetween(ListNode head, int left, int right) {
        if (left == 1) {
            return reverseN(head, right);
        }
        head.next = reverseBetween(head.next, left - 1, right - 1);
        return head;
    }
    ListNode successor = null; // 后驱节点
    // 反转以 head 为起点的 n 个节点,返回新的头结点
    ListNode reverseN(ListNode head, int n) {
        if (n == 1) {
            successor = head.next;
            return head;
        }
        ListNode last = reverseN(head.next, n - 1);
        head.next.next = head;
        head.next = successor;
        return last;
    }


🌟 每日一练刷题专栏 🌟

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

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

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

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

Golang每日一练 专栏

Python每日一练 专栏

C/C++每日一练 专栏

Java每日一练 专栏

相关内容

热门资讯

python基础语法【模块 包... 模块 包 异常捕获 1.模块 python一个py文件就是一个模块 1.1 使用方法 1)前提&#x...
在recyclerview中使... 问题描述 最近在使用RecycerView的瀑布流布局,我想直接用ViewBindin...
java中Long型数据大小比... 起因 今天在做项目的时候,想构建一个树形结构,从数据库中查询出了所有数据...
智能控制 | AIRIOT智慧... 许多行业客户在智慧楼宇的建设中主要面临运营管理低效,楼宇内部各个系统相互独立ÿ...
Redis 数据结构 这里写目录标题Redis 数据结构一、String类型String数据类型的使用场景key 的设置约...
基于 MM32SPIN0280... M32SPIN0280 是灵动微电机新推出的针对电机控制市场的专用 MCU,该系列 M...
C++学习(指针、引用、结构体... 1编译软件Visual Studio2基本语法2.1指针2.1.1指针的使用//定义一个指针int ...
【UML】项目开发流程 以下模型是一个项目从启动到最终部署,逐步细化(精化)、实现...
docker-java应用部署 目录          1端口映射 2.Mysql部署 3.Tomcat部署 4.Nginx部署 5...
CentOS操作系统libc.... 使用xshell登陆Linux后查看jdk版本提示 /lib64/libc.so.6: versio...
Linux串口实现树莓派与电脑... 目录 一  串口说明 二  USB—TTL模块 ● usb-ttl模块接口  三  串口通信常用的A...
BeanPostProcess... 文章目录一、BeanPostProcessor的作用1. 源码2. 使用案例二、Spring生命周期...
2023.3.22 文章目录@13:static关键字**一:static修饰变量&...
模糊PID控制双容水箱液位控制... 资源:双容水箱液位模糊PID控制MATLAB仿真-电子商务文档类资源-CSDN文库模糊...
基于springboot家政服... 大家好✌!我是CZ淡陌。一名专注以理论为基础实战为主的技术博主,将再这里...
提升代码质量,使用插件对 ja... 目录前言一、使用maven-checkstyle-plugin插件1. maven-checksty...
VSCode配置git bas... 打开左下角齿轮图标      打开Settings 搜索框输入 terminal.integrat...
Winform控件开发(21)... 一、属性 1、Name 用于获取控件对象 2、Anchor 锚定控件对于父控件的位置 3、BackC...
【kubernetes云原生】... 目录 一、标签选择器来源 二、什么是标签选择器 2.1 标签选择器概述 2.2 标签选择器概述属性 ...
重构条件-Replace Ne... 重构条件-Replace Nested Conditional with Guard Clauses...