349. Intersection of Two Arrays
Given two arrays, write a function to compute their intersection.
Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2] Output: [2]
Example 2:
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] Output: [9,4]
Note:
- Each element in the result must be unique.
- The result can be in any order.
给定两个数组,编写一个函数来计算它们的交集。
class Solution(object):
def intersection(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
res=[]
for c in nums1:
if c in nums2 and c not in res:
res.append(c)
return res
350. Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection.
Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2] Output: [2,2]
Example 2:
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] Output: [4,9]
Note:
- Each element in the result should appear as many times as it shows in both arrays.
- The result can be in any order.
Follow up:
- What if the given array is already sorted? How would you optimize your algorithm?
- What if nums1's size is small compared to nums2's size? Which algorithm is better?
- What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
给定两个数组,编写一个函数来计算它们的交集。
class Solution(object):
def intersect(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
nums1.sort()
nums2.sort()
res=[]
l=0
r=0
while l<len(nums1) and r<len(nums2):
if nums1[l]==nums2[r]:
res.append(nums1[l])
l+=1
r+=1
elif nums1[l]<nums2[r]:
l+=1
elif nums1[l]>nums2[r]:
r+=1
return res
75. Sort Colors
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
Note: You are not suppose to use the library's sort function for this problem.
Example:
Input: [2,0,2,1,1,0] Output: [0,0,1,1,2,2]
Follow up:
- A rather straight forward solution is a two-pass algorithm using counting sort.
First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's. - Could you come up with a one-pass algorithm using only constant space?
给定一个包含红色、白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。
此题中,我们使用整数 0、 1 和 2 分别表示红色、白色和蓝色。
有点像快排,两边两个指针,遍历i=0,如果nums[i]==2,就和最后一个指针替换,最后一个指针减一,如果nums[i]==0,就和第一个指针替换,第一个指针加一,i+1,如果nums[i]==1,i+1
class Solution(object):
def sortColors(self, nums):
"""
:type nums: List[int]
:rtype: None Do not return anything, modify nums in-place instead.
"""
if nums is None:
return
l=0
r=len(nums)-1
i=0
while i<=r:
if nums[i]==2:
nums[i],nums[r]=nums[r],nums[i]
r-=1
elif nums[i]==0:
nums[i],nums[l]=nums[l],nums[i]
l+=1
i+=1
else:
i+=1
return nums
976. Largest Perimeter Triangle
Given an array A
of positive lengths, return the largest perimeter of a triangle with non-zero area, formed from 3 of these lengths.
If it is impossible to form any triangle of non-zero area, return 0
.
Example 1:
Input: [2,1,2] Output: 5
Example 2:
Input: [1,2,1] Output: 0
Example 3:
Input: [3,2,3,4] Output: 10
Example 4:
Input: [3,6,2,3] Output: 8
给定由一些正数(代表长度)组成的数组 A
,返回由其中三个长度组成的、面积不为零的三角形的最大周长。
如果不能形成任何面积不为零的三角形,返回 0
。
思路:先将数组从大到小进行排序,然后按照三角形的原则,两边之和大于第三边,进行判断
class Solution(object):
def largestPerimeter(self, A):
"""
:type A: List[int]
:rtype: int
"""
A.sort(reverse=True)
for i in range(len(A)-2):
if A[i+1]+A[i+2]>A[i]:
return A[i]+A[i+1]+A[i+2]
return 0