Problem : 移除数组中给定target的元素,返回剩余数组中元素个数
 
首先对数组进行排序,之后对数组进行遍历操作
当遇到不等于val的值是对下标i进行++操作。
 
参考代码: 
package leetcode_50;

import java.util.Arrays;

/***
*
* @author pengfei_zheng
* 移除数组中重复元素,返回剩余元素个数
*/
public class Solution27 {
public static int removeElement(int[] nums, int val) {
Arrays.sort(nums);
int i=0;
for(int n:nums){
if(n!=val){
nums[i++]=n;
}
}
return i;
} public static void main(String[]args){
int []nums = {4,5,3,6,3,7};
System.out.println(removeElement(nums,3));
}
}

LeetCode 27 Remove Element (移除数组中指定元素)的更多相关文章

  1. Leetcode27--->Remove Element(移除数组中给定元素)

    题目:给定一个数组array和一个值value,移除掉数组中所有与value值相等的元素,返回新的数组的长度:要求:不能分配额外的数组空间,且必须使用原地排序的思想,空间复杂度O(1); 举例: Gi ...

  2. [LeetCode] 27. Remove Element 移除元素

    Given an array nums and a value val, remove all instances of that value in-place and return the new ...

  3. [LeetCode]27. Remove Element移除元素

    Given an array nums and a value val, remove all instances of that value in-place and return the new ...

  4. LeetCode数组移除数组中目标元素等题目

    一种自己解题,一种高赞解题 /** * 移除数组中目标元素,返回新数组长度 * @param nums * @param val * @return */ public int removeEleme ...

  5. 【LeetCode每天一题】Find First and Last Position of Element in Sorted Array(找到排序数组中指定元素的开始和结束下标)

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  6. js能力测评——移除数组中的元素

    移除数组中的元素 题目描述 : 移除数组 arr 中的所有值与 item 相等的元素.不要直接修改数组 arr,结果返回新的数组 示例1 输入 [1, 2, 3, 4, 2], 2 输出 [1, 3, ...

  7. 移除数组中指定键(Yii2)

    /** * 移除数组中指定key * @param $data * @param $key * @return array */ public static function removeKey($d ...

  8. Leetcode算法【34在排序数组中查找元素】

    在之前ARTS打卡中,我每次都把算法.英文文档.技巧都写在一个文章里,这样对我的帮助是挺大的,但是可能给读者来说,一下子有这么多的输入,还是需要长时间的消化. 那我现在改变下方式,将每一个模块细分化, ...

  9. 27. Remove Element - 移除元素-Easy

    Description: Given an array and a value, remove all instances of that value in place and return the ...

随机推荐

  1. C# 把字符串中间的多个连续的空格转化成一个空格

    今天在弄帮客户将txt文件中的信息导入到数据库中,遇到了这个问题.因为客户的txt文件中两个字符串之间的空格数量不确定,没有办法使用split函数来分割,最后想到的办法是,将连续的空格转成一个空格,然 ...

  2. Unity3d 动态加载材质方法

    Texture img = (Texture)Resources.Load("LedPicture"); GameObject.Find("Led").rend ...

  3. fedora26在编译s3c2440内核时make menuconfig *** Unable to find the ncurses libraries

    [root@fedora-26 linux-2.6.32.2]# make menuconfig *** Unable to find the ncurses libraries or the *** ...

  4. 关于Struts2的多文件上传

    之前写过一篇文章,关于Struts2文件上传:http://www.cnblogs.com/lichenwei/p/3927964.html 现在来说下多文件上传,其实就把上传文件当成是一个数组去处理 ...

  5. POI简易帮助文档系列--读取Excel文件

    上篇博客通过简单的几行代码就学会了POI新建Excel文档的使用,本篇博客也从简单出发,通过查看POI的官网文档和一个简单的代码实例,学习怎么遍历出一个Excel文档的内容. package com. ...

  6. Lua之转义字符

    print("\a");    --bell 硬件滴一声 print("a"); print("\b");    --back space  ...

  7. phpcms列表页调用 点击量

    很多朋友经常问Phpcms v9的首页.列表页.内容页点击量如何调用.现在就给大家分享phpcms V9如何分别在首页.列表页.内容页调用点击量代码: 1. Phpcms v9首页调用点击量{pc:c ...

  8. teamviwer安装提示 Verification of your Teamviewer version failed!.

    打开cmd输入 regsvr32 c:\windows\SysWOW64\wintrust.dll 就可以了.

  9. Tomcat------如何打开配置界面

    如图操作即可:

  10. Eclipse cdt解决github导入的项目无法打开声明的bug (cannot open declaration)

    概述: 我利用eclipse 的git插件clone github上的远程项目(C++)到本地时遇到一个问题:clone下来的项目没有C++特性,无法使用open declaration等操作,下面是 ...