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 ...
随机推荐
- 【转】 Build a RESTful Web service using Jersey and Apache Tomcat 2009
Build a RESTful Web service using Jersey and Apache Tomcat Yi Ming Huang with Dong Fei Wu, Qing GuoP ...
- 初次接触Groovy
其实我这两年业余时间一直断断续续的在学java,水平还不怎么样,不过起码能参与小项目写写接口了.现在我决定暂时放下大众的java,改学小众的Groovy,只因为我新拿到个offer,对方公司的自动化框 ...
- Redis学习笔记(6)-SortedSet
package cn.com; import java.util.HashMap; import java.util.Map; import java.util.Set; import redis.c ...
- QRadioButton
#include "dialog.h" #include "ui_dialog.h" #include <QtCore> #include < ...
- AX中四种库存ABC分析法原理研究
库存ABC分类,简单的说就是抓大放小,是为了让我们抓住重点,用最大精力来管理最重要的物料,而对于不太重要的物料则可以用较少的精力进行管理.它和我们平常说的八二法则有异曲同工之妙. 既然要应用库存ABC ...
- Java学习-026-类名或方法名应用之二 -- 统计分析基础
前文讲述了类名或方法的应用之一调试源码,具体请参阅:Java学习-025-类名或方法名应用之一 -- 调试源码 此文主要讲述类名或方法应用之二统计分析,通过在各个方法中插桩(调用桩方法),获取方法的调 ...
- 让DIV中的内容水平和垂直居中
让一个层水平垂直居中是一个非常常见的布局方式,但在html中水平居中使用margin:0px auto;可以实现,但垂直居中使用外边距是无法达到效果的.(页面设置height:100%;是无效的),这 ...
- 通往成功的钥匙--Web前端开发技术
互联网是一个服务性行业,用户对网站良好的体验度,直接影响到网站的效果.无论你做了多少广告推广,没有用户体验度等于零.Web前端技术是为了解决用户体验度而诞生的.无论是百度.新浪.阿里巴巴等大型网站,还 ...
- POJ 1035问题解答
#include <iostream>#include <cstdio>#include <cmath> #include <string>#inclu ...
- saltstack之(十一)扩展组件salt-returners
场景:每次执行salt任务后,将返回结果存入到数据库,可以做任务跟踪以及历史查看. 1.在node1上安装mysql数据库并启动设置root密码.[root@node1 ~]# yum -y inst ...