[LeetCode27]Remove Element
2015-12-20 20:04
351 查看
题目:
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.
思路:设置两个变量start和end同时从前后向中间遍历,遇到等于val的元素则交换数组end和start位置的值,直到end==start
代码:
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.
思路:设置两个变量start和end同时从前后向中间遍历,遇到等于val的元素则交换数组end和start位置的值,直到end==start
代码:
public class Solution { public int RemoveElement(int[] nums, int val) { int start = 0; int end = nums.Length; if(end == 0) return 0; while(start < end) { if(nums[start] == val) { int temp = nums[start]; nums[start] = nums[end-1]; nums[end-1] = temp; end--; } else { start++; } } return end; } }
相关文章推荐
- java nio 缓冲区(一)
- Android事件分发机制研究
- 信息安全系统设计基础期末总结
- 20135213 20135231 信息安全系统设计基础课程第四次实验报告
- shader error: unable to unroll loop, loop does not appear to terminate in a timely manner的解决办法
- IOS开发--C语言入门--如何结合Terminal和Vim开发C语言程序
- CENTOS 服务器安装WordPress流程
- SSL/TLS编程
- 20135213 20135231 信息安全系统设计基础课程第三次实验报告
- HDU 1573 X问题 数论
- 优化系统架构性能
- HTML 5 File API:文件拖放上传功能
- 最近想打算开发一个用于记录个人学习的博客,记录下进程。
- 21. Merge Two Sorted Lists
- Android进阶——声波振幅显示
- HTML5拖放API2
- iOS电话、短信、网页的调用
- live555编译、播放示例
- 从国内镜像网站 下载Android源码
- JavaScript设计模式--状态模式