26.Remove Duplicates from Sorted Array
来源:互联网 发布:淘宝机械人 编辑:程序博客网 时间:2024/06/05 12:40
/*Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.For example,Given input array nums = [1,1,2],Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length. *///解法一 7% 172msint removeDuplicates(int* nums, int numsSize) { int i,j; for(i=0;i<numsSize-1;i++) if(nums[i]==nums[i+1]) { for(j=i;j<numsSize-1;j++) { nums[j] = nums[j+1]; } numsSize--; i--; } return numsSize;}//解法二 23% 19ms/*这里我参考了27. Remove Element 题目第二种做法对算法进行改进,机理是相同的*/int removeDuplicates(int* nums, int numsSize) { int i,count = 1; if(nums[0]==NULL&&numsSize==0) return NULL; for(i=0;i<numsSize-1;i++) if(nums[i]!=nums[i+1]) nums[count++] = nums[i+1]; return count;}//解法三 69% 12msint removeDuplicates(int* nums, int numsSize) { int i, j; for (i = 1, j = 0; i < numsSize; i++) if (nums[i] != nums[i-1]) nums[++j] = nums[i]; return (numsSize > 0 ? j+1 : j);}
阅读全文
0 0
- [LeetCode]26.Remove Duplicates from Sorted Array
- 26. Remove Duplicates from Sorted Array
- LeetCode 26. Remove Duplicates from Sorted Array
- LeetCode --- 26. Remove Duplicates from Sorted Array
- LeetCode 26.Remove Duplicates from Sorted Array
- [Leetcode] 26. Remove Duplicates from Sorted Array
- 26. Remove Duplicates from Sorted Array
- 26. Remove Duplicates from Sorted Array
- 【leetcode】26. Remove Duplicates from Sorted Array
- 【leetcode】 26. Remove Duplicates from Sorted Array
- 26. Remove Duplicates from Sorted Array
- [leetCode]26. Remove Duplicates from Sorted Array
- LeetCode 26.Remove Duplicates from Sorted Array
- 26. Remove Duplicates from Sorted Array
- 26. Remove Duplicates from Sorted Array LeetCode
- 26. Remove Duplicates from Sorted Array
- 26. Remove Duplicates from Sorted Array
- 26. Remove Duplicates from Sorted Array
- effective C++条款四十解读
- MobileNet训练分类网
- 268. Missing Number
- Galera Cluster for MySQL启动不了,一直不停的自动重启!
- LinkedHashSet实现Hashcode和equals方法
- 26.Remove Duplicates from Sorted Array
- CentOS下Hive2.3.0单机模式安装详解
- PAT 甲级 1003. Emergency
- CDH安装
- 啊哈,灵机一动-----记网易内推编程题堆棋子
- Django编写RESTful API(二):请求和响应
- 27.Remove Element
- Win10 配置tensorflow1.3 GPU版本以及tensorboard的使用
- 修改eclipse工程jdk版本