【【LeetCode-easy】合并两个排序的链表(Java)】教程文章相关的互联网学习教程文章

leetcode 7. Reverse Integer [java]

public int reverse(int x) {long res = 0;while (x != 0){res = res* 10 + x % 10;x /= 10;}if(res >= Integer.MAX_VALUE || res < Integer.MIN_VALUE)return 0;return (int)res;}

【leetcode561】数组拆分 I java题解【代码】

【leetcode分类下所有的题解均为作者本人经过权衡后挑选出的题解,在易读和可维护性上有优势 每题只有一个答案,避免掉了太繁琐的以及不实用的方案,所以不一定是最优解】 给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), …, (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最大。 示例 1: 输入: [1,4,3,2] 输出: 4 解释: n 等于 2, 最大总和为 4 = min(1, 2) + min(3, 4). 提示: 1.n 是正整数,范围在...

leetcode 2. Add Two Numbers [java]

注意点:最后的进位 (l1 == null || l1.next == null) break;public ListNode addTwoNumbers(ListNode l1, ListNode l2) {boolean j = false;ListNode p = new ListNode(0);ListNode res = p;while(true){int a = (l1 != null ? l1.val : 0) + (l2 != null ? l2.val : 0) + (j ? 1 : 0); if(a >= 10){j = true;p.val = a - 10;}else{j = false;p.val = a;}l1 = (l1 == null || l1.next == null) ? null : l1.next;l2 = (l2 == null...

leetcode 1. Two Sum [java]

注意点:HashMap<Integer, Integer> return new int[]{}; 3 2 4 target:6 return null;public int[] twoSum(int[] nums, int target) {HashMap<Integer, Integer> m = new HashMap<>();for(int i = 0; i < nums.length; ++i){if(m.containsKey(target - nums[i]))return new int[]{m.get(target - nums[i]), i};m.put(nums[i], i);}return null;}

leetcode 72 编辑距离 JAVA【代码】

题目: 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 。 你可以对一个单词进行如下三种操作:插入一个字符 删除一个字符 替换一个字符示例 1: 输入: word1 = "horse", word2 = "ros" 输出: 3 解释: horse -> rorse (将 h 替换为 r) rorse -> rose (删除 r) rose -> ros (删除 e)示例 2: 输入: word1 = "intention", word2 = "execution" 输出: 5 解释: intention -> inention (删除 t) inentio...

LeetCode92. 反转链表 II------Java语言【代码】

92. 反转链表 II 要求:反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。 说明: 1 ≤ m ≤ n ≤ 链表长度。 示例: 输入: 1->2->3->4->5->NULL, m = 2, n = 4 输出: 1->4->3->2->5->NULL 该题目是LeetCode 206. 反转链表的升级版 分析: ①题目要求时间复杂度为O(n) 即只能遍历一次 ②如何反转 思路: ①1 ≤ m ≤ n ≤ 链表长度,由于m是最小的,所以当遍历到m的位置时就进行反转 ②一直反转到n的位置 首先定义一些指针,用于...

LeetCode——67 Java之二进制求和【代码】

题目要求: 给定两个二进制字符串,返回他们的和(用二进制表示)。 输入为非空字符串且只包含数字 1 和 0。 示例 1: 输入: a = "11", b = "1" 输出: "100" 示例 2: 输入: a = "1010", b = "1011" 输出: "10101"思路:看到题目要求是一个关于二进制求和的问题,那么就要想一想二进制求和的结果有哪几种形式,of course,很简单,进位or不进位。那么哪种情况下进位,哪种情况下无需进位,我们就来简单说一下。进位:1+1=2进位,1+1+...

[Java]LeetCode690. 员工的重要性 | Employee Importance【代码】

You are given a data structure of employee information, which includes the employees unique id, his importance value and his direct subordinates id. For example, employee 1 is the leader of employee 2, and employee 2 is the leader of employee 3. They have importance value 15, 10 and 5, respectively. Then employee 1 has a data structure like [1, 15, [2]], and employee 2 has [2, 10, [3]], and employ...

LeetCode算法题-Longest Univalue Path(Java实现)【代码】

这是悦乐书的第290次更新,第308篇原创01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第158题(顺位题号是687)。给定二叉树,找到路径中每个节点具有相同值的最长路径的长度。此路径可能会也可能不会通过根目录。例如: 输入:5/ 4 5/ \ 1 1 5 输出:路径为[5,5,5],边长为2输入:1/ 4 5/ \ 4 4 5 输出:路径为[4,4,4],边长为2注意:两个节点之间的路径长度...

leetcode:颠倒二进制位(java,考察点:二进制一般会和位运算符有关)

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

leetcode 56 合并区间 JAVA【代码】

题目: 给出一个区间的集合,请合并所有重叠的区间。 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: 区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].示例 2: 输入: [[1,4],[4,5]] 输出: [[1,5]] 解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。 思路: 先将给出的数组,对其按照start属性大小排序,然后当后者的start小于前者的end时,修改temp的start、end的值,否者result.add(temp)./*** Defi...

(Java)leetcode-141 Linked List Cycle【代码】【图】

题目 【判断链表是否有环】 Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list. Example 1: Input: head = [3,2,0,-4], pos = 1 Output: true Explanation: There is a cycle in the linked list,...

LeetCode算法题-Second Minimum Node In a Binary Tree(Java实现)【代码】

这是悦乐书的第285次更新,第302篇原创01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第153题(顺位题号是671)。给定非空的特殊二叉树,其由具有非负值的节点组成,其中该树中的每个节点具有恰好两个或零个子节点。 如果节点具有两个子节点,则该节点的值是其两个子节点中的较小值。给定这样的二叉树,您需要输出由整个树中所有节点的值组成的集合中的第二个最小值。如果不存在这样的第二个最小值,则输出-1。例如:2/ \2...

LeetCode算法题-Image Smoother(Java实现)【代码】

这是悦乐书的第282次更新,第299篇原创01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第150题(顺位题号是661)。给定表示图像灰度的2D整数矩阵M,您需要设计一个平滑器以使每个单元的灰度变为所有8个周围单元及其自身的平均灰度(向下舍入)。如果一个单元的周围单元少于8个,那么尽可能多地使用单元。例如: 输入:[[1,1,1],[1,0,1][1,1,1]] 输出:[[0,0,0],[0,0,0],[0,0,0]] 说明: 对于点(0,0),(0,2),(2,0)...

(Java)leetcode-191 Number of 1 Bits(带符号右移与无符号右移的区别)【代码】

题目 1的位数 Write a function that takes an unsigned integer and return the number of ‘1’ bits it has (also known as the Hamming weight). Example 1: Input: 00000000000000000000000000001011 Output: 3Explanation: The input binary string 00000000000000000000000000001011 has a total of three ‘1’ bits. Example 2: Input: 00000000000000000000000010000000 Output: 1Explanation: The input binary string ...

链表 - 相关标签