题目描述:

自己的提交:

class Solution:
def transformArray(self, arr: List[int]) -> List[int]:
if len(arr) < 3: return arr
flag = True
while flag:
tmp = []
tmp.append(arr[0])
for i in range(1,len(arr)-1):
if arr[i] > arr[i-1] and arr[i] > arr[i+1]:
tmp.append(arr[i] - 1)
elif arr[i] < arr[i-1] and arr[i] < arr[i+1]:
tmp.append(arr[i] + 1)
else:
tmp.append(arr[i])
tmp.append(arr[-1])
if arr == tmp:
flag = False
arr = tmp
return arr

优化:

class Solution:
def transformArray(self, A: List[int]) -> List[int]:
change = 1
n = len(A)
while change:
change = 0
B = A[:]
for i in range(1, n - 1):
if A[i-1] < A[i] > A[i+1]:
B[i] -= 1
change = 1
if A[i-1] > A[i] < A[i+1]:
B[i] += 1
change = 1
A = B
return B

leetcode-12双周赛-1243-数组变换的更多相关文章

  1. LeetCode 1243 数组变换

    地址 https://leetcode-cn.com/contest/biweekly-contest-12/problems/array-transformation/ 首先,给你一个初始数组 ar ...

  2. LeetCode:寻找旋转排序数组中的最小值【153】

    LeetCode:寻找旋转排序数组中的最小值[153] 题目描述 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0 ...

  3. 【LeetCode】搜索旋转排序数组【两次二分】

    假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 搜索一个给定的目标值,如果数组中存在这个目标值, ...

  4. Leetcode题库——6.Z字形变换

    @author: ZZQ @software: PyCharm @file: convert.py @time: 2018/9/20 20:12 要求: Z字形变换 将字符串 "PAYPAL ...

  5. LeetCode 81——搜索旋转排序数组 II

    1. 题目 2. 解答 2.1. 方法一 基于 LeetCode 33--搜索旋转排序数组 中的方法二. 当 nums[mid] = nums[right] 时,比如 [1, 1, 2, 1, 1], ...

  6. LeetCode:搜索旋转排序数组【33】

    LeetCode:搜索旋转排序数组[33] 题目描述 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2]  ...

  7. Leetcode(6)Z字形变换

    Leetcode(6)Z字形变换 [题目表述]: 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" ...

  8. leetcode 12题 数字转罗马数字

    leetcode 12题 数字转罗马数字 答案一:我的代码 代码本地运行完全正确,在线运行出错 class Solution { public: string intToRoman(int num) ...

  9. [每日一题2020.06.16] leetcode双周赛T3 5423 找两个和为目标值且不重叠的子数组 DP, 前缀和

    题目链接 给你一个整数数组 arr 和一个整数值 target . 请你在 arr 中找 两个互不重叠的子数组 且它们的和都等于 target .可能会有多种方案,请你返回满足要求的两个子数组长度和的 ...

随机推荐

  1. List Comprehension ()(二)

    在list comprehension中加入if条件判断: >>> lines = [line.rstrip() for line in open('script2.py') if ...

  2. 职责链模式ChainOfResponsibility

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11407114.html  1.定义 使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合 ...

  3. 文本处理工具——sed基础

    一sed介绍 三剑客是grep,sed,awk,功能都很强大. 其中sed是Stream EDitor,流编辑器 行,编辑器的简写,它一次处理一行内容. sed的强大在于可以对文件进行修改,很适合在脚 ...

  4. sql的分页

    public static string GetPageSql(string sql, int start, int end)        {            return string.Fo ...

  5. php time()函数 语法

    php time()函数 语法 time()函数怎么用? php time()函数用来返回当前时间的unix时间戳.语法是time(),返回自从 Unix 纪元(格林威治时间 1970 年 1 月 1 ...

  6. http中请求协议 GET和POST两种基本请求方法的区别

    GET和POST是什么?HTTP协议中的两种发送请求的方法. HTTP是什么?HTTP是基于TCP/IP的关于数据如何在万维网中如何通信的协议. HTTP的底层是TCP/IP.所以GET和POST的底 ...

  7. setTag()与getTag()的使用介绍

    转载博客:http://www.cnblogs.com/topcoderliu/archive/2011/06/07/2074419.html View中的setTag(Onbect)表示给View添 ...

  8. 栈Stack --- 数组实现

    栈最大的一个特点就是先进后出(FILO—First-In/Last-Out). /** * 栈:后进先出 * Created by fred on 2018/7/31. */ public class ...

  9. strlen、strcmp、strcat、strlen、memmove

    #include <cassert> #include <iostream> using namespace std; /* strlen 返回字符串不包含结束符\0的长度 * ...

  10. mysql添加制定ip访问

    GRANT ALL PRIVILEGES ON *.* TO 'root'@'120.244.114.45' IDENTIFIED BY 'Zhh722@7758521' WITH GRANT OPT ...