[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 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.
解法:
这道题让我们移除一个数组中和给定值相同的数字,并返回新的数组的长度。是一道比较容易的题,我们只需要一个变量用来计数,然后遍历原数组,如果当前的值和给定值不同,我们就把当前值覆盖计数变量的位置,并将计数变量加1。代码如下:
public class Solution {
public int removeElement(int[] nums, int val) {
if (nums == null || nums.length == 0) {
return 0;
}
int newLength = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != val) {
nums[newLength++] = nums[i];
}
}
return newLength;
}
}
[LeetCode] 27. Remove Element ☆的更多相关文章
- leetCode 27.Remove Element (删除元素) 解题思路和方法
Remove Element Given an array and a value, remove all instances of that value in place and return th ...
- LeetCode 27. Remove Element (移除元素)
Given an array and a value, remove all instances of that value in place and return the new length. D ...
- [LeetCode] 27. Remove Element 移除元素
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
- LeetCode 27 Remove Element
Problem: Given an array and a value, remove all instances of that value in place and return the new ...
- Leetcode 27 Remove Element STL
和remove zero类似的方法完成该题 class Solution { public: int removeElement(vector<int>& nums, int va ...
- Java [leetcode 27]Remove Element
题目描述: Given an array and a value, remove all instances of that value in place and return the new len ...
- Leetcode 27——Remove Element
Given an array and a value, remove all instances of that value in-place and return the new length. D ...
- (双指针) leetcode 27. Remove Element
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
- 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]27. Remove Element删除元素
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
随机推荐
- Linux java项目冲突不能正常运行
部署项目,在windows环境部署到Tomcat能够正常运行,部署到Linux环境下只能访问一些html资源,不能正常运行. 解决步骤: 1.清除webapps目录下所有文件夹,将war包上传至web ...
- 软件工程第八周PSP
1.本周PSP 2.本周进度条 代码行数 0 博文字数 356 知识点 无 3.时间分配饼状图 4.折线图
- 用VS测试程序
怀着一种忐忑的心情,我开始了我的软件测试. #include "stdio.h" #include "stdlib.h" int main(int argc, ...
- 冲刺ing-3
第三次Scrum冲刺 队员完成的任务 队员 完成任务 吴伟华 分配任务,燃尽图 蔺皓雯 编写博客,美化主界面 蔡晨旸 美化主界面 曾茜 主页面设计 鲁婧楠 服务器建构 杨池宇 服务器建构 成员遇到的问 ...
- "助成"招聘网站视频简介
我们小组为我们的作品录制了一个一分多钟的电梯介绍视频,这是视频连接,我上传到了优酷上:http://v.youku.com/v_show/id_XMzIzMTc1ODc2NA==.html?spm=a ...
- 每日scrum--No.1
Yesterday:无 Today: 查找学校的官方地图和亲自测试其准确性 Problem :不能使地图适应我们的软件;未解决地图上存在的问题: 明天继续加油.
- 使用C和C++实现“电梯”的区别
C 面向过程: 该电梯不允许未卜先知,故程序需逐条处理乘客请求并更新当前各变量状态. 如何获得最短时间:是否立即响应请求,计算出不同决策下的总时间,并进行比较,然后选择最短时间 ...
- 软工实践-Alpha 冲刺 (7/10)
队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 已经解决登录注册等基本功能的界面. 完成非功能的主界面制作 ...
- Floyd模板(详细操作最基础版)
#include<cstdio> #include<iostream> using namespace std; #define MAX 500 #define INFE 1& ...
- texbbox,combobox设置属性
--输入框 $("#xx").textbox('setValue','value'); //设置输入框的值 $('#xx').textbox('textbox').attr('r ...