日月换新天。为有牺牲多壮志,敢教

[Swift]LeetCode1089. 复写零 | Duplicate Zeros

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/11014391.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Given a fixed length array arr of integers, duplicate each occurrence of zero, shifting the remaining elements to the right.

Note that elements beyond the length of the original array are not written.

Do the above modifications to the input array in place, do not return anything from your function. 

Example 1:

Input: [1,0,2,3,0,4,5,0]
Output: null
Explanation: After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4]

Example 2:

Input: [1,2,3]
Output: null
Explanation: After calling your function, the input array is modified to: [1,2,3] 

Note:

  1. 1 <= arr.length <= 10000
  2. 0 <= arr[i] <= 9

给你一个长度固定的整数数组 arr,请你将该数组中出现的每个零都复写一遍,并将其余的元素向右平移。

注意:请不要在超过该数组长度的位置写入元素。

要求:请对输入的数组 就地 进行上述修改,不要从函数返回任何东西。 

示例 1:

输入:[1,0,2,3,0,4,5,0]
输出:null
解释:调用函数后,输入的数组将被修改为:[1,0,0,2,3,0,0,4]

示例 2:

输入:[1,2,3]
输出:null
解释:调用函数后,输入的数组将被修改为:[1,2,3] 

提示:

  1. 1 <= arr.length <= 10000
  2. 0 <= arr[i] <= 9

Runtime: 40 ms
Memory Usage: 21.1 MB
复制代码
 1 class Solution {
 2     func duplicateZeros(_ arr: inout [Int]) {
 3         var n:Int = arr.count
 4         var a:[Int] = arr
 5         var p:Int = 0
 6         for i in 0..<n
 7         {
 8             if a[i] == 0
 9             {
10                 if p < n 
11                 {
12                     arr[p] = 0
13                     p += 1
14                 }
15                 if p < n
16                 {
17                     arr[p] = 0
18                     p += 1
19                 }
20             }
21             else
22             {
23                 if p < n
24                 {
25                     arr[p] = a[i]
26                     p += 1
27                 }
28             }
29         }
30         
31     }
32 }
复制代码

44ms

 

复制代码
 1 class Solution {
 2     func duplicateZeros(_ arr: inout [Int]) {
 3         let c = arr.count
 4         if c <= 1 { return }
 5 
 6         var i = 0
 7         while i < c {
 8             if arr[i] == 0 {
 9                 arr.insert(0, at: i)
10                 i += 2
11             } else {
12                 i += 1
13             }
14         }
15         while arr.count > c {
16             _ = arr.popLast()
17         }
18     }
19 }
复制代码

48ms

复制代码
 1 class Solution {
 2     func duplicateZeros(_ arr: inout [Int]) {
 3         var count = 0
 4         for (index, value) in arr.enumerated() {
 5             if value == 0 {
 6                 count += 1
 7             }
 8         }
 9         var index = arr.count - 1
10         while index >= 0 {
11             let value = arr[index]
12             if index + count < arr.count {
13                 arr[index + count] = value
14             }
15             if value == 0 {
16                 count -= 1
17                 if index + count < arr.count {
18                     arr[index + count] = 0
19                 }
20             }
21             index -= 1
22         }
23     }
24 }
复制代码

 

posted @   为敢技术  阅读(593)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°