[Leetcode 27, easy] Remove Element
2013-11-12 02:44
507 查看
Problem:
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.
Analysis:
This is a typical way to manipulating two pointers of an array.
Solutions:
C++:
Java:
Python:
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.
Analysis:
This is a typical way to manipulating two pointers of an array.
Solutions:
C++:
int removeElement(int A[], int n, int elem) { if(n==0) return n; if(n==1){ if(A[0]==elem) return 0; else return 1; } int end=n-1; for(int start=n-1; start>=0; --start){ if(A[start]==elem){ if(start==end){ --end; }else{ int temp=A[end]; A[end]=A[start]; A[start]=temp; --end; } } } return end+1; }
Java:
Python:
相关文章推荐
- 字符串练习
- notepad++运行paython程序
- hosts相关配置备份
- 面向对象方法中的数据库设计
- [Leetcode 19, Easy] Remove Nth Node From End of List
- [Backup] How to build and use Pansenti / meta-pansenti for Gumstix Overo Ironstorm
- 【MZ】CF 363E #211 (Div. 2)
- 人不聪明怎么办?【知乎】
- 网站网页设计的几项基本原则
- main
- linux-epoll研究
- seoer网站降权的解决办法
- 猴子吃桃 用递归和循环两种办法解决
- Linux内核-内存管理-PAE(物理地址扩展)
- PHP扩展库安装
- 如何设置PPT,演示者能看到备注而观众看不到
- 设置Windows为ntp服务器
- 5分钟搞定内存字节对齐
- hello world
- HDU 1595