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.

分析

这是一道很简单的题目,对数组进行一次遍历,删除与给定值相等的关键字,返回新长度即可。

AC代码

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        int i = 0 , k = 0;
        while (i < nums.size())
        {
            if (nums[i] != val)
                nums[k++] = nums[i];
            i++;
        }
        return k;
    }
};

GitHub测试程序源码

时间: 2024-08-24 20:00:43

LeetCode(27)Remove Element的相关文章

LeetCode(2) Remove Duplicates from Sorted Array

Remove Duplicates from Sorted Array 题目 考察数组 描述 Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with const

LeetCode(26) Remove Duplicates from Sorted Array

题目 Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example, Given input array n

LeetCode(169)Majority Element

题目 Given an array of size n, find the majority element. The majority element is the element that appears more than ? n/2 ? times. You may assume that the array is non-empty and the majority element always exist in the array. Credits: Special thanks t

LeetCode(19) Remove Nth Node From End of List

题目 Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: Giv

LeetCode(80)Remove Duplicates from Sorted List

题目 Given a sorted linked list, delete all duplicates such that each element appear only once. For example, Given 1->1->2, return 1->2. Given 1->1->2->3->3, return 1->2->3. 分析 删除链表中重复元素结点. 该题目本质很简单,只需一次遍历.需要注意的是,要释放删除的结点空间. AC代码

leetcode第27题-Remove Element

#include<stdio.h> #include<stdlib.h> int removeElement(int A[], int n, int elem) { if(n==0) return 0; int len=n; for(int i=0,pos=0;i<n;i++) { if(A[i]==elem) len--; else A[pos++]=A[i]; } return len; } int main() { int n; while(scanf("%d

Leetcode(5)最长回文子串

Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 一开始我的思路如下:回文子串的特点是首尾字母相同,所以我对每一个字母都找到位于它后面的相同字母,利用切片判断这一段是否为回文子串(str[i:j]==str[i:j][::-1]).时间复杂度很高,主要是因为str.find操作非常耗时. class Solution(object): def lo

java每日小算法(27)

/* [程序27]  题目:求100之内的素数    */ package test; import java.util.Scanner; public class test { public static boolean prime(int number) { boolean flag = true; int mid = (int)Math.sqrt(number); for(int i = 2; i< mid+1; i++) { if(number % i == 0) { flag = fa

HTML5移动开发之路(27)—— JavaScript回顾2

本文为 兄弟连IT教育 机构官方 HTML5培训 教程,主要介绍:HTML5移动开发之路(27)-- JavaScript回顾2 JavaScript面向对象基础知识 1.如何定义一个类,使用如下语法来创建一个类 [javascript] view plain copy print? function Person(name, age){ //习惯上第一个字母大写 //this修饰的变量称为属性 this.name = name; this.age = age; //如果属性值是一个函数,则这个