Skip to content

Commit 3d7da1a

Browse files
committed
Adding colution of 75, 78, 79, 80, 82, 83 problems
1 parent e4a529d commit 3d7da1a

File tree

7 files changed

+246
-0
lines changed

7 files changed

+246
-0
lines changed

75.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'''
2+
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.
3+
4+
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
5+
6+
Note: You are not suppose to use the library's sort function for this problem.
7+
8+
Example:
9+
10+
Input: [2,0,2,1,1,0]
11+
Output: [0,0,1,1,2,2]
12+
'''
13+
class Solution(object):
14+
def sortColors(self, nums):
15+
"""
16+
:type nums: List[int]
17+
:rtype: void Do not return anything, modify nums in-place instead.
18+
"""
19+
zero, last = 0, len(nums)-1
20+
index = 0
21+
while index <= last:
22+
if nums[index] == 1:
23+
index += 1
24+
elif nums[index] == 0:
25+
nums[index], nums[zero] = nums[zero], nums[index]
26+
index += 1
27+
zero += 1
28+
elif nums[index] == 2:
29+
nums[last], nums[index] = nums[index], nums[last]
30+
last -= 1

78.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'''
2+
Given a set of distinct integers, nums, return all possible subsets (the power set).
3+
'''
4+
5+
class Solution(object):
6+
def subsets(self, nums):
7+
"""
8+
:type nums: List[int]
9+
:rtype: List[List[int]]
10+
"""
11+
result = [[]]
12+
for num in nums:
13+
for j in range(len(result)):
14+
result.append(result[j] + [num])
15+
return result

79.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'''
2+
Given a 2D board and a word, find if the word exists in the grid.
3+
4+
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
5+
6+
Example:
7+
8+
board =
9+
[
10+
['A','B','C','E'],
11+
['S','F','C','S'],
12+
['A','D','E','E']
13+
]
14+
15+
Given word = "ABCCED", return true.
16+
Given word = "SEE", return true.
17+
Given word = "ABCB", return false.
18+
'''
19+
20+
class Solution(object):
21+
def exist(self, board, word):
22+
"""
23+
:type board: List[List[str]]
24+
:type word: str
25+
:rtype: bool
26+
"""
27+
28+
result = False
29+
for row in range(len(board)):
30+
for col in range(len(board[0])):
31+
if self.dfs(board, word, row, col, 0):
32+
return True
33+
return False
34+
35+
def dfs(self, board, word, row, col, curr_len):
36+
if row < 0 or col < 0 or row >= len(board) or col >= len(board[0]):
37+
return False
38+
if board[row][col] == word[curr_len]:
39+
c = board[row][col]
40+
board[row][col] = '#'
41+
42+
if curr_len == len(word) - 1:
43+
return True
44+
elif (self.dfs(board, word, row-1, col, curr_len+1) or self.dfs(board, word, row+1, col, curr_len+1) or self.dfs(board, word, row, col-1, curr_len+1) or self.dfs(board, word, row, col+1, curr_len+1)):
45+
return True
46+
47+
board[row][col] = c
48+
return False

80.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'''
2+
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length.
3+
4+
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
5+
6+
Example 1:
7+
8+
Given nums = [1,1,1,2,2,3],
9+
10+
Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively.
11+
12+
It doesn't matter what you leave beyond the returned length.
13+
'''
14+
15+
class Solution(object):
16+
def removeDuplicates(self, nums):
17+
"""
18+
:type nums: List[int]
19+
:rtype: int
20+
"""
21+
if len(nums) <= 2:
22+
return len(nums)
23+
24+
prev, curr = 1, 2
25+
26+
while curr < len(nums):
27+
if nums[prev] == nums[curr] and nums[curr] == nums[prev-1]:
28+
curr += 1
29+
else:
30+
prev += 1
31+
nums[prev] = nums[curr]
32+
curr += 1
33+
return prev+1

81.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'''
2+
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
3+
4+
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
5+
6+
You are given a target value to search. If found in the array return true, otherwise return false.
7+
8+
Example 1:
9+
10+
Input: nums = [2,5,6,0,0,1,2], target = 0
11+
Output: true
12+
Example 2:
13+
14+
Input: nums = [2,5,6,0,0,1,2], target = 3
15+
Output: false
16+
'''
17+
18+
class Solution(object):
19+
def search(self, nums, target):
20+
"""
21+
:type nums: List[int]
22+
:type target: int
23+
:rtype: bool
24+
"""
25+
left, right = 0, len(nums) -1
26+
while left <= right:
27+
mid = (left + right) / 2
28+
if nums[mid] == target:
29+
return True
30+
if nums[left] < nums[mid]:
31+
if nums[left] <= target < nums[mid]:
32+
right = mid - 1
33+
else:
34+
left = mid + 1
35+
elif nums[mid] < nums[left]:
36+
if nums[mid] < target <= nums[right]:
37+
left = mid + 1
38+
else:
39+
right = mid -1
40+
else:
41+
left += 1
42+
43+
return False

82.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'''
2+
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
3+
4+
Example 1:
5+
6+
Input: 1->2->3->3->4->4->5
7+
Output: 1->2->5
8+
Example 2:
9+
10+
Input: 1->1->1->2->3
11+
Output: 2->3
12+
'''
13+
14+
# Definition for singly-linked list.
15+
# class ListNode(object):
16+
# def __init__(self, x):
17+
# self.val = x
18+
# self.next = None
19+
20+
class Solution(object):
21+
def deleteDuplicates(self, head):
22+
"""
23+
:type head: ListNode
24+
:rtype: ListNode
25+
"""
26+
if not head:
27+
return None
28+
29+
result = ListNode(0)
30+
ans = result
31+
curr = head
32+
while curr:
33+
value = curr.val
34+
count = 0
35+
while curr and curr.val == value:
36+
curr = curr.next
37+
count += 1
38+
if count == 1:
39+
result.next = ListNode(value)
40+
result = result.next
41+
return ans.next

83.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'''
2+
Given a sorted linked list, delete all duplicates such that each element appear only once.
3+
4+
Example 1:
5+
6+
Input: 1->1->2
7+
Output: 1->2
8+
Example 2:
9+
10+
Input: 1->1->2->3->3
11+
Output: 1->2->3
12+
'''
13+
14+
# Definition for singly-linked list.
15+
# class ListNode(object):
16+
# def __init__(self, x):
17+
# self.val = x
18+
# self.next = None
19+
20+
class Solution(object):
21+
def deleteDuplicates(self, head):
22+
"""
23+
:type head: ListNode
24+
:rtype: ListNode
25+
"""
26+
if not head:
27+
return None
28+
29+
curr = head
30+
while curr and curr.next:
31+
if curr.val == curr.next.val:
32+
curr.next = curr.next.next
33+
else:
34+
curr = curr.next
35+
36+
return head

0 commit comments

Comments
 (0)