`
SunnyYoona
  • 浏览: 366956 次
社区版块
存档分类
最新评论

[LeetCode]80.Remove Duplicates from Sorted Array II

 
阅读更多

【题目】

Remove Duplicates from Sorted Array II

Total Accepted:4460Total Submissions:15040My Submissions

Follow up for "Remove Duplicates":
What if duplicates are allowed at mosttwice?

For example,
Given sorted array A =[1,1,1,2,2,3],

Your function should return length =5, and A is now[1,1,2,2,3].

【代码】

/*********************************
*   日期:2014-01-15
*   作者:SJF0115
*   题目: 80.Remove Duplicates from Sorted Array II
*   来源:http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/
*   结果:AC
*   来源:LeetCode
*   总结:
**********************************/
#include <iostream>
#include <stdio.h>
using namespace std;
// 时间复杂度 O(n),空间复杂度 O(1)
class Solution {
public:
    //A数组的重要特点是已经排序
    int removeDuplicates(int A[], int n) {
        if(n == 0){
            return 0;
        }
        else if(n == 1){
            return 1;
        }
        //新数组下标
        int index = 2;
        for(int i = 2;i < n;i++){
            if(A[i] != A[index-2]){
                A[index++] = A[i];
            }
        }
        return index;
    }
};
int main() {
    int result;
    Solution solution;
    int A[] = {1,1,2,2,2,2,4,4,5,6,8,8,8};
    result = solution.removeDuplicates(A,13);
    printf("Length:%d\n",result);
    for(int i = 0;i < result;i++){
        printf("%d ",A[i]);
    }
    return 0;
}






分享到:
评论

相关推荐

    26.Remove Duplicates from Sorted Array删除有序数组中的重复项【LeetCode单题讲解系列】

    26.Remove_Duplicates_from_Sorted_Array删除有序数组中的重复项【LeetCode单题讲解系列

    LeetCode Remove Duplicates from Sorted Array解决方案

    LeetCode Remove Duplicates from Sorted Array解决方案

    lrucacheleetcode-leetcode:leetcode

    lru缓存leetcode leetcode 1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 5. Longest Palindromic Substring 7. Reverse Integer 9. ...

    LeetCode C++全解

    Remove Duplicates from Sorted Array iii. Plus One iv. Pascal's Triangle v. Merge Sorted Array vi. Sum vii. Find Minimum in Rotated Sorted Array viii. Largest Rectangle in Histogram ix. Maximal ...

    leetcode1004-leetcode:leetcode

    leetcode 1004 leetcode E:简单,M:中等,H:困难 数组和字符串 217. Contains Duplicate (E) 48. Rotate Image (M) -&gt; 2 73. Set Matrix Zeroes (M) 1. Two Sum (E) 167. Two Sum II - Input array is sorted (E)...

    lrucacheleetcode-LeetCode:这个库用于总结leetcode中遇到的习题,期望按照数据结构和常用方法分成2类,进行总结,

    leetcode LeetCode 这个库用于总结leetcode中遇到的习题 常用数据结构习题总结 1.线性表 解决进度 No. Describition mark 1 Remove Duplicates from Sorted Array 2 Remove Duplicates from Sorted Array II 3 ...

    javalruleetcode-leetcode-java:力码笔记

    26.Remove Duplicates from Sorted Array 53.Maximum Subarray 70.Climbing Stairs 121.Best Time to Buy and Sell Stock 122.Best Time to Buy and Sell Stock II 123.Best Time to Buy and Sell Stock III 141....

    leetcode叫数-leetcode:leetcode

    leetcode叫数 Leetcode Personal repository implement with Ruby 13. Roman to Integer 查表,通过从前往前筛选字符串,把代表的值一个个加起来 26. Remove Duplicates from Sorted Array 难度easy的题目。根据题目...

    Leetcode经典01背包-algo:一些记录

    Leetcode经典01背包 algo 1. 数据结构与算法 数组,链表,(串和序列) 堆,栈,队列 树,图 排序,搜索 贪心,回溯,动态规划 堆:一种完全二叉树,任意节点大于左右孩子(大顶堆) 树:二叉树,DFS,BFS 1 排序与...

    leetcode338-coding_notebook:编码_笔记本

    String/26_remove_duplicates_from_sorted_array.md) [(雅虎)139。 Word Break](Leetcode 问题/数组和字符串/139.word_break.md) [140. Word Break ii](Leetcode 问题/数组和字符串/140.word_break_ii.md) [151. ...

    leetcode-remove-duplicates-from-sorted-array

    leetcode从重复数组中删除重复项 给定一个已排序的数组nums,就地删除重复项,以使每个元素仅出现一次并返回新的长度。 不要为另一个数组分配额外的空间,必须通过使用O(1)额外的内存就地修改输入数组来做到这...

    LeetCode最全代码

    26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)| [C++](./C++/remove-duplicates-from-sorted-array.cpp) [Python](./Python/remove-duplicates...

    leetcode2-Algorithms-Practice:创建此repo是为了跟踪我在解决问题方面的进展

    leetcode 2 LeetCode-练习 我的 Leetcode“解决方案”(在解决方案/文件夹中)来解决 leetcode 问题。 它用于练习和跟踪进度不是 100% 优化的。 我的账户链接 问题名称 运行时和内存使用 1. Two Sum 运行时间:4412 ...

    leetcode答案-LeetCode:Swift中的LeetCode

    leetcode 答案LeetCode LeetCode in Swift 这个Repo 用来存下我在LeetCode 解题的原始档,包含解题中遇到的错误,也包含解出AC 的答案, 以下的清单将连结到我的Github Pages 中,皆有题目中文翻译与解题的过程。...

    leetcode-liwang:leetcode学习笔记

    O(m+n) time, O(m+n) sapce.*0026 Remove Duplicates from Sorted Array使用双指针,一个快指针,一个慢指针。开始时,两个指针都指向首元素。当两指针元素值相同时,快指针+1;当两指针元素不同时,慢

    leetcode算法题主函数如何写-leetcode:leetcode

    leetcode算法题主函数如何写 leetcode 每日一题 从今天开始,每天必须完成至少2题。即为保持写代码的手感,也巩固一下编码的基础知识。 2018.1.23 题目:Remove Duplicates from Sorted Array Given a sorted array,...

    LeetCode 26. 删除排序数组中的重复项

    题目来源:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array 题目 给定一个排序数组,你需要在 原地 删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。 不要使用额外的...

    test2.1.1_2.1.2.rar_网络编程_C++_

    leetcode 代码Remove Duplicates from Sorted Array

    javalruleetcode-LeetCode::lollipop:个人LeetCode习题解答仓库-多语言

    java lru leetcode :ice_cream: LeetCode Kindem 的个人 LeetCode ...Duplicates from Sorted Array 48 Rotate Image 53 Maximum Subarray 55 Jump Game 56 Merge Intervals 64 Minimum Path Sum 73

    cpp-算法精粹

    Remove Duplicates from Sorted Array II Longest Consecutive Sequence Two Sum 3Sum 3Sum Closest 4Sum Remove Element Move Zeroes Next Permutation Permutation Sequence Valid Sudoku Trapping Rain Water ...

Global site tag (gtag.js) - Google Analytics