【leetcode】1243. Array Transformation
2024-10-07 02:10:50
题目如下:
Given an initial array
arr
, every day you produce a new array using the array of the previous day.On the
i
-th day, you do the following operations on the array of dayi-1
to produce the array of dayi
:
- If an element is smaller than both its left neighbor and its right neighbor, then this element is incremented.
- If an element is bigger than both its left neighbor and its right neighbor, then this element is decremented.
- The first and last elements never change.
After some days, the array does not change. Return that final array.
Example 1:
Input: arr = [6,2,3,4]
Output: [6,3,3,4]
Explanation:
On the first day, the array is changed from [6,2,3,4] to [6,3,3,4].
No more operations can be done to this array.Example 2:
Input: arr = [1,6,3,4,3,5]
Output: [1,4,4,4,4,5]
Explanation:
On the first day, the array is changed from [1,6,3,4,3,5] to [1,5,4,3,4,5].
On the second day, the array is changed from [1,5,4,3,4,5] to [1,4,4,4,4,5].
No more operations can be done to this array.Constraints:
1 <= arr.length <= 100
1 <= arr[i] <= 100
解题思路:题目不难,注意每次变换前先备份arr,再根据备份的arr的值修改本身的arr。
代码如下:
class Solution(object):
def transformArray(self, arr):
"""
:type arr: List[int]
:rtype: List[int]
"""
while True:
arr_bak = arr[::]
for i in range(1,len(arr_bak)-1):
if arr_bak[i] > arr_bak[i-1] and arr_bak[i] > arr_bak[i+1]:
arr[i] -= 1
elif arr_bak[i] < arr_bak[i-1] and arr_bak[i] < arr_bak[i+1]:
arr[i] += 1
if arr_bak == arr:
break
return arr
最新文章
- CSS知识图--转载
- javascript对象(1)
- greenDao 学习之坑 ";java-gen"; 目录下的类不能引用
- Python 批量创建同文件名的特定后缀文件
- 创建eclipse新的workspace并设置workspace共享配置
- iOS蓝牙4.0协议简单介绍
- linux上应用随机启动
- 彻底删除&;quot;提示删除文件和目录&;quot;时出错的文件或目录
- ionic中应用sass
- 计算幂 51Nod 1046 A^B Mod C
- iOS----------Mac维修预约-如何找到电脑维修单
- Java学习笔记之——线程的生命周期、线程同步
- Kafka 详解(三)------Producer生产者
- 小甲鱼Python第二十三讲课后习题--025,字典
- 第48节:Java当中的集合框架
- Running Web API using Docker and Kubernetes
- java 根据身份证号码获取出生日期、性别、年龄
- 一些关于SQL优化的总结
- oozie java api提交作业
- dubbo监控报错Error creating bean with name &#39;uriBrokerService&#39;