Leetcode - Sort Transformed Array
Leetcode - Sort Array By Parity

Leetcode - Duplicate Zeros

violet posted @ Jul 14, 2020 04:23:12 AM in 算法 with tags Algorithm Golang array , 457 阅读

https://leetcode.com/problems/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]

 

func duplicateZeros(arr []int)  {
    count := 0
    for _, n := range arr {
        if n == 0 {
            count++
        }
    }
    if count == 0 {
        return
    }
    
    for i := len(arr)-1; i >= 0; i-- {
        if arr[i] == 0 {
            count--
        }
        newIndex := i + count
        if newIndex >= len(arr) {
            continue
        }
        
        arr[newIndex] = arr[i]
        if arr[i] == 0 && newIndex+1 < len(arr) {
            arr[newIndex+1] = 0
        }
  
    }
}
Delete Instagram Acc 说:
Dec 21, 2022 10:18:08 PM

Instagram is a well-known social networking application in the world. It enables users to speak with and connect with individuals all around the world. Users of Instagram have the facility to share photos, videos, stories and etc to make followers, which helps to grow as a celebrity if you are a common individual. Delete Instagram Account Temporarily Based on personal reasons some users are going to delete their Instagram accounts. Before deleting their account, should closely review if you truly want it or not.

Emma 说:
Jan 20, 2023 07:19:59 PM

Here they have shared an update regarding programs. They explain the details regarding the program of finding the duplicate zeros in the program. The program is well CBD Topical Vs CBD Vapes written here and such kinds of codes are really helping people to understand more about such programs and keep sharing more details here.

BSNL Portal Recharge 说:
Feb 06, 2023 04:17:53 PM

The best facility for every prepaid mobile customer is online recharge to feel proud for managing recharges from own hands with new offers provided by Bharat Sanchar Nigam Limited. This may possible through a new website call as BSNL Quick Recharge Portal or with My BSNL App. BSNL Portal Recharge This is one of the best service to avail Full Talktime Top Up, STV’s or any latest prepaid mobile plan recharges online. It is like an instant service, but from anywhere on just log in to BSNL Recharge Portal.

pavzi.com 说:
Jan 28, 2024 01:17:46 AM

Pavzi website is a multiple Niche or category website which will ensure to provide information and resources on each and every topic. Some of the evergreen topics you will see on our website are Career, Job Recruitment, Educational, Technology, Reviews and others. pavzi.com We are targeting mostly so it is true that Tech, Finance, and Product Reviews. The only reason we have started this website is to make this site the need for your daily search use.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter