Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0cd3c5f

Browse files
Update to 033
Update to 033
1 parent d5db77b commit 0cd3c5f

File tree

4 files changed

+164
-0
lines changed

4 files changed

+164
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!usr/bin/env python3
2+
# -*- coding:utf-8 -*-
3+
'''
4+
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
5+
6+
Do not allocate extra space for another array, you must do this in place with constant memory.
7+
8+
For example,
9+
Given input array nums = [1,1,2],
10+
11+
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.
12+
'''
13+
14+
15+
class Solution:
16+
def removeDuplicates(self, nums):
17+
"""
18+
:type nums: List[int]
19+
:rtype: int
20+
"""
21+
if not nums:
22+
return 0
23+
index = 1
24+
start = 0
25+
for i in range(1, len(nums)):
26+
if nums[start] != nums[i]:
27+
nums[index] = nums[i]
28+
index += 1
29+
start = i
30+
return index
31+
32+
33+
if __name__ == "__main__":
34+
assert Solution().removeDuplicates([1, 1, 2]) == 2
35+

‎Python3/027_Remove_Element.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!usr/bin/env python3
2+
# -*- coding:utf-8 -*-
3+
'''
4+
Given an array and a value, remove all instances of that value in place and return the new length.
5+
6+
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
7+
'''
8+
9+
10+
class Solution:
11+
def removeElement(self, nums, val):
12+
"""
13+
:type nums: List[int]
14+
:type val: int
15+
:rtype: int
16+
"""
17+
left = 0
18+
right = len(nums) - 1
19+
while left <= right:
20+
while left <= right and nums[left] != val:
21+
left += 1
22+
while left <= right and nums[right] == val:
23+
right -= 1
24+
if left < right:
25+
nums[left] = nums[right]
26+
left += 1
27+
right -= 1
28+
return right + 1
29+
30+
31+
if __name__ == "__main__":
32+
assert Solution().removeElement([1, 2, 3, 4, 3, 2, 1], 1) == 5
33+
assert Solution().removeElement([2], 3) == 1
34+

‎Python3/028_Implement_strStr().py‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!usr/bin/env python3
2+
# -*- coding:utf-8 -*-
3+
'''
4+
Implement strStr().
5+
6+
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
7+
8+
Subscribe to see which companies asked this question
9+
'''
10+
11+
12+
class Solution:
13+
def strStr(self, haystack, needle):
14+
"""
15+
:type haystack: str
16+
:type needle: str
17+
:rtype: int
18+
"""
19+
if not needle:
20+
return 0
21+
if not haystack:
22+
return -1
23+
i = 0
24+
needleL = len(needle)
25+
while i < len(haystack):
26+
a = haystack[i:i + needleL]
27+
if a == needle:
28+
return i
29+
else:
30+
index = 0
31+
try:
32+
index = needle.rindex(haystack[i + needleL])
33+
except Exception:
34+
i += needleL + 1
35+
i += needleL - index
36+
return -1
37+
38+
39+
if __name__ == "__main__":
40+
assert Solution().strStr("abcdefg", "ab") == 0
41+
assert Solution().strStr("abcdefg", "bc") == 1
42+
assert Solution().strStr("abcdefg", "cd") == 2
43+
assert Solution().strStr("abcdefg", "fg") == 5
44+
assert Solution().strStr("abcdefg", "bcf") == -1
45+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!usr/bin/env python3
2+
# -*- coding:utf-8 -*-
3+
'''
4+
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
5+
6+
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
7+
8+
You are given a target value to search. If found in the array return its index, otherwise return -1.
9+
10+
You may assume no duplicate exists in the array.
11+
'''
12+
13+
14+
class Solution(object):
15+
def search(self, nums, target):
16+
"""
17+
:type nums: List[int]
18+
:type target: int
19+
:rtype: int
20+
"""
21+
left = 0
22+
right = len(nums) - 1
23+
while left <= right:
24+
mid = left + (right - left) // 2
25+
if nums[mid] == target:
26+
return mid
27+
28+
if nums[mid] > nums[left]:
29+
if nums[left] <= target <= nums[mid]:
30+
right = mid - 1
31+
else:
32+
left = mid + 1
33+
elif nums[mid] < nums[left]:
34+
if nums[mid] <= target <= nums[right]:
35+
left = mid + 1
36+
else:
37+
right = mid - 1
38+
else:
39+
left += 1
40+
return -1
41+
42+
43+
if __name__ == "__main__":
44+
assert Solution().search([4, 5, 6, 7, 0, 1, 2], 4) == 0
45+
assert Solution().search([4, 5, 6, 7, 0, 1, 2], 7) == 3
46+
assert Solution().search([4, 5, 6, 7, 0, 1, 2], 0) == 4
47+
assert Solution().search([4, 5, 6, 7, 0, 1, 2], 2) == 6
48+
assert Solution().search([3, 1], 3) == 0
49+
assert Solution().search([3, 1], 1) == 1
50+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /