LeetCode 023:合并K个排序链表
You are given an array of k linked-lists lists,
each linked-list is sorted in ascending order.
Merge all the linked-lists into one sorted linked-list and return it.
三种解决方案:
1)两两合并,详细可看LeetCode:23. 合并K个排序链表(python)
2)将所有的链表放入数组,对数组排序后创建新的链表,详细可看leetcode 23. 合并K个排序链表 python
3)利用python的堆排序,将所有链表放入一个堆中,新链表存放堆中元素。详细可看简便python两解法。这个解决方案是从leetcode中文官网上找到的。
由于个人喜好简洁代码,所以这里放的是第三种解决方案。
class Solution(object):
def mergeKLists(self, lists):
import heapq
head = point = ListNode(0)
heap = []
for l in lists:
while l:
heapq.heappush(heap, l.val)
l = l.next
while heap:
val = heappop(heap)
point.next = ListNode(val)
point = point.next
point.next = None
return head.next
作者:milkyer-2
链接:https://leetcode-cn.com/problems/merge-k-sorted-lists/solution/jian-bian-pythonliang-jie-fa-by-milkyer-2/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
LeetCode 026:删除排序数组中的重复项
Given a sorted array nums, remove the duplicates in-place
such that each element appears only once and returns the new length.
Do not allocate extra space for another array,
you must do this by modifying the input array in-place with O(1) extra memory.
Clarification:
Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference,
which means a modification to the input array will be known to the caller as well.
Internally you can think of this:
// nums is passed in by reference. (i.e., without making a copy)
int len = removeDuplicates(nums);
// any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
print(nums[i]);
}
解决思路:
1)双指针,第一个指针指向当前不重复数字的位置,第二个指针从头至尾依次扫描,当第二个指针与第一个指针的值不同时,说明出现了一个不同的数字,把第一个向后移动一位,并把该数字存入。下面代码出自LeetCode.26 删除排序数组中的重复项(python解法)。
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
i = 0 # 第一个指针
for j in range(1, len(nums)): # 第二个指针
if nums[i]!=nums[j]:
i += 1 # 第一个指针移动一位
nums[i] = nums[j]
return i + 1
2)利用pop 从后往前遍历
解决方案出自leetcode 26. 删除排序数组中的重复项 python3详解。
3)利用set去重
这个方法代码最少,但是因为输出不符合leetcode官网要求,所以提交不成功,但这个方案确实是可行的。在以后自己的代码里输出格式没有特别要求的情况下可以考虑使用。解决方案出自LeetCode 26 删除排序数组中的重复项 (Python实现的两种方法
def removeDuplicates(nums):
n_nums = list(set(nums)
return n_nums, len(n_nums)
nums = [1, 1, 2]
n_nums, n_nums_length = removeDuplicates(nums)
print(n_nums)
print(n_nums_length)
LeetCode33:搜索旋转排序数组
You are given an integer array nums sorted in ascending order (with distinct values),
and an integer target.
Suppose that nums is rotated at some pivot unknown to you beforehand
(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
If target is found in the array return its index, otherwise, return -1.
第一种解法:正儿八经的用二分法进行搜索。
LeetCode 33. 搜索旋转排序数组
第二种解法代码比较简单,用python的.index()
函数。
LeetCode 33. 搜索旋转排序数组 python
class Solution(object):
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
try:
index = nums.index(target)
except:
index = -1
return index
任务链接:
team-learning-program/LeetCodeTencent/023 合并K个排序链表.md
team-learning-program/LeetCodeTencent/026 删除排序数组中的重复项.md
team-learning-program/LeetCodeTencent/033 搜索旋转排序数组.md