Movatterモバイル変換


[0]ホーム

URL:


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

Commit0cd3c5f

Browse files
committed
Update to 033
Update to 033
1 parentd5db77b commit0cd3c5f

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+
classSolution:
16+
defremoveDuplicates(self,nums):
17+
"""
18+
:type nums: List[int]
19+
:rtype: int
20+
"""
21+
ifnotnums:
22+
return0
23+
index=1
24+
start=0
25+
foriinrange(1,len(nums)):
26+
ifnums[start]!=nums[i]:
27+
nums[index]=nums[i]
28+
index+=1
29+
start=i
30+
returnindex
31+
32+
33+
if__name__=="__main__":
34+
assertSolution().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+
classSolution:
11+
defremoveElement(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+
whileleft<=right:
20+
whileleft<=rightandnums[left]!=val:
21+
left+=1
22+
whileleft<=rightandnums[right]==val:
23+
right-=1
24+
ifleft<right:
25+
nums[left]=nums[right]
26+
left+=1
27+
right-=1
28+
returnright+1
29+
30+
31+
if__name__=="__main__":
32+
assertSolution().removeElement([1,2,3,4,3,2,1],1)==5
33+
assertSolution().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+
classSolution:
13+
defstrStr(self,haystack,needle):
14+
"""
15+
:type haystack: str
16+
:type needle: str
17+
:rtype: int
18+
"""
19+
ifnotneedle:
20+
return0
21+
ifnothaystack:
22+
return-1
23+
i=0
24+
needleL=len(needle)
25+
whilei<len(haystack):
26+
a=haystack[i:i+needleL]
27+
ifa==needle:
28+
returni
29+
else:
30+
index=0
31+
try:
32+
index=needle.rindex(haystack[i+needleL])
33+
exceptException:
34+
i+=needleL+1
35+
i+=needleL-index
36+
return-1
37+
38+
39+
if__name__=="__main__":
40+
assertSolution().strStr("abcdefg","ab")==0
41+
assertSolution().strStr("abcdefg","bc")==1
42+
assertSolution().strStr("abcdefg","cd")==2
43+
assertSolution().strStr("abcdefg","fg")==5
44+
assertSolution().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+
classSolution(object):
15+
defsearch(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+
whileleft<=right:
24+
mid=left+ (right-left)//2
25+
ifnums[mid]==target:
26+
returnmid
27+
28+
ifnums[mid]>nums[left]:
29+
ifnums[left]<=target<=nums[mid]:
30+
right=mid-1
31+
else:
32+
left=mid+1
33+
elifnums[mid]<nums[left]:
34+
ifnums[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+
assertSolution().search([4,5,6,7,0,1,2],4)==0
45+
assertSolution().search([4,5,6,7,0,1,2],7)==3
46+
assertSolution().search([4,5,6,7,0,1,2],0)==4
47+
assertSolution().search([4,5,6,7,0,1,2],2)==6
48+
assertSolution().search([3,1],3)==0
49+
assertSolution().search([3,1],1)==1
50+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp