题目如下:

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 day i-1 to produce the array of day i:

  1. If an element is smaller than both its left neighbor and its right neighbor, then this element is incremented.
  2. If an element is bigger than both its left neighbor and its right neighbor, then this element is decremented.
  3. 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

最新文章

  1. CSS知识图--转载
  2. javascript对象(1)
  3. greenDao 学习之坑 &quot;java-gen&quot; 目录下的类不能引用
  4. Python 批量创建同文件名的特定后缀文件
  5. 创建eclipse新的workspace并设置workspace共享配置
  6. iOS蓝牙4.0协议简单介绍
  7. linux上应用随机启动
  8. 彻底删除&amp;quot;提示删除文件和目录&amp;quot;时出错的文件或目录
  9. ionic中应用sass
  10. 计算幂 51Nod 1046 A^B Mod C
  11. iOS----------Mac维修预约-如何找到电脑维修单
  12. Java学习笔记之——线程的生命周期、线程同步
  13. Kafka 详解(三)------Producer生产者
  14. 小甲鱼Python第二十三讲课后习题--025,字典
  15. 第48节:Java当中的集合框架
  16. Running Web API using Docker and Kubernetes
  17. java 根据身份证号码获取出生日期、性别、年龄
  18. 一些关于SQL优化的总结
  19. oozie java api提交作业
  20. dubbo监控报错Error creating bean with name &#39;uriBrokerService&#39;

热门文章

  1. Linux Shell输出颜色字符学习笔记(附Python脚本实现自动化定制生成)
  2. windows 装mac
  3. Python中的 _init__和 _new__的区别
  4. linux 下如何将网页版应用生成桌面图标
  5. C语言函数调用时候内存中栈的动态变化详细分析(彩图)
  6. Git复习(一)之简介、安装、集中式和分布式
  7. 使用@ResponseBody输出JSON
  8. Redis分布式之前篇
  9. js 重要函数
  10. 错误信息 NSError