Leetcode: Remove Elements
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.
一次性通过的,比较顺利,从读题到编写到检查到通过,14分50秒,我在不断进步中,相信经过一段时间联系,这种题可以一眼就写出来,不超过5分钟。
这道题应该说方法跟 Remove Duplicates from sorted Array挺类似的
My Solution:
public class Solution {
public int removeElement(int[] A, int elem) {
int count = 0;
for(int i=0; i<A.length; i++){
if(A[i] != elem){
A[count] = A[i];
count++;
}
}
return count;
}
}
再贴个另外两个人的solution,以便对照参考,比较优劣:
Solution 1: 这个想法有点曲折
public class Solution {
public int removeElement(int[] A, int elem) {
// Start typing your Java solution below
// DO NOT write main() function
int i=0, j=A.length-1;
while(i<=j){
if(A[i]==elem)
swap(A,i,j--);
else
i++;
}
return j+1;
}
public void swap(int[] A,int i, int j){
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
Solution 2: 跟我的想法一致
public class RemoveElement {
public int removeElement(int[] A, int elem) {
// Start typing your Java solution below
// DO NOT write main() function
if (A.length == 0) {
return 0;
}
int counter = 0;
for (int i = 0; i < A.length; i++) {
if (A[i] != elem) {
A[counter] = A[i];
counter++;
}
}
return counter;
}
}
Leetcode: Remove Elements的更多相关文章
- LeetCode:Remove Duplicates from Sorted List I II
LeetCode:Remove Duplicates from Sorted List Given a sorted linked list, delete all duplicates such t ...
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
- Javacript Remove Elements from Array
參考自: https://love2dev.com/blog/javascript-remove-from-array/ 1. Removing Elements from End of Array ...
- [LeetCode] Remove Linked List Elements 移除链表元素
Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...
- [LeetCode] Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- LeetCode Remove Linked List Elements 删除链表元素
题意:移除链表中元素值为val的全部元素. 思路:算法复杂度肯定是O(n),那么就在追求更少代码和更少额外操作.我做不出来. /** * Definition for singly-linked li ...
- leetcode 283 Move Zeros; 27 Remove Elements; 26 Remove Duplicated from Sorted Array;
,,,,}; //把数组的值赋给vector vector<int> vec(arr, arr+sizeof(arr)/sizeof(int)); 解法一: 时间复杂度O(n) 空间复杂度 ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Element 移除元素
Given an array and a value, remove all instances of that value in place and return the new length. T ...
随机推荐
- 不绑架输入--document.getElementById("linkage_"+id_type+"_echo").value="";--联动
<script> function w_linkage(id_type) { var selected = $("#linkage_"+id_type+"_t ...
- 8添加一些样式:开始学习CSS
CSS中简单的表达式,成为规则.一个典型的规则包括一个选择符.若干属性和属性值. 1.在XHTML中直接添加CSS样式,必须在<head>元素里添加样式开始和结束标记.(但这未必是最好的方 ...
- HTML5 测验记录
结果:11/20 您的回答: 1.HTML5 之前的 HTML 版本是? 您的回答:HTML 4 正确答案:HTML 4.01 2.HTML5 的正确 doctype 是? 您的回答:<!DOC ...
- 单片机与嵌入式 以及ARM DSP FPGA 几个概念的理解
嵌入式设备一般要满足实时性的要求,而实时性是要求数据输入和输出的延时满足一定的要求.当然嵌入式一般都便携性都比PC要好,功能没有PC多,PC是通用,他是专用,一般只专注某些功能的实现,比如DSP专注数 ...
- Freemarker的初次使用之FTL标签嵌套与map的使用
入职第二周了,在熟悉了公司自动化测试脚本的编写(使用什么数据库,使用哪种语言,框架带了哪些方法)后,现在开始熟悉模拟器,我们把请求发到服务器1,服务器1根据请求参数处理后将结果发给模拟器,模拟器根据服 ...
- lifecycle of opensource products--x86-64
x86是指intel的开发的一种32位指令集,从386开始时代开始的,一直沿用至今,是一种cisc指令集,所有intel早期的cpu,amd早期的cpu都支持这种指令集,ntel官方文档里面称为“IA ...
- 用Java操作树莓派!pi4j简介与安装
简介 对C不熟?习惯了使用java不想换语言,但又想操作树莓派?想一边喝咖啡,一边吃树莓派蛋糕?快来使用pi4j吧! pi4j旨在为java开发者提供面友好的面向对象的API,来操控树莓派.pi4j对 ...
- Python 扫面文件夹中的文件
#coding=utf-8 import os,sys reload(sys) sys.setdefaultencoding("utf-8") def scan_files_sub ...
- IOS证书的申请和使用
苹果的证书繁锁复杂,制作管理相当麻烦,今天决定重置一个游戏项目中的所有证书,做了这么多次还是感觉很纠结,索性直接记录下来,日后你我他查阅都方便: 关于证书 苹果使用密文签名技术来验证App的合法性,不 ...
- jQuery 复选框全选反选
<script type="text/javascript"> $(function(){ //全选 $("#CheckedAll").click( ...