【leetcode】27.RemoveElement

题目描述

给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度。

https://leetcode-cn.com/problems/remove-element/

解法1

时间复杂度:O(n)

空间复杂度:O(1)

思路:覆盖法,遍历数组,将非指定值放置在数组前方

int removeElement(vector<int>& nums, int val)
{
    int j = 0;
    for (int i = 0; i < nums.size(); i ++)
    {
        if (nums[i] != val)
        {
            nums[j++] = nums[i];
        }
    }
    return j;
 }

解法2

时间复杂度:O(n)

空间复杂度:O(1)

思路:第一种方法的改进,当i与j相等时,不需要赋值,避免多余的赋值操作。

int removeElement(vector<int>& nums, int val)
{
    int j = 0;
    for (int i = 0; i < nums.size(); i ++)
    {
        if (nums[i] != val)
        {
            if (i != j)
                nums[j] = nums[i];
            j ++;
        }
    }
    return j;
}

原文地址:https://www.cnblogs.com/JesseTsou/p/10322282.html

时间: 2024-11-01 10:54:26

【leetcode】27.RemoveElement的相关文章

【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. Solution 1: 设置一个新的下标length,遍历过程中遇到val就跳过,否则就赋给新数组下标对应元素.缺

【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

【LeetCode】27.移除元素

class Solution { public: int removeElement(vector<int>& nums, int val) { if(nums.empty()) return 0; int l=0; int r=nums.size()-1; while(l<r){ if(nums[l]==val) { while(l<r&&nums[r]==val) { r--; if(l==r) break; } swap(nums[l],nums[r]

【LeetCode】27.Linked List—Reverse Linked List l链表逆置

Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both? 思路: 重塑链表,将首结点逐个后移,移动过程中遇到的结点都

【leetcode】Word Break II

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens = "catsanddog",dict = ["cat", "cats"

【题解】【数组】【Leetcode】Median of Two Sorted Arrays

Median of Two Sorted Arrays There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 递归和非递归,此提比较简单.广度优先遍历即可.关键之处就在于如何保持访问深度. 下面是4种代码: 1

【leetcode】编辑距离

dp方程“ 1.初始化:dp[0][i]=i; dp[j][0]=j; 2.dp[i][j]=         dp[i-1][j-1](相等) dp[i-1][j]+1 ,,dp[i][j-1]+1; dp[i-1][j-1] (这个对应是改的况) 注意字符串下标开始位置就OK了 1 public class Solution { 2 public int minDistance(String word1, String word2) { 3 char c1[]=word1.toCharArr

【leetcode】Clone Graph

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label and each neigh