Remove Duplicates from Sorted Array
来源:互联网 发布:淘宝购物津贴有什么用 编辑:程序博客网 时间:2024/06/03 17:39
Remove Duplicates from Sorted Array
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 A = [1,1,2],
Your function should return length = 2, and A is now [1,2].
#include<stdio.h>int removeDuplicates(int A[],int length){ if(length == 0){ return 0; } int count = 1; for(int i = 1; i < length; i++){ if(A[i] == A[i - 1]){ continue; }else{ A[count] = A[i]; count++; } } for(int i = count; i < length; i++){ A[i] = 0x0; } printf("A[] = "); for(int i = 0; i < count; i++){ printf("%d ",A[i]); } printf("\n"); return count; }int main(){ int A[] = {1,1,2}; int length = sizeof(A)/sizeof(A[0]); int count; count = removeDuplicates(A,length); printf("count = "); printf("%d\n",count); return 0;}
运行结果如图:
阅读全文
0 0
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates From Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- 用Linux shell脚本爬取豆瓣邮箱
- Edittext添加自定义过滤器
- greendao操作数据库升级导致表数据丢失
- java菜鸟---------用java写的汉诺塔问题程序
- AndroidStudio 查看Framework代码
- Remove Duplicates from Sorted Array
- 关于spring与springmvc的依赖注入的思考.
- 正则表达式(一) 匹配原理/贪婪与非贪婪
- 在使用Androidstudio打包签名apk文件时,Signature Versions V1(Jar Signature) V2(Full APK Signature)选项
- bootstrap-table简单应用
- jQuery自定义元素右键点击事件
- iOS常用设计模式
- Redis持久化操作
- C# Queue模板类