LeetCode记录之27——Remove Element
这道题跟26题很类似,并且有官方的答案。看了官方的答案之后发现写得特别巧,自己做的题太少思路太窄。有意思的是我的算法的时间复杂度是O(N^2),官方的是O(N),我的实际运行时间还少了2ms。
iven 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.
给定一个数组和一个值,删除该值的所有实例,并返回新的长度。
不要为另一个数组分配额外的空间,您必须使用常量内存来执行此操作。
元素的顺序可以改变。 无论你离开新的长度什么都不重要。
例:
给定输入数组nums = [3,2,2,3],val = 3
你的函数应该返回length = 2,num的前两个元素为2。
我的实现算法:
class Solution {
public int removeElement(int[] nums, int val) {
int index=1,count1=0,count2=0;
for(int i=0;i<nums.length;i++){
if(nums[i]==val){
for(int j=i;j<nums.length;j++){
int temp=0;
if(nums[j]!=val){
temp=nums[j];
nums[j]=nums[i];
nums[i]=temp;
count2++;
break;
}
}
}
else
count1++;
}
return count1+count2;
}
}
官方的实现算法:
class Solution {
public int removeElement(int[] nums, int val) {
int i = 0;
for (int j = 0; j < nums.length; j++) {
if (nums[j] != val) {
nums[i] = nums[j];
i++;
}
}
return i;
}
}
LeetCode记录之27——Remove Element的更多相关文章
- leetCode练题——27. Remove Element
1.题目 27. Remove Element——Easy Given an array nums and a value val, remove all instances of that valu ...
- [LeetCode&Python] Problem 27. Remove Element
Given an array nums and a value val, remove all instances of that value in-placeand return the new l ...
- LeetCode Array Easy 27. Remove Element 解题
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
- 【leetcode❤python】27. Remove Element
#-*- coding: UTF-8 -*- class Solution(object): def removeElement(self, nums, val): "& ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- [Leetcode][Python]27: Remove Element
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...
- 27. Remove Element【easy】
27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...
- Leetcode 题目整理-7 Remove Element & Implement strStr()
27. Remove Element Given an array and a value, remove all instances of that value in place and retur ...
- C# 写 LeetCode easy #27 Remove Element
27. Remove Element Given an array nums and a value val, remove all instances of that value in-place ...
随机推荐
- 【bzoj4296】再见Xor
4269: 再见Xor Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 176 Solved: 107[Submit][Status][Discuss ...
- Rails 和 Django 的深度技术对比
我想以一个免责声明来开始下面的内容.我使用 Django开发网站已经有三年了,众所周知,我喜欢Django.我已经写了一个开源的应用程序( app),并且我已经将补丁发送到了Django.然而,我以尽 ...
- Flask框架 之 学生管理分析
先看模板吧. index.html <body> <h1>学生列表</h1> <table border="1"> <thea ...
- Ajax——三种数据传输格式
一.HTML HTML由一些普通文本组成.如果服务器通过XMLHTTPRequest发送HTML,文本将存储在responseText属性中. 从服务器端发送的HTML的代码在浏览器端不需要用Java ...
- Shiro——MD5加密
一.shiro默认密码的比对 通过 AuthenticatingRealm 的 credentialsMatcher 属性来进行的密码的比对 /**源码org.apache.shiro.realm.A ...
- SVN常见问题及解决方式(二)
1.分支不同 ==> update merge(svn自动合并)2.分支冲突 ==> 协商解决冲突,选择一个正确的版本覆盖(最新的正确直接Revert最新):出现四个文件.黄色感叹号代表S ...
- Monkey进行压力测试定位问题分析
Monkey测试的log分析,我们可以通过几个关键词来判断测试是否通过. 分析log方法一(粗糙一点): 1)Monkey finished打开LOG,查看log的最下端,是否有类似以下字段:## ...
- 特殊的HttpApplication事件处理
在global.asax中,针对HttpApplication的事件处理,可以通过定义特殊命名的方法来实现.首先,这些方法必须符合System.EventHandler,因为所有的HttpApplic ...
- window phone 8资源管理器打开文件
一.打开安装包里面文件: StorageFolder sf = Package.Current.InstalledLocation;//ApplicationData.Current.LocalFol ...
- 生成MySql数据库的数据字典代码参考
Code: /** * 生成mysql数据字典 */ //配置数据库 $dbserver = "127.0.0.1"; $dbusername = "root" ...