Leetcode题解(23)】的更多相关文章

合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. 示例: 输入: [ 1->4->5, 1->3->4, 2->6 ] 输出: 1->1->2->3->4->4->5->6 答案: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { va…
方法一:分治 + 递归 解题思路 在21. 合并两个有序链表,我们知道如何合并两个有序链表.而本题是合并 k 个有序链表,可以通过大问题拆分成小问题解决,即把 k 个链表,拆分成 k/2 个链表组,俩俩合并,直到合并成一个链表,这就是分而治之的思想. 可以通过递归来实现分治算法: 定义函数功能:合并链表数组中的 left 到 right 个链表 结束条件:left == right,即只剩下一个链表,无须合并,直接返回 递推公式:将 [left, right] 拆分成两半,分别获得左边已合并的链…
目录 描述 解法一:排序算法(不满足时间复杂度要求) Java 实现 Python 实现 复杂度分析 解法二:最小堆 思路 Java 实现 Python 实现 复杂度分析 解法三:桶排序(bucket sort) 思路 Java 实现 Python 实现 复杂度分析 更多 LeetCode 题解笔记可以访问我的 github. 描述 给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [1,2] 示例 2…
目录 [LeetCode题解]7_反转整数 描述 方法一 思路 Java 实现 类似的 Java 实现 Python 实现 方法二:转化为求字符串的倒序 Java 实现 Python 实现 [LeetCode题解]7_反转整数 描述 给定一个 32 位有符号整数,将整数中的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: 21 注意: 假设我们的环境只能存储 32 位有符号整数,其数值范围是 \([-2…
目录 [LeetCode题解]349_两个数组的交集 描述 方法一:两个哈希表 Java 实现 类似的 Java 实现 Python 实现 类似的 Python 实现 方法二:双指针 Java 实现 方法三:二分查找 Java 实现 方法四 Java 实现 [LeetCode题解]349_两个数组的交集 描述 给定两个数组,编写一个函数来计算它们的交集. 示例1: 输入:nums1 = [1,2,2,1], nums2 = [2,2] 输出:[2] 示例2: 输入:nums1 = [4,9,5]…
前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 2. 思路 找到规则即可 罗马数字的表示: I~1 V~5 X~10 L~50 C~100 D~500…
前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and…
前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], t…
前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i]…
前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head…