星空网 > 软件开发 > Java

[LeetCode] Remove Element

 

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.

       

     这道题也很简单,个人认为,特别是因为只是要求remove,对order的顺序也没有改变(当然了如果愿意还是尽量可以不改变顺序)。可以用个简单的two pointer来解决。一个int用来loop,另一个int则是用来排序剩下的integer。

public class Solution {  public int removeElement(int[] nums, int val) {    int a = 0;    int b = 0;    if (nums.length==0){      return 0;    }     while( b<nums.length ) {      if ( nums[b]!=val ) {        nums[a] = nums[b];        a++;      }          b++;    }    return a;  }}

 




原标题:[LeetCode] Remove Element

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

加拿大商标申请流程详解:https://www.kjdsnews.com/a/1388028.html
包装商标注册指南从申请到注册的全流程:https://www.kjdsnews.com/a/1388029.html
加拿大注册商标需要多少费用?:https://www.kjdsnews.com/a/1388030.html
加拿大注册商标费用攻略:https://www.kjdsnews.com/a/1388031.html
加拿大注册商标的费用收费标准汇总:https://www.kjdsnews.com/a/1388032.html
同一主题下的中国商标和美国商标有何不同?:https://www.kjdsnews.com/a/1388948.html
携程旅游网折扣优惠最新攻略(携程旅游网更佳折扣优惠):https://www.vstour.cn/a/411227.html
深圳到西安自驾路线攻略 深圳到西安自驾最佳路线:https://www.vstour.cn/a/411228.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流