【leetcode】1089. Duplicate Zeros

题目如下:

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,如果arr[i]等于0,在i+1位置插入0,索引移动到i+2;如果不为0,索引移动到i+1。

代码如下:

class Solution(object):
    def duplicateZeros(self, arr):
        """
        :type arr: List[int]
        :rtype: None Do not return anything, modify arr in-place instead.
        """
        length = len(arr)
        inx = 0
        while inx < length:
            if arr[inx] != 0:
                inx += 1
                continue
            else:
                arr.insert(inx,0)
                arr.pop(-1)
                inx += 2

原文地址:https://www.cnblogs.com/seyjs/p/11044681.html

时间: 2024-10-12 17:32:31

【leetcode】1089. Duplicate Zeros的相关文章

【LeetCode】283. Move Zeros

题目 Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0]. Note:Yo

【Leetcode】Delete Duplicate Emails

题目链接:https://leetcode.com/problems/delete-duplicate-emails/ 题目: Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id. +--+------+ | Id | Email | +--+------+ | 1 | [email 

【leetcode】Contains Duplicate &amp; Rectangle Area(easy)

Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. 思路:简单题用set bool cont

【LeetCode】Contains Duplicate II

Contains Duplicate II 问题描述 Given an array of integers and an integer k, find out whether there there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k. 算法思想 思想一: 两个数组下标i,j,在叫j-i<

【LeetCode】Contains Duplicate II 解题小结

题目:Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k. 题目意思就是判断在一个数组中是否存在两个相同的元素,他们之间的距离在k以内. 自己开始的思路是嵌套的两个for循

【leetcode】Set Matrix Zeros

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. Follow up:Did you use extra space?A straight forward solution using O(mn) space is probably a bad idea.A simple improvement uses O(

【leetcode】Contains Duplicate

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. 1 class Solution { 2 public: 3 bool con

【LeetCode】Subsets 解题报告

[题目] Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,

【LeetCode】Subsets II 解题报告

[题目] Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,2], a solutio