1. 原题链接

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

2. 题目要求

给定一个整数数组 nums[ ] 和一个整数 val,删除数组中与val相同的元素,并返回删除后的数组长度

注意:不能定义新的数组,只能使用O(1)空间大小

3. 解题思路

遍历一次,将每个元素与给定的value进行比较,不同则给nums[count++]赋予当前元素的值;相同则直接跳过,最后返回count,即为删除后数组的长度。

4. 代码实现

public class RemoveElement27 {
public static void main(String[] args) {
int[] nums = {2, 34, 5, 67, 89, 5, 4};
System.out.println(removeElement(nums, 5));
} public static int removeElement(int[] nums, int val) {
int count = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != val)
nums[count++] = nums[i];
}
return count;
}
}

  

LeetCode:27. Remove Element(Easy)的更多相关文章

  1. Leetcode No.27 Remove Element(c++实现)

    1. 题目 1.1 英文题目 Given an integer array nums and an integer val, remove all occurrences of val in nums ...

  2. 【leetcode】Remove Element (easy)

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  3. LeetCode:20. Valid Parentheses(Easy)

    1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')',  ...

  4. LeetCode:7. Reverse Integer(Easy)

    题目要求:将给出的整数进行逆序输出 注意:整数的最大范围-2147483648-2147483647,当翻转后的数超出范围后返回0 思路:对给出的整数除以10,取余和取整:然后对取整部分继续取余和取整 ...

  5. 27. Remove Element【easy】

    27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...

  6. leetcode 1.回文数-(easy)

    2019.7.11leetcode刷题 难度 easy 题目名称 回文数 题目摘要 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 思路 一些一定不为回文数的 ...

  7. [Leetcode][Python]27: Remove Element

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...

  8. Leetcode 27. Remove Element(too easy)

    Given an array and a value, remove all instances of that value in-place and return the new length. D ...

  9. LeetCode:39. Combination Sum(Medium)

    1. 原题链接 https://leetcode.com/problems/combination-sum/description/ 2. 题目要求 给定一个整型数组candidates[ ]和目标值 ...

随机推荐

  1. IOS 录音(AVAudioRecorder)

    #import "HMViewController.h" #import <AVFoundation/AVFoundation.h> @interface HMView ...

  2. 添加模糊效果demo

    添加模糊效果demo: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> & ...

  3. Spring Boot 配置文件详解:Properties和YAML

    一.配置文件的生效顺序,会对值进行覆盖: 1. @TestPropertySource 注解 2. 命令行参数 3. Java系统属性(System.getProperties()) 4. 操作系统环 ...

  4. 【转】Spring boot 打成jar包问题总结

    http://www.cnblogs.com/xingzc/p/5972488.html 1.Unable to find a single main class from the following ...

  5. 运lucky

    运 [问题背景] zhx 和妹子们玩数数游戏. [问题描述] 仅包含 4 或7 的数被称为幸运数. 一个序列的子序列被定义为从序列中删去若干个数, 剩下的数组成的新序列. 两个子序列被定义为不同的当且 ...

  6. HDU 1222 Wolf and Rabbit(数学,找规律)

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  7. o'Reill的SVG精髓(第二版)学习笔记——第七章

    第七章:路径 所有描述轮廓的数据都放在<path>元素的d属性中(d是data的缩写).路径数据包括单个字符的命令,比如M表示moveto,L表示lineto.接着是该命令的坐标信息. 7 ...

  8. etcd客户端c#

    etcd是什么东西就不介绍了,自己网上搜索,简单说就是一个分布式K/V存储系统: 由于它是go语言写的,没有其它客户端,找到一个java的. 出于方案积累原因,写了c#版本,可以使用.c#版本是基于e ...

  9. 【例题收藏】◇例题·6◇ 电压机制(voltage)

    ◆例题·6◆ 电压机制 周六日常模拟赛……已经不知道该说什么了(感觉做不出来的都是好题) ▷ 题目 (终于不用自己翻译英文题了╮(╯-╰)╭) [问题描述] 科学家在“无限神机”(Infinity M ...

  10. sql语句中#{}和${}的区别

    #---将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号.如:order by #user_id#,如果传入的值是111,那么解析成sql时的值为order by “111”, 如果传入的 ...