Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenzhang1007 committed Jul 14, 2020
1 parent 57f7e46 commit ff91365
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 43 deletions.
34 changes: 20 additions & 14 deletions array/11_盛最多水的容器/11_medium_盛最多水的容器.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,27 @@
# @lc code=start
class Solution:
def maxArea(self, height: List[int]) -> int:
Area = 0
i, j = 0, len(height) - 1
while i < j:
if (height[i] < height[j]) :
minHeight = height[i]
i += 1
area = (j-i+1) * minHeight
Area = max(area, Area)
"""求取最大面积区域
Args:
height: a list with height
Returns:
The maxArea
"""
# 双指针解法
max_area = 0
leftPointer, rightPointer = 0, len(height) - 1
while leftPointer < rightPointer:
if height[leftPointer] < height[rightPointer]:
area = height[leftPointer] * (rightPointer - leftPointer)
leftPointer += 1
max_area = max(area, max_area)
else:
minHeight = height[j]
j -= 1
area = (j-i+1) * minHeight
Area = max(area, Area)
return Area

area = height[rightPointer] * (rightPointer - leftPointer)
rightPointer -= 1
max_area = max(area, max_area)
return max_area


# @lc code=end

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#
# @lc app=leetcode.cn id=4 lang=python3
#
# [4] 寻找两个有序数组的中位数
#
# https://leetcode-cn.com/problems/median-of-two-sorted-arrays/description/
#
# algorithms
# Hard (37.27%)
# Likes: 2822
# Dislikes: 0
# Total Accepted: 215.9K
# Total Submissions: 563.6K
# Testcase Example: '[1,3]\n[2]'
#
# 给定两个大小为 m 和 n 的正序(从小到大)数组 nums1 和 nums2。
#
# 请你找出这两个正序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。
#
# 你可以假设 nums1 和 nums2 不会同时为空。
#
#
#
# 示例 1:
#
# nums1 = [1, 3]
# nums2 = [2]
#
# 则中位数是 2.0
#
#
# 示例 2:
#
# nums1 = [1, 2]
# nums2 = [3, 4]
#
# 则中位数是 (2 + 3)/2 = 2.5
#
#
#

# @lc code=start
class Solution:
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
# @lc code=end

1 change: 0 additions & 1 deletion hash-table/1_两数之和/1_easy_两数之和.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def twoSum(self, nums, target) :
return [temp_dict[nums[i]], i]
else:
temp_dict[target - nums[i]] = i

Solution().twoSum([2, 7, 11, 15], 13)

# @lc code=end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,39 +48,35 @@
# self.next = None

class Solution:
# 翻转一个子链表,并且返回新的头与尾
def reverse(self, head: ListNode, tail: ListNode):
prev = tail.next
p = head
while prev != tail:
nex = p.next
p.next = prev
prev = p
p = nex
return tail, head

def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
hair = ListNode(0)
hair.next = head
pre = hair
dummy = ListNode(-1)
dummy.next = head
start = dummy

while head:
tail = pre
# 查看剩余部分长度是否大于等于 k
while start:
# 设置end结点,看是否够k个一组
end = start
for i in range(k):
tail = tail.next
if not tail:
return hair.next
nex = tail.next
head, tail = self.reverse(head, tail)
# 把子链表重新接回原链表
pre.next = head
tail.next = nex
pre = tail
head = tail.next
end = end.next
if not end:
return dummy.next

# swap node
swap_a = start.next
swap_b = swap_a.next
for i in range(k - 1):
temp = swap_b.next
swap_b.next = swap_a
swap_a = swap_b
swap_b = temp
temp = start.next
start.next = swap_a
temp.next = swap_b
start = temp
return dummy.next

return hair.next



# @lc code=end

0 comments on commit ff91365

Please sign in to comment.