当前位置:网站首页>数组 移动0
数组 移动0
2022-06-10 20:50:00 【Morris_】
LC 数组 移动零
- Swift
输入 [0, 0, 1]
var nums: [Int] = [0, 0, 1]
moveZeroes(&nums)
print(nums)
输出 [1, 0, 0]
实现一
func moveZeroes(_ nums: inout [Int]) {
var temp: [Int] = []
var index = 0
while index < nums.count {
if nums[index] == 0 {
temp.append(nums[index])
nums.remove(at: index)
}
else {
index += 1
}
}
nums += temp
}
实现二
func moveZeroes(_ nums: inout [Int]) {
// 循环次数
var count = 1
// 数组下标
var index = 0
while count < nums.count && index < nums.count {
if nums[index] == 0 {
nums.remove(at: index)
nums.append(0)
}
else {
index += 1
}
count += 1
}
}
边栏推荐
- A WPF developed Print dialog box -printdialogx
- C language -- 1 c language cognition
- Cordova Plugin /JPush PhoneGap 极光推送_本地推送_消息推送
- Extracting Nalu unit data from H264 real-time stream
- Super detailed tutorial for installing mysql8 in centos7 (no pit!)
- About type-C
- ThinkPHP v6.0.x反序列化漏洞复现
- Mysql之将查询结果插入到其它表中
- Can I make up the exam if I fail the soft exam? Here comes the answer
- C language ---9 first knowledge of macros and pointers
猜你喜欢

学IT毕业后该去哪个城市?哪个岗位薪资高?哪些公司待遇好?

用少儿编程思维塑造青少年领悟能力

登堂入室之soc开发环境及硬件开发准备
![[nk] 牛客月赛51 F-平均题](/img/b3/c36a0032e606f38fdc2f7c4562713c.png)
[nk] 牛客月赛51 F-平均题

Install MySQL on Linux system. Problems encountered in xshell

C language -- 11 branch statement if else

As a programmer, is it really that important for the underlying principles?

JS anchor positioning can extend many functions

Acl2022 | bert2bert: an efficient pre training method of parameter reuse, which significantly reduces the training cost of oversized models

Standard dual airbags, starting from 48900 for butcher Chang'an Lumin
随机推荐
Abbexa acrylamide peg NHS instructions
Install MySQL on Linux system. Problems encountered in xshell
Exec function of PHP
Abbexa AML1 DNA binding ELISA Kit instructions
An analysis of SQL query optimization principle (900w+ data from 17s to 300ms)
在模仿学习中进步的智能机器人
About type-C
MySQL数据库如何查看表占用空间大小
2022-06-09 RK817 PMU 电池温度检测
自媒体视频创作如何选择领域?
Summary of common mysql8 commands in centos7 environment
Abbkine column exkine Pro animal cell / tissue Total Protein Extraction Kit
C language -- 8 familiar keywords
What do software test engineers do?
SQL第四练:字符串处理函数
Introduction to database system -- Chapter 1 -- Introduction (important knowledge points)
Constructing the implementation strategy of steam education for children
自制Table表格
Asynchronous, thread pool (completablefuture)
CCF class a conference or journal - regression related papers