Two Pointers

1.Easy

(2)Add Two Numbers

(26)Remove Element from Sorted Array

Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once.(巨大前提是数据广义单增) The relative order of the elements should be kept the same. Then return the number of unique elements in nums.

Consider the number of unique elements of nums to be k, to get accepted, you need to do the following things:

Change the array nums such that the first k elements of nums contain the unique elements in the order they were present in nums initially. The remaining elements of nums are not important as well as the size of nums. Return k.

1
2
3
4
5
6
7
8
class Solution(object):
def removeDuplicates(self, nums):
dummy=1
for i in range(1,len(nums)):
if nums[i]!=nums[i-1]:
nums[dummy]=nums[i]
dummy+=1
return dummy

(27)Remove Elements

1
2
3
4
5
6
7
8
class Solution(object):
def removeElement(self, nums, val):
index=0
for i in range(len(nums)):
if nums[i]!=val:
nums[index]=nums[i]
index+=1
return index

(28) Find the Index of the First Occurrence in a String

1
2
3
4
5
6
class Solution(object):
def strStr(self, haystack, needle):
for i in range(len(haystack)):
if haystack[i:i+len(needle)]==needle:
return i
return -1

(141)Linked List Cycle

(283)Move Zeroes

(345)Reverse Vowels of a String

知识点:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
s= "asvisl"
s_list=list(s)
c_list=list(reversed(s_list))
s=''.join(c_list)
print(s_list,c_list,s)

#result:['a', 's', 'v', 'i', 's', 'l'] ['l', 's', 'i', 'v', 's', 'a'] lsivsa

class Solution:
def reverseVowels(self,s):
s_list=list(s)
c_list=list(reversed(s_list))
s=''.join(c_list)
return s

答案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution:
def reverseVowels(self, s):
Vowels=set('aeiouAEIOU')
s=list(s)
i,j=0,len(s)-1
while i<j:
if s[i] not in Vowels:
i=i+1
elif s[j] not in Vowels:
j=j-1
else:
s[i],s[j]=s[j],s[i]
i=i+1
j=j-1
return ''.join(s)

Palindrome questions:
(9) Palindrome Number
(345) Reverse Vowels of a String
(680) Valid Palindrome II

(680)Valid Palindrome II

1
2
3
4
5
6
7
8
9
class Solution(object):
def validPalindrome(self, s):
l,r=0,len(s)-1
while l<r:
if s[l]!=s[r]:
return s[l+1:r+1] == s[l+1:r+1][::-1] or s[l:r] == s[l:r][::-1]
l+=1
r-=1
return True

Palindrome questions:
(9) Palindrome Number
(345) Reverse Vowels of a String
(680) Valid Palindrome II

2.Medium

(5)Longest Palindromic Substring

(15)3Sum

(19)Remove Nth Node From End of List

(31)Next Permutation

(142)Linked List Cycle II

(148)Sort List

(253)Meeting Rooms II

(581)Shortest Unsorted Continuous Subarray

(633)Sum of Square Numbers

1
2
3
4
5
6
7
class Solution(object):
def judgeSquareSum(self, c):
for i in range(c):
for j in range(c):
if i**2+j**2==c:
return True
return False