经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » 编程经验 » 查看文章
LeetCode 27. Remove Element
来源:cnblogs  作者:flowingfog  时间:2018/10/15 9:05:42  对本文有异议

分析

难度 易

来源

https://leetcode.com/problems/remove-element/description/

题目

Given an array nums and a value val, 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 by modifying the input array in-place with O(1) extra memory.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

Example 1:

  1. Given nums = [3,2,2,3], val = 3,
  1.  
  1. Your function should return length = 2, with the first two elements of nums being 2.
  1.  
  1. It doesn't matter what you leave beyond the returned length.

Example 2:

  1. Given nums = [0,1,2,2,3,0,4,2], val = 2,
  1.  
  1. Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.
  1.  
  1. Note that the order of those five elements can be arbitrary.
  1.  
  1. It doesn't matter what values are set beyond the returned length.

Clarification:

Confused why the returned value is an integer but your answer is an array?

Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.

Internally you can think of this:

  1. // nums is passed in by reference. (i.e., without making a copy)
  1. int len = removeElement(nums, val);
  1.  
  1. // any modification to nums in your function would be known by the caller.
  1. // using the length returned by your function, it prints the first len elements.
  1. for (int i = 0; i < len; i++) {
  1.     print(nums[i]);
  1. }

 

解答

  1. package LeetCode;
  2.  
  3. public class L27_RemoveElement {
  4. public int removeElement(int[] nums, int val) {
  5. /*
  6. 返回移除val后的数组长度
  7. 同时需要对nums进行修改,使得nums前len个数不包含val值
  8. 前len个数的顺序不做要求
  9. 考虑类似快排的交换方式
  10. */
  11. int len=nums.length;
  12. int start=0,end=len-1;
  13. while(end>=start){
  14. //从后往前比较
  15. while(end>=start&&nums[end]==val) //如果当前值等于val,比较下一个,直到有不等于val的交换位置,然后又从前往后比较
  16. {
  17. end--;
  18. len--;
  19. }
  20. //从前往后比较
  21. while(end>=start&&nums[start]!=val)//如果没有等于val的,比较下一个,直到有比等于val的交换位置
  22. start++;
  23. if(end>=start){
  24. int temp = nums[start];
  25. nums[start] = nums[end];
  26. nums[end] = temp;
  27. }
  28. //一次循环交换一对数值的位置
  29. }
  30. // 比较结束,nums即基本有序。左边的值都不等于val,右边的值等于val
  31. return len;
  32. }
  33. public static void main(String[] args){
  34. int[] nums={1};
  35. int val=1;
  36. L27_RemoveElement re=new L27_RemoveElement();
  37. int len = re.removeElement(nums, val);
  38. // any modification to nums in your function would be known by the caller.
  39. // using the length returned by your function, it prints the first len elements.
  40. for (int i = 0; i < len; i++) {
  41. System.out.println(nums[i]);
  42. }
  43. }
  44. }

 

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号