27.Remove Element
来源:互联网 发布:淘宝机械人 编辑:程序博客网 时间:2024/06/03 06:35
/*Given an array and a value, 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 in place with constant memory.The order of elements can be changed. It doesn't matter what you leave beyond the new length.Example:Given input array nums = [3,2,2,3], val = 3Your function should return length = 2, with the first two elements of nums being 2.*///解法一 超越33% 3msint removeElement(int* nums, int numsSize, int val) { int i,j; for(i=0;i<numsSize;i++) if(nums[i] == val) { for(j=i;j<numsSize-1;j++) nums[j] = nums[j+1]; numsSize--;//如果有重复的那么长度减一 i = i - 1;//并且定位到下一个元素,由于提前一位,所以减一重新检查 } return numsSize;}//解法二 超越33% 3msint removeElement(int* nums, int numsSize, int val) { int i,begin = 0; for(i=0;i<numsSize;i++) if(nums[i]!=val) nums[begin++]=nums[i]; return begin;}
阅读全文
0 0
- [LeetCode]27.Remove Element
- LeetCode --- 27. Remove Element
- LeetCode 27.Remove Element
- [Leetcode] 27. Remove Element
- 27.Remove Element
- [Leetcode] 27.Remove Element
- 27. Remove Element
- 27. Remove Element
- 【leetcode】27. Remove Element
- 【leetcode】27. Remove Element
- 27. Remove Element
- 27. Remove Element
- 27. Remove Element
- Leetcode 27. Remove Element
- 27. Remove Element
- LeetCode 27. Remove Element
- 27. Remove Element LeetCode
- 27. Remove Element
- CentOS下Hive2.3.0单机模式安装详解
- PAT 甲级 1003. Emergency
- CDH安装
- 啊哈,灵机一动-----记网易内推编程题堆棋子
- Django编写RESTful API(二):请求和响应
- 27.Remove Element
- Win10 配置tensorflow1.3 GPU版本以及tensorboard的使用
- 修改eclipse工程jdk版本
- Maven之scope含义的说明-yellowcong
- 堆排序
- 关于这些天的小小想法
- 283.Move Zeroes
- PAT 甲级 1005. Spell It Right
- 292. Nim Game