LeetCode记录之27——Remove Element

这道题跟26题很类似,并且有官方的答案。看了官方的答案之后发现写得特别巧,自己做的题太少思路太窄。有意思的是我的算法的时间复杂度是O(N^2),官方的是O(N),我的实际运行时间还少了2ms。

iven 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 matter what you leave beyond the new length.

Example:
Given input array nums = [3,2,2,3]val = 3

Your function should return length = 2, with the first two elements of nums being 2.

给定一个数组和一个值,删除该值的所有实例,并返回新的长度。

不要为另一个数组分配额外的空间,您必须使用常量内存来执行此操作。

元素的顺序可以改变。 无论你离开新的长度什么都不重要。

例:
给定输入数组nums = [3,2,2,3],val = 3

你的函数应该返回length = 2,num的前两个元素为2。



我的实现算法:

 1 class Solution {
 2     public int removeElement(int[] nums, int val) {
 3     int index=1,count1=0,count2=0;
 4         for(int i=0;i<nums.length;i++){
 5             if(nums[i]==val){
 6                 for(int j=i;j<nums.length;j++){
 7                     int temp=0;
 8                     if(nums[j]!=val){
 9                         temp=nums[j];
10                         nums[j]=nums[i];
11                         nums[i]=temp;
12                         count2++;
13                         break;
14                     }
15                 }
16             }
17             else
18                 count1++;
19         }
20         return count1+count2;
21 }
22 }

官方的实现算法:

 1 class Solution {
 2     public int removeElement(int[] nums, int val) {
 3     int i = 0;
 4     for (int j = 0; j < nums.length; j++) {
 5         if (nums[j] != val) {
 6             nums[i] = nums[j];
 7             i++;
 8         }
 9     }
10     return i;
11 }
12 }
时间: 2024-10-10 21:49:09

LeetCode记录之27——Remove Element的相关文章

LeetCode 27.Remove Element 数组元素删除

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.

LeetCode记录之26——Remove Duplicates from Sorted Array

国外的表达思维跟咱们有很大差别,做这道题的时候很明显.简单说本题就是让你把有序数组中的重复项给换成正常有序的.比如 1 2 2 3换成 1 2 3 3,根本不需要考虑重复的怎么办,怎么删除重复项等等.拿起键盘干就行了.然后返回有序项的下标就可以. Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not

27. Remove Element【easy】

27. Remove Element[easy] 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 ch

No.27 Remove Element

No.27 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. Tags: Array Two Pointers 移除数组中所有的给定数

27. Remove Element(js)

27. Remove Element Given an array nums and a value val, 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 by modifying the input array in-place with O(1) extra memor

[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 27.Remove Element (删除元素) 解题思路和方法

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. 思路:此题和26题一脉相承,算法上不难,具体如代码所示: public class

27. Remove Element【leetcode】

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 matter

leetcode 27 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. 题解:水题.遇到val就和最后一个不是val的数交换位置就好了. class Solution { public: