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 ...
随机推荐
- android 设计工具栏
设计工具栏Action Bar(订制工具栏类型) 工具栏给用户提供了一种熟悉和可预测的方式来执行某种动作和操纵应用程序,但是这并不意味着它就需要和其他的应用程序看起来一样的.如果想设计工具栏以使得它能 ...
- jvm 参数
参数名称 含义 默认值 -Xms 初始堆大小 物理内存的1/64(<1GB) 默认(MinHeapFreeRatio参数可以调整)空余堆内存小于40%时,JVM就会增大堆直到-Xmx的最大限 ...
- Java中instanceof用法
java 中的instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例.instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例. 用法:resu ...
- 超简单的处理JSON格式和JSON数组格式的String
现在网站上有不少处理JSON格式的工具类,但是我找了一天,发现大都是需要编写相应对象类来进行处理,比较麻烦,比如:Gson,json-lib.Gson,json-lib这些处理那些接口之类的参数名字和 ...
- python 输出乱码
在Python中有两种默认的字符串:str和unicode.在Python中一定要注意区分“Unicode字符串”和“unicode对象”的区别.后面所有的“unicode字符串”指的都是python ...
- CSS控制"标题前增加小图标或编号"
---题目前加图片--- p:before { content:url(xxx/xx.png); }//所有p的最前都有一个图标 p.a:after { content:url(xxx/xx.png) ...
- 变态的HelloWorld
public static void main(String[] args) { int i, n[] = { (((1 << 1) << (1 << 1) < ...
- SQLSERVER 16进制转10进制
原码.补码.反码参考: http://www.cnblogs.com/zhangziqiu/archive/2011/03/30/ComputerCode.html 进制转换参考: http://ww ...
- Gym 101102A Coins -- 2016 ACM Amman Collegiate Programming Contest(01背包变形)
A - Coins Time Limit:3000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Descript ...
- CodeMIrror 简单使用
代码高亮是程序员的刚需,不管是在笔记类,论坛类,博客类web网站中,都对代码高亮提出要求,不高亮的代码阅读体验很差,codeMirror是一个前端代码高亮库,使用方便. codeMirror可以直接在 ...