[Leetcode]-Remove Duplicates from Sorted Array
2016-03-05 19:56
253 查看
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.
Hide Tags :Array 。Two Pointers
题目26:输入一个已经拍好序的数组。当中有可能有反复数据。找出反复数据。栓出多余的反复数据。仅仅保留一个反复数据。
思路:记录当中的反复数据次数。比方当中有三个数据都是5,那么反复次数就为2。
将后面的数据依次填充到反复数据位置.
题目27:本题和题26相似。27题要求将与给定数据val反复数据都栓出,不保留不论什么一个反复数据,方法相似,记录反复次数比26题多1个即可
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.
Hide Tags :Array 。Two Pointers
题目26:输入一个已经拍好序的数组。当中有可能有反复数据。找出反复数据。栓出多余的反复数据。仅仅保留一个反复数据。
思路:记录当中的反复数据次数。比方当中有三个数据都是5,那么反复次数就为2。
将后面的数据依次填充到反复数据位置.
if(nums[i] == nums[i+1]) count++; else nums[i+1-count] = nums[i+1];
题目27:本题和题26相似。27题要求将与给定数据val反复数据都栓出,不保留不论什么一个反复数据,方法相似,记录反复次数比26题多1个即可
if(nums[i] == val) count++; else nums[i-count] = nums[i];
#include <stdlib.h> #include <stdio.h> //26: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 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. */ //思路一直累加反复的个数,数组第i 位= i - 反复的个数 int removeDuplicates(int* nums, int numsSize) { int i = 0; int count = 0; for(i=0;i<numsSize-1;i++) { if(nums[i] == nums[i+1]) { count++; } else { nums[i+1-count] = nums[i+1]; } } return numsSize-count; } //27 :Remove Element //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. int removeElement(int* nums, int numsSize, int val) { int i = 0; int count = 0; for(i=0;i<numsSize;i++) { if(nums[i] == val) { count++; } else { nums[i-count] = nums[i]; } } return numsSize-count; } int main() { int n[10] = {1,2,3,3,3,4,5,5,6,7}; int s[10] = {1,2,3,4,5,6,7,8,9,10}; int r = removeDuplicates(s,10); printf("r = %d\n",r); int i = 0 ; for(i=0;i<r;i++) { printf("n[%d] = %d\n",i,n[i]); } }
相关文章推荐
- xinu在vbox上,使用debian8,并在上面进行xinu编程
- 在Linux最小系统上编译运行第一个helloworld程序
- 掌握DEBUG 基本命令及其功能
- 钱币兑换问题
- iOS Runloop学习
- 【项目 2 - 就拿胖子说事】
- Android中的享元模式
- 如何新建gitHub帐号并与自己电脑进行关联
- IOS学习资源收集--开发UI控件相关
- AI制作牙膏笔刷文字
- PHP正则表达式
- 上机题目(初级)- 鼠标控制小球(Java)
- 太毒的文件操作错误
- 运行Android studio 模板报错
- An ant's story
- 【深入了解cocos2d-x 3.x】定时器(scheduler)的使用和原理探究(3)
- 本周的学习进度情况
- DOM 元素 操作
- Activity的生命周期(四)——多个Activity相互交互的生命周期
- Xcode编译Undefined symbols for architecture xxx 错误总