欢迎光临
我们一直在努力

【算法基础】力扣 27. 移除元素

在这里插入图片描述

package 移除元素;

public class Solution { //双指针( 快慢指针)
public int removeElements(int[] nums,int val){
int slowIndex = 0;
for(int fastIndex = 0;fastIndex<nums.length;fastIndex++){
if(nums[fastIndex]!=val){
nums[slowIndex]=nums[fastIndex];
slowIndex++;
}
}
return slowIndex;
}
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.removeElements(new int[]{1,2,3,4,5,6,7,8,9},3));
}
}

赞(0)
未经允许不得转载:171主机测评 » 【算法基础】力扣 27. 移除元素
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址