Java基础算法每日5道详解(3)

news2025/2/25 8:27:04

136. Single Number

单号

Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.

You must implement a solution with a linear runtime complexity and use only constant extra space.
给定一个非空整数数组 nums,每个元素出现两次,除了一个。 找到那个单一的。

您必须实现一个具有线性运行时复杂性的解决方案,并且只使用恒定的额外空间。

Example 1:

Input: nums = [2,2,1]
Output: 1

Example 2:

Input: nums = [4,1,2,1,2]
Output: 4

Example 3:

Input: nums = [1]
Output: 1


leetcode代码

class Solution {
    public int singleNumber(int[] nums) {
        int ans =0;

        int len = nums.length;
        for(int i=0;i!=len;i++)
            ans ^= nums[i];

        return ans;

    }
}

141. Linked List Cycle 链表循环

Given head, the head of a linked list, determine if the linked list has a cycle in it.

There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail’s next pointer is connected to. Note that pos is not passed as a parameter.

Return true if there is a cycle in the linked list. Otherwise, return false.

Example 1:

https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png

Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).

Example 2:

https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png

Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 0th node.

Example 3:

https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png

Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.

leetcode代码

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
    ListNode slow = head, fast = head;
  
    while (fast != null && fast.next != null) {
        slow = slow.next;
        fast = fast.next.next;

        if (slow == fast) 
            return true;
    }

    return false;
        
    }
}

144. Binary Tree Preorder Traversal

二叉树先序遍历

Given the root of a binary tree, return the preorder traversal of its nodes’ values.
给定二叉树的根,返回其节点值的前序遍历。

Example 1:

https://assets.leetcode.com/uploads/2020/09/15/inorder_1.jpg

Input: root = [1,null,2,3]
Output: [1,2,3]

Example 2:

Input: root = []
Output: []

Example 3:

Input: root = [1]
Output: [1]

leetcode代码

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
   public List<Integer> preorderTraversal(TreeNode node) {
        List<Integer> list = new LinkedList<Integer>();
        Stack<TreeNode> rights = new Stack<TreeNode>();
        while(node != null) {
            list.add(node.val);
            if (node.right != null) {
                rights.push(node.right);
            }
            node = node.left;
            if (node == null && !rights.isEmpty()) {
                node = rights.pop();
            }
        }
        return list;
   }
}

145. Binary Tree Postorder Traversal

二叉树邮政顺序遍历

Given the root of a binary tree, return the postorder traversal of its nodes’ values. 给定二叉树的根,返回其节点值的后序遍历。

Example 1:

https://assets.leetcode.com/uploads/2020/08/28/pre1.jpg

Input: root = [1,null,2,3]
Output: [3,2,1]

Example 2:

Input: root = []
Output: []

Example 3:

Input: root = [1]
Output: [1]

leetcode代码

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        LinkedList<Integer> ans = new LinkedList<>();
        Stack<TreeNode> stack = new Stack<>();
        if (root == null) return ans;

        stack.push(root);
        while (!stack.isEmpty()) {
            TreeNode cur = stack.pop();
            ans.addFirst(cur.val);
            if (cur.left != null) {
                stack.push(cur.left);
            }
            if (cur.right != null) {
                stack.push(cur.right);
            } 
        }
        return ans;
    }
}

160. Intersection of Two Linked Lists

两个链表的交集

Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null.

For example, the following two linked lists begin to intersect at node c1:

https://assets.leetcode.com/uploads/2021/03/05/160_statement.png

The test cases are generated such that there are no cycles anywhere in the entire linked structure.

Note that the linked lists must retain their original structure after the function returns.

Custom Judge:

The inputs to the judge are given as follows (your program is not given these inputs):

  • intersectVal - The value of the node where the intersection occurs. This is 0 if there is no intersected node.
  • listA - The first linked list.
  • listB - The second linked list.
  • skipA - The number of nodes to skip ahead in listA (starting from the head) to get to the intersected node.
  • skipB - The number of nodes to skip ahead in listB (starting from the head) to get to the intersected node.

The judge will then create the linked structure based on these inputs and pass the two heads, headA and headB to your program. If you correctly return the intersected node, then your solution will be accepted.
给定两个单链表 headA 和 headB 的头,返回两个列表相交的节点。如果两个链表完全没有交集,则返回 null。

例如,以下两个链表在节点 c1 处开始相交:

测试用例的生成使得整个链接结构中的任何地方都没有循环。

请注意,函数返回后,链表必须保留其原始结构。

自定义裁判:

给法官的输入如下(你的程序没有给出这些输入):

intersectVal - 发生交点的节点的值。如果没有相交节点,则为 0。
listA - 第一个链表。
listB - 第二个链表。
skipA - 在 listA 中向前跳过(从头部开始)以到达相交节点的节点数。
skipB - 在 listB 中向前跳过(从头部开始)以到达相交节点的节点数。
然后,法官将根据这些输入创建链接结构,并将两个头(headA 和 headB)传递给您的程序。如果您正确返回相交节点,那么您的解决方案将被接受。
Example 1:

https://assets.leetcode.com/uploads/2021/03/05/160_example_1_1.png

Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
Output: Intersected at '8'
Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect).
From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,6,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.
- Note that the intersected node's value is not 1 because the nodes with value 1 in A and B (2nd node in A and 3rd node in B) are different node references. In other words, they point to two different locations in memory, while the nodes with value 8 in A and B (3rd node in A and 4th node in B) point to the same location in memory.

Example 2:

https://assets.leetcode.com/uploads/2021/03/05/160_example_2.png

Input: intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
Output: Intersected at '2'
Explanation: The intersected node's value is 2 (note that this must not be 0 if the two lists intersect).
From the head of A, it reads as [1,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.

Example 3:

https://assets.leetcode.com/uploads/2021/03/05/160_example_3.png

Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
Output: No intersection
Explanation: From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.
Explanation: The two lists do not intersect, so return null.

leetcode代码

public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
    //boundary check
    if(headA == null || headB == null) return null;
    
    ListNode a = headA;
    ListNode b = headB;
    
    //if a & b have different len, then we will stop the loop after second iteration
    while( a != b){
    	//for the end of first iteration, we just reset the pointer to the head of another linkedlist
        a = a == null? headB : a.next;
        b = b == null? headA : b.next;    
    }
    
    return a;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/151585.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

在Multisim导入TI提供的SPICE模型

对在multisim中导入TI模型的一个记录。 multisim中只有常规的元器件&#xff0c;对于很多元器件multisim都没有相应的模型&#xff0c;这就需要手动导入了。 Multisim导入模型1、从官网下载相应的模型文件2、在Multisim中导入模型3、写在后面1、从官网下载相应的模型文件 &…

c/c++ 函数(一) setw()、isdigit()、isalpha()、atoi()、itoa()

目录 1、setw(int n) <iomanip> 2、int isdigit(char ch) <ctype.h> 3、int isalpha(int c) <ctype.h> 4、int atoi(const char* str) <stdlib.h> 5、char* itoa(int num) <stdlib.h> 1、se…

Linux 基本权限

目录 1 shell命令以及运行原理 1.1 理解 1.2 意义 2 Linux权限的概念 2.1 概念 2.2 用户分类 2.3 Linux文件属性 2.4 Linux文件权限 2.4.1 文件访问者的分类&#xff08;人&#xff09; 2.4.2 root&&普通用户 vs 拥有者&&所属组&&other 2.4…

异步架构,避免相互依赖的系统耦合

前言&#xff1a; 使用缓存架构可以减少不必要的计算&#xff0c;快速响应用户请求&#xff0c;但是缓存只能改善系统的读操作性能&#xff0c;也就是在读取数据的时候&#xff0c;可以不从数据源中读取&#xff0c;而是通过缓存读取&#xff0c;以加速数据的读取速度。 但是…

vulnhub DC系列 DC-6

总结:wpscan爆破&#xff0c;nmap提权 下载地址 DC-6.zip (Size: 619 MB)Download: http://www.five86.com/downloads/DC-6.zipDownload (Mirror): https://download.vulnhub.com/dc/DC-6.zip使用方法:解压后&#xff0c;使用vm直接打开ova文件。 漏洞分析 信息收集 这里还是使…

4. 数据处理:用R语言实现【多路替换】真高效!!

b站课程视频链接&#xff1a; https://www.bilibili.com/video/BV19x411X7C6?p1 腾讯课堂(最新&#xff0c;但是要花钱&#xff0c;我花99&#x1f622;&#x1f622;元买了&#xff0c;感觉讲的没问题&#xff0c;就是知识点结构有点乱&#xff09;&#xff1a;https://ke.qq…

Linux中的vim最小集、指令集及其配置

目录 1. vim 最小集 2 vim指令集 2.1 命令模式的指令 2.1.1 插入模式 2.1.2 移动光标 2.1.3 删除文字 2.1.4 复制 2.1.5 替换 2.1.6 撤销 2.1.7 更改 2.1.8 跳至指定的行 2.1.9 shift ~:快速大小写切换 2.2 末行模式的指令 2.2.1 set nu/set nonu 2.2.2 vs file…

【自学Python】Python变量

Python变量 Python变量教程 不论是使用哪种高级程序语言编写程序&#xff0c;变量都是其程序的基本组成单位。变量相当于内存中一个数据存储空间的表示&#xff0c;通过变量名可以访问到变量的具体的值。 Python变量 Python 是弱类型语言&#xff0c;因此 Python 变量无须声…

Hudi的核心概念 —— 时间轴(TimeLine)

文章目录时间轴&#xff08;TimeLine&#xff09;时间轴&#xff08;TimeLine&#xff09; 就是一个时间线&#xff0c;它的每一个操作都记录在内&#xff0c;每一个时刻&#xff0c;你做了什么事情&#xff0c;对某一个时刻&#xff0c;记录一个时刻的数据 Hudi 的核心是维护…

擎创运维大数据治理解决方案,荣膺金融业数字化转型突出贡献奖

近日&#xff0c;由《金融电子化》杂志社主办的“2022中国金融科技年会暨第十三届金融科技应用创新奖颁奖典礼”成功于线上举办。擎创科技“运维大数据治理解决方案”&#xff0c;荣膺“2022科技赋能金融业数字化转型突出贡献奖”。人民银行《金融科技发展规划&#xff08;2022…

C++ string类

在c语言中&#xff0c;我们想要记录字符串需要创建一个字符串的数组&#xff0c;而c则提供了另一种方式&#xff1b; 也就是这篇博客所说的string类&#xff1b; string类 #include<string> 作为字符串数组的升级版&#xff0c;string类自然也有它的独特之处——可变长数…

自动驾驶标定基础知识

目录基础概念1. 缩略语2. 为什么需要外参标定3. 基于使用场景的标定分类4. 基于方法的分类5. 基础坐标系6. 超差EOL标定1.EOL特点2. EOL标定流程3. EOL标定软件约束4. EOL标定软件流程5. 算法设计原则6. 算法基本原理背景式标定1.背景式标定的特点2. 背景式标定运行流程3. 背景…

高级树结构之线索化二叉树

文章目录一 线索化二叉树简介二 线索化规则三 前序线索化3.1 代码演示四 中序线索化4.1 代码演示五 后序线索化5.1 代码演示一 线索化二叉树简介 线索化&#xff1a;将一颗二叉树的结点指向为空的指针&#xff0c;线索化为某一种顺序遍历的的指向下一个按顺序的结点的指针一颗…

虚拟主机3种方式nginx/apache+跨域知识点整理

目录referer、prototype、array、json笔记整理: [http://t.csdn.cn/s4P8x](http://t.csdn.cn/s4P8x)虚拟主机3种方式nginx/apache跨域知识点整理一、Apache基于多IP、多端口、多域名访问1、添加网卡三种方法1、虚拟机添加网络适配器2、命令添加3、用nmtui 添加ip地址2、添加配置…

Jwt 最流行的跨域身份验证解决方案

1. 什么是JWT JSON Web Token (JWT)是一个开放标准(RFC 7519)&#xff0c;它定义了一种紧凑的、自包含的方式&#xff0c;用于作为JSON对象在各方之间安全地传输信息。该信息可以被验证和信任&#xff0c;因为它是数字签名的。 JWT被设计为紧凑且安全的&#xff0c;特别适用于分…

Python制做一个电脑通知小工具,再也不怕忘记事情拉~

前言 嗨喽&#xff0c;大家好呀~这里是爱看美女的茜茜呐 又到了学Python时刻~ Windows不是有个消息通知功能&#xff0c;挺喜欢这个功能的&#xff0c; 但是不太方便使用&#xff0c;也懒得去研究&#xff0c; 于是准备用Python自己写一个&#xff0c;通过设定通知的间隔时…

Pikachu靶场暴力破解通关

目录 字典获取 BP四种攻击模式 一、Sniper(狙击手模式) 二、Battering ram(攻城锤模式) 三、Pitchfork(叉子模式) 四、Cluster bomb(炸弹模式) 靶场练习 基于表单的暴力破解 验证码绕过(on server) 验证码绕过(on client) token防爆破? 字典获取 在github里找到心…

公司注册商标的流程是什么

公司商标注册流程是什么? 1、商标查询。查询有关商标登记注册情况&#xff0c;以了解自己准备申请注册的商标是否与他人已经注册或正在注册的商标相同或近似的程序; 2、申请文件准备。商标注册申请书;)商标图样;申请注册集体商标、证明商标的&#xff0c;应当提交申请人主体资…

发布了一个jar包到中央仓库,我的心好累…

原创&#xff1a;微信公众号 码农参上&#xff0c;欢迎分享&#xff0c;转载请保留出处。 哈喽大家好啊&#xff0c;我是Hydra。 前几天我在网上冲浪的时候&#xff0c;看见有一个老铁在git上给我提了一个issue&#xff1a; 万万没想到&#xff0c;有一天我写的烂代码居然也会…

家庭装修流程五大步骤是怎样的,家庭装修有什么注意事项!

家庭装修流程五大步骤是怎样的家庭装修有什么注意事项&#xff01;现在的人们不是在城里面购买了楼房&#xff0c;就是在家里面进行翻修房子&#xff0c;不管是哪一种&#xff0c;都肯定是需要用到装修的。但是有很多的人们对于装修方面并不是很了解。想知道家庭装修流程五大步…