LeetCode:27. Remove Element(Easy)
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)的更多相关文章
- 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 ...
- 【leetcode】Remove Element (easy)
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- LeetCode:20. Valid Parentheses(Easy)
1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')', ...
- LeetCode:7. Reverse Integer(Easy)
题目要求:将给出的整数进行逆序输出 注意:整数的最大范围-2147483648-2147483647,当翻转后的数超出范围后返回0 思路:对给出的整数除以10,取余和取整:然后对取整部分继续取余和取整 ...
- 27. Remove Element【easy】
27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...
- leetcode 1.回文数-(easy)
2019.7.11leetcode刷题 难度 easy 题目名称 回文数 题目摘要 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 思路 一些一定不为回文数的 ...
- [Leetcode][Python]27: Remove Element
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...
- 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 ...
- LeetCode:39. Combination Sum(Medium)
1. 原题链接 https://leetcode.com/problems/combination-sum/description/ 2. 题目要求 给定一个整型数组candidates[ ]和目标值 ...
随机推荐
- 如何使用代码获得一个function module的Where Used List
如果要获得一个function module的Where Used List,我们通常用的办法是使用ABAP workbench里提供的功能. where used list显示结果如下: 但我接到一 ...
- IOS 照片浏览器总结(思想步骤)
1. 界面分析========================================1> 需要读取或修改属性的控件需要设置属性// 序号标签// 图片// 图片描述// 左边按钮// ...
- { ($0, Resolver($0.box)) }(Promise<T>(.pending)):闭包的定义与执行合一
public class func pending() -> (promise: Promise<T>, resolver: Resolver<T>) { return ...
- 【[ZJOI2008]骑士】
这道题好暴力啊 发现自己刚学\(OI\)的时候对着这道题写了一个大搜索 发现已经看不懂了 果然我现在菜到连一年半前的我都不如了 这其实是一个基环树\(dp\)啦,基环树上的最大点独立集 其实很简单,我 ...
- linux服务基础之CentOS6编译安装mariadb
1. 下载mariadb https://downloads.mariadb.org/mariadb/+releases/ 2. 解压到指定目录 # tar xf mariadb--linux-x86 ...
- 【转】Android手机分辨率基础知识(DPI,DIP计算)
1.术语和概念 术语 说明 备注 Screen size(屏幕尺寸) 指的是手机实际的物理尺寸,比如常用的2.8英寸,3.2英寸,3.5英寸,3.7英寸 摩托罗拉milestone手机是3.7英寸 A ...
- 架构师必须知道的26项PHP安全实践
PHP是一种开源服务器端脚本语言,应用很广泛.Apache web服务器提供了这种便利:通过HTTP或HTTPS协议,访问文件和内容.配置不当的服务器端脚本语言会带来各种各样的问题.所以,使用php时 ...
- Oracle连接问题
ORA-01034: ORACLE not availableORA-27101: shared memory realm does not exist sqlplus /nolog conn /as ...
- 原生ajax接收json字符串(简单介绍)
什么是json? JSON的全称是 Javascript Object Notation(javascript对象表示法),是基于javascript对象字面量,如果单从眼睛看,JSON里的数据是被保 ...
- java类的初始化程序块以及被实例化时候的执行顺序
初始化块:在类实例化过程中初始化执行顺序是先执行静态初始化块,然后执行普通初始化块,最后执行构造函数,而且静态初始化只在第一次被实例化时执行且只执行一次.public class HelloWorld ...