#1299 Replace Elements with Greatest Element on Right Side
Given an array
arr
, replace every element in that array with the greatest element among the elements to its right, and replace the last element with-1
.After doing so, return the array.
解题思路:
按照题意,直接for循环改变每一个数,取值于右边最大数。最后一个数 = -1。
class Solution:
def replaceElements(self, arr: List[int]) -> List[int]:
for i in range(len(arr)-1):
arr[i] = max(arr[i+1:])
arr[len(arr)-1] = -1
return arr
runtime:
打扰了。不明白为啥那么慢。
看了一下109ms的思路:
默认最大值是-1,for循环是从后往前。所以等于max()的时候就只要比较两个数值就够了。而我之前从前往后是max()要比较剩下的所有。原来是这样才慢。
重新写一遍。
class Solution:
def replaceElements(self, arr: List[int]) -> List[int]:
maxium = -1
for i in range(len(arr)-1, -1, -1):
temp = arr[i]
arr[i] = maxium
if temp > maxium:
maxium = temp
return arr
runtime:
看了discussion forum里 lee215 的解法,更快。不用temp,直接swap。parameter时就默认设置maxium的值为-1。So cool。
class Solution:
def replaceElements(self, arr: List[int], mx=-1) -> List[int]:
for i in range(len(arr)-1, -1, -1):
arr[i], mx = mx, max(mx, arr[i])
return arr
runtime:
#283 Move Zeroes
Given an integer array
nums
, move all0
's to the end of it while maintaining the relative order of the non-zero elements.Note that you must do this in-place without making a copy of the array.
解题思路:
用two pointer法,一个是写入指针 w(rite) ,初始设置为0;一个是读取指针 r(ead),默认为1。
1)先判断写入指针 w 的值是否为零,如果不是,则两个指针都右移一位;如果是,则继续看读取指针的值是否为零。
2)如果读取指针的值不是零,则两值互换后,两个指针都右移一位;如果是零,则读取指针r右移一位,直到读取指针不再是零为止后,让写入指针与读取指针的数值互换。
直到读取指针达到数列的尾端,程序终止。
edge case:数列只有一个元素时,不做任何处理。
class Solution:
def moveZeroes(self, nums: List[int], w=0, r=1) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
length = len(nums) - 1
if length > 0:
while r <= length:
if nums[w] == 0:
if nums[r] != 0:
nums[w], nums[r] = nums[r], 0
w += 1
else:
w += 1
r += 1
.runtime:
参考discussion forum里ZitaoWang的思路:
也是两指针,不过都从0开始。然后如果所在值非零的话,两个指针一起右移;否则,读取指针右移,写入指针不移。这样会更简洁。连edge case也不用判断。(但是如果两个指针重合的话,就等于白做功?)
重新写一遍:
class Solution:
def moveZeroes(self, nums: List[int], write=0) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
for read in range(len(nums)):
if nums[read] != 0:
nums[write], nums[read] = nums[read], nums[write]
write += 1
但runtime反而变差了:
无法理解…… 回头再看看。