LeetCode题目
代码实现:Go语言
func moveZeroes(nums []int) {
// p为去除0的结果
p:=removeElement(nums,0)
// 将后面的值补0
for p < len(nums){
nums[p] = 0
p++
}
}
//27题代码
func removeElement(nums []int, val int) int {
fast,slow:=0,0
for fast<len(nums){
if nums[fast]!=val{
nums[slow]=nums[fast]
slow++
}
fast++
}
return slow
}
解题思路:将所有0去掉(详见27题)再把后面的元素都赋值为 0 。
这篇博客介绍了如何使用Go语言解决LeetCode中的两个问题:一是移除数组中的指定元素;二是将数组中所有的0移到末尾并填充0。通过27题的removeElement函数移除0,然后遍历数组将其余位置填充0。这种方法有效地实现了数据结构的变换。
943

被折叠的 条评论
为什么被折叠?



