LeetCode之Remove Element
1、题目
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 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.
2、代码实现
public class Solution {public int removeElement(int[] nums, int val) {if (nums == null || nums.length == 0)return 0;int count = 0;for (int i = 0; i < nums.length; ++i) {if (nums[i] != val) {nums[count++] = nums[i];}}return count;}
}
class Solution {
public:int removeElement(vector<int>& nums, int val) {int count = 0;for (int i = 0; i < nums.size(); ++i) {if (nums[i] != val) {nums[count++] = nums[i];}}return count;}
};
3、总结
LeetCode之Remove Element相关推荐
- leetcode 27. Remove Element
问题描述: Given an array and a value, remove all instances of that value in-place and return the new len ...
- LeetCode算法入门- Remove Element -day20
LeetCode算法入门- Remove Element -day20 1. 题目描述 Given an array nums and a value val, remove all instance ...
- [LeetCode] Remove Element 分析
Remove Element算是LeetCode的一道水题,不过这题也有多种做法,现就我所知的几种做一点讨论. 题目链接:https://leetcode.com/problems/remove-el ...
- [leetcode]83.Remove Duplicates from Sorted List
题目 Given a sorted linked list, delete all duplicates such that each element appear only once. Exampl ...
- [勇者闯LeetCode] 83. Remove Duplicates from Sorted List
[勇者闯LeetCode] 83. Remove Duplicates from Sorted List Description Given a sorted linked list, delete ...
- LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] c++
LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...
- LeetCode——Kth Largest Element in an Array
LeetCode--Kth Largest Element in an Array Question Find the kth largest element in an unsorted array ...
- [LeetCode] 169. Majority Element 多数元素
Given an array of size n, find the majority element. The majority element is the element that appear ...
- LeetCode:Remove Nth Node From End of List 移除链表倒第n项
2019独角兽企业重金招聘Python工程师标准>>> 1.题目名称 Remove Nth Node From End of List(移除链表中倒数第n项) 2.题目地址 http ...
最新文章
- C++实现判断两个单词是否为变位词
- CTF-IDA的常用操作(初学者)
- c语言ascii码表数字,求教!我想显示数字但是现在显示的却是数字在ASCII码中对应的符...
- [深度学习] FM FFM 算法基本原理
- 知识图谱需要解决的问题
- mysql and 和where,关于mysql:连接sql查询中where和and子句的区别
- PATB1017 A除以B
- vim插件管理利器:pathogen
- 猿如意中的【取色器】效率工具详情介绍
- 债务人无力偿还,债权人可否直接起诉“次债务人”
- 【02】一个实现h5的拖放的整个过程-魔芋
- VMX(1) -- 简介
- app软件小程序开发
- 单精度浮点型(float)和双精度浮点型(double)的区别
- 积微——荀子《强国篇》,给每个职场人士推荐
- Java学习笔记之基础语法(一)
- word2vec and glove
- PDF格式的文档如何编辑修改
- 2021-10-17idea无法导入依赖
- PPT的三种抠图方法-演示