Nuggets team number online, help you Offer impromptu! Click on theCheck the details

preface

The first time to participate in the Nuggets punch card activity, among other things, mainly rush to reward. 4.12 Start to rush to achieve the small goal of question 14!! This is problem number eight.

Topic describes

Delete duplicates from an ordered array

For the title description, I mainly use the screenshot leetcode, so the title is as follows

Thought analysis

At first, I was thinking that it would be a simple array to undo quickly

The commit found an errorLooks like we need to start over

1. Give you an ordered array nums, ask you to delete the repeated elements, make each element appear only once, return the new length of the array after deletion. See this sentence there is no familiar feeling js splice

2. Nums are sorted in ascending order

Use the easiest way to iterate through the list and then use splice to delete duplicate items

Try it on:Passed with flying colors

AC code

/** * @param {number[]} nums * @return {number} */ var removeDuplicates = function(nums) { if(nums.length===0){ return 0  } for(let i = 0; i < nums.length; i++) { if(nums[i] === nums[i+1]) { nums.splice(i, 1) i = i - 1 } } return nums.length };Copy the code

conclusion

Persistence is victory. Problem 8 algorithm complete, persistence is victory!!

Left left left

→ Algorithm series link ←

Write write write

You can order it here! You can order it here! You can order it here!