leetcode解题笔记-Remove Element

题目要求:

去除所有数组内与所给值相等的元素,并且返回新数组的长度。

个人解法:

1.定义一个int len=nums.length当有nums[i]==value时,len--,但是这个仅仅是返回新数组的长度,并没有做到剔除相等元素

参考解法:

1.核心在于,i的值从前往后,len的值从后往前

2.i在外循环递增,len在内循环递减

3.利用nums[--len]的值来代替与value相等的值

代码:

for(int i=0 ;i< len; i++){

while(nums[i]==value&&i<len)

nums[i]=nums[--len]

}

最后,有关i--与--i,一个是先赋值再运算,另外一个是先运算再赋值,例如题中如果--len改为len--就会导致边界溢出

时间: 2024-10-07 22:56:48

leetcode解题笔记-Remove Element的相关文章

leetcode解题笔记-Remove Duplicates from Sorted Array

题目要求: 去除数组中相重复的的元素,并且返回新数组的长度.(要求不能再申请额外空间,只能在原来数组上进行操作) 个人理解: 1. 数组定义之后就是定长的不能改变,只能在原来数组上进行操作,不能像链表一样根据指针进行操作 2. 与remove element相似,利用数组中后续的元素进行代替,将数组的一部分变为无重复且排序好的结果子数组: if(nums[i-1]!=nums[i]) nums[id++] = nums[i]; // 如果前一项的值不等于后一项,那么在id+1项的值就等于当前项,

Leetcode 线性表 Remove Element

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Element Total Accepted: 13840 Total Submissions: 42676 Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be change

【LeetCode】27. Remove Element 解题小结

题目:Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. The order of elements can be changed. It doesn't matt

122. Best Time to Buy and Sell Stock(二) leetcode解题笔记

122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell on

121. Best Time to Buy and Sell Stock (一) leetcode解题笔记

121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algor

LeetCode(27)题解:Remove Element

https://leetcode.com/problems/remove-element/ Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. 思路: 因为可以改变元

[Leetcode][Python]27: Remove Element

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 27: Remove Elementhttps://oj.leetcode.com/problems/remove-element/ Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can b

Leetcode解题笔记-Contains Duplicate &amp;&amp; Contains Duplicate II&amp;&amp;Contain Duplicate III

Contain Duplicate 题目要求: 给定一个数组如果有重复就返回true,如果没有就返回false 个人分析: 这个题比剔除重复的要简单许多,只需要判断就好 代码如下: public static boolean containsDuplicate(int[] nums){ if (nums.length==0) return false; Arrays.sort(nums); for(int i =1; i < nums.length; i++){ if(nums[i-1]==nu

LeetCode:27. Remove Element(Easy)

1. 原题链接 https://leetcode.com/problems/remove-element/description/ 2. 题目要求 给定一个整数数组 nums[ ] 和一个整数 val,删除数组中与val相同的元素,并返回删除后的数组长度 注意:不能定义新的数组,只能使用O(1)空间大小 3. 解题思路 遍历一次,将每个元素与给定的value进行比较,不同则给nums[count++]赋予当前元素的值:相同则直接跳过,最后返回count,即为删除后数组的长度. 4. 代码实现 p