【Java实现 LeetCode 652 寻找重复的子树(两个map的DFS)】教程文章相关的互联网学习教程文章

LeetCode 685. Redundant Connection II 冗余连接 II (C++/Java)【代码】

题目: In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) for which all other nodes are descendants of this node, plus every node has exactly one parent, except for the root node which has no parents. The given input is a directed graph that started as a rooted tree with N nodes (with distinct values 1, 2, ..., N), with one additional directed edge a...

leetcode 旋转链表 Java【代码】

题干 示例 1: 输入: 1->2->3->4->5->NULL, k = 2 输出: 4->5->1->2->3->NULL 解释: 向右旋转 1 步: 5->1->2->3->4->NULL 向右旋转 2 步: 4->5->1->2->3->NULL示例 2: 输入: 0->1->2->NULL, k = 4 输出: 2->0->1->NULL 解释: 向右旋转 1 步: 2->0->1->NULL 向右旋转 2 步: 1->2->0->NULL 向右旋转 3 步: 0->1->2->NULL 向右旋转 4 步: 2->0->1->NULL想法 看题干我们就知道了 肯定是先定位到链表尾巴构成循环链表 再通过k值决定新断开...

LeetCode 688. Knight Probability in Chessboard “马”在棋盘上的概率 (C++/Java)【代码】【图】

题目: On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exactly K moves. The rows and columns are 0 indexed, so the top-left square is (0, 0), and the bottom-right square is (N-1, N-1). A chess knight has 8 possible moves it can make, as illustrated below. Each move is two squares in a cardinal direction, then one square in an orthogonal direction. Each...

LeetCode 687. Longest Univalue Path 最长同值路径 (C++/Java)【代码】

题目: Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root. The length of path between two nodes is represented by the number of edges between them. Example 1: Input:5/ 4 5/ \ 1 1 5Output: 2 Example 2: Input:1/ 4 5/ \ 4 4 5Output: 2 N...

Java实现 LeetCode 151 翻转字符串里的单词【代码】

151. 翻转字符串里的单词 给定一个字符串,逐个翻转字符串中的每个单词。 示例 1: 输入: “the sky is blue” 输出: “blue is sky the” 示例 2: 输入: " hello world! " 输出: “world! hello” 解释: 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。 示例 3: 输入: “a good example” 输出: “example good a” 解释: 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。 说明...

LeetCode 684. Redundant Connection 冗余连接(C++/Java)【代码】【图】

题目: In this problem, a tree is an undirected graph that is connected and has no cycles. The given input is a graph that started as a tree with N nodes (with distinct values 1, 2, ..., N), with one additional edge added. The added edge has two different vertices chosen from 1 to N, and was not an edge that already existed. The resulting graph is given as a 2D-array of edges. Each element of edge...

LeetCode 231. Power of Two 判断是否为2的幂(Java)【代码】

题目: Given an integer, write a function to determine if it is a power of two.Example 1: Input: 1 Output: true Explanation: 20 = 1Example 2: Input: 16 Output: true Explanation: 24 = 16Example 3: Input: 218 Output: false解答: 解答一:位运算 首先想到的思路为: 若 n 为2的幂,则 n 对应的二进制有且只有一个1。 基于此,对 n 的二进制进行位运算,通过与 & 运算判断二进制是否有且只有一个1 class Solution {pu...

Leetcode---java之string用法【代码】

题目描述:字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 class Solution { public String reverseLeftWo...

Leetcode刷题java之34. 在排序数组中查找元素的第一个和最后一个位置

执行结果: 通过 显示详情 执行用时 :0 ms, 在所有 Java 提交中击败了100.00% 的用户 内存消耗 :41.9 MB, 在所有 Java 提交中击败了51.78%的用户 题目: 给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。 你的算法时间复杂度必须是 O(log n) 级别。 如果数组中不存在目标值,返回 [-1, -1]。 示例 1: 输入: nums = [5,7,7,8,8,10], target = 8 输出: [3,4] 示例 2: 输入:...

Leetcode刷题java之31. 下一个排列

执行结果: 通过 显示详情 执行用时 :1 ms, 在所有 Java 提交中击败了99.97% 的用户 内存消耗 :36.5 MB, 在所有 Java 提交中击败了66.63%的用户 题目; 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列。 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列)。 必须原地修改,只允许使用额外常数空间。 以下是一些例子,输入位于左侧列,其相应输出位于右侧列。 1,2,3 →...

Leetcode刷题java之11. 盛最多水的容器(top100)

执行结果: 通过 显示详情 执行用时 :5 ms, 在所有 Java 提交中击败了45.23% 的用户 内存消耗 :40 MB, 在所有 Java 提交中击败了25.70%的用户 题目: 给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。 说明:你不能倾斜容器,且 n 的值至少为 2。 图中垂直线代...

leetcode颠倒二进制位java【代码】

题干 颠倒给定的 32 位无符号整数的二进制位。 示例 1: 输入: 00000010100101000001111010011100 输出: 00111001011110000010100101000000 解释: 输入的二进制串 00000010100101000001111010011100 表示无符号整数 43261596,因此返回 964176192,其二进制表示形式为 00111001011110000010100101000000。示例 2: 输入:11111111111111111111111111111101 输出:10111111111111111111111111111111 解释:输入的二进制串 111111111...

Leetcode(Java)-24. 两两交换链表中的节点

给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。 示例: 给定 1->2->3->4, 你应该返回 2->1->4->3. /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/ class Solution {public ListNode swapPairs(ListNode head) {ListNode Sentinel = new ListNode(...

LeetCode 67. Add Binary 二进制求和 (Java)【代码】

题目: Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only characters 1 or 0.Example 1: Input: a = “11”, b = “1” Output: “100”Example 2: Input: a = “1010”, b = “1011” Output: “10101”解答: 解法一: 首先相到了用一下方法,思路为: (1)对字符串等长位数进行求和,使用temp表示进位数。 (2)每一对应位的真实求和current= 进位...

LeetCode 232题用栈实现队列(Implement Queue using Stacks) Java语言求解【代码】【图】

题目链接 https://leetcode-cn.com/problems/implement-queue-using-stacks/ 题目描述 使用栈实现队列的下列操作: push(x)?--?将一个元素放入队列的尾部。pop()?--?从队列首部移除元素。peek()?--?返回队列首部的元素。empty()?--?返回队列是否为空。 示例: MyQueue queue = new MyQueue();queue.push(1);queue.push(2); queue.peek(); // 返回 1queue.pop(); // 返回 1queue.empty(); // 返回 false 思路 使用两个栈来完成操...