Remove Element
来源:互联网 发布:单晶片编程 编辑:程序博客网 时间:2024/05/30 22:58
Remove Element
Total Accepted: 2651 Total Submissions: 7854My SubmissionsGiven 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下标记录当前新数组的长度,遍历原数组,遇到与目标数不同的数就把它赋值到新数组中,start往后移动。
class Solution {public: int removeElement(int A[], int n, int elem) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. int start = 0; for(int i=0; i<n; i++){ if(A[i] != elem){ A[start++] = A[i]; } } return start; }};
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- WindowsMobile项目中ListView的显示表头的基本操作
- js访问后台cs中的代码
- 关于共用体存储的问题探讨
- ava学习之路,数年经验之谈
- Bug管理器Jira如何把bug列表导出为excel
- Remove Element
- Nignx学习之前期调研
- CCTbleview
- VC++中使用ADO方式操作ACCESS数据库
- js调用android本地代码失败 兼容问题
- iOS 7 教程:定制iOS 7中的导航栏和状态栏
- 在没有stonith设备下使用heartbeat实现高可用的原理和测试
- android中sqlite distinct中使用多个字段的方法
- TFIDF算法