Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions yuhuacao/ReverseLinkedList.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
题目:
给定一个链表,把这个链表反转过来。
示例:
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
解题思路:
比较简单,对链表遍历,需保存上一个指针和下一个指针。在遍历过程中理清三者关系,注意不要互相覆盖即可。

# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None

class Solution(object):
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head==None:
return []
last_temp = None
temp = head
while(temp!=None):
temp_next = temp.next
temp.next = last_temp
last_temp = temp
temp = temp_next

return last_temp
2 changes: 2 additions & 0 deletions yuhuacao/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@

1. Merge Sorted Array
2. Maximum Length of Repeated Subarray
3. Reverse Linked List
4. Three Sum
45 changes: 45 additions & 0 deletions yuhuacao/threeSum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note: The solution set must not contain duplicate triplets.

For example, given array S = [-1, 0, 1, 2, -1, -4],

A solution set is: [ [-1, 0, 1], [-1, -1, 2] ]
"""
"""
解题思路:
1,先将数组排序。
2,排序后,可以按照TwoSum的思路来解题。怎么解呢?可以将num[i]的相反数即-num[i]作为target,然后从i+1到len(num)-1的数组元素中寻找两个数使它们的和为-num[i]就可以了。下标i的范围是从0到len(num)-3。
3,这个过程要注意去重。
4,时间复杂度为O(N^2)。
"""


class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
res = []
nums.sort()
for i in range(0, len(nums)):
if i > 0 and nums[i] == nums[i - 1]:
continue
target = 0 - nums[i]
start, end = i + 1, len(nums) - 1
while start < end:
if nums[start] + nums[end] > target:
end -= 1
elif nums[start] + nums[end] < target:
start += 1
else:
res.append((nums[i], nums[start], nums[end]))
end -= 1
start += 1
while start < end and nums[end] == nums[end + 1]:
end -= 1
while start < end and nums[start] == nums[start - 1]:
start += 1
return res