一、数组Array
数组:在连续的内存空间中,存储一组相同类型的元素。比如“[1,2,3]”
区分元素和索引:
区分数组访问和数组搜索:
数组访问(Access):a[1] = 2
数组搜索(Search):找2这个元素
时间复杂度:
数组适合读不适合写,适合于读多写少的情况!
二、Python数组常用操作
1、创建数组: a = [ ]
2、添加元素:
3、访问元素:用索引访问元素
4、更新元素
5、 删除元素(3种方法)
6、获取数组长度
7、 遍历数组(3种方法)
8、查找某个元素
9、数组排序
三、力扣485题
class Solution(object):
def findMaxConsecutiveOnes(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if nums is None or len(nums) == 0:
return 0
count = 0
result = 0
for i in nums:
if i == 1:
count += 1
else:
result = max(result,count)
count = 0
return max(result,count)
四、力扣283题
class Solution(object):
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: None Do not return anything, modify nums in-place instead.
"""
index = 0
for num in nums:
if num != 0:
nums[index]=num
index += 1
for i in range(index,len(nums)):
nums[i] = 0
五、力扣27题
class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
if nums is None or len(nums) == 0:
return 0
l = 0
r = len(nums)-1
while(l<r):
while(l<r and nums[l] != val):
l += 1
while(l<r and nums[r] == val):
r -= 1
nums[l],nums[r] = nums[r],nums[l]
return l if nums[l] == val else l+1
版权声明:本文为m0_52018791原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。