class Solution {
public:
int removeDuplicates(int A[], int n) {
int *s=&A[],*e=&A[]; //s指向开头第一个,e往后遍历相同的
int t,i,j=n;
for(i=;i<n;i++){
e++;
if(*s==*e)
j--;
else{
s++;
*s=*e;
}
}
return j;
}
};

题意:给一个整型有序数组,将其中重复的元素删除,几个相同的元素只留下一个即可,并返回共有多少种不同的元素。

思路:这是数组,所以有重复的地方就要移动后面的元素,用两个指针s和e,e用来遍历所有元素,s用来指向当前已处理过的地方,最终e会遍历到第n个,s会指向新数组的最后一个元素。

注意:没什么好注意的,这么短的代码。指针不要越界。

LeetCode Remove Duplicates from Sorted Array删除整型数组中的重复元素并返回剩下元素个数的更多相关文章

  1. [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  2. LeetCode Remove Duplicates from Sorted Array II 删除整型数组中的重复元素并返回剩下元素个数2

    class Solution { public: int removeDuplicates(int A[], int n) { ],*e=&A[]; //s指向“连续数字”的第一个,e往后遍历 ...

  3. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  4. LeetCode:Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

  5. [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)

    https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...

  6. leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  7. [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  8. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  9. [Leetcode] Remove Duplicates From Sorted Array II (C++)

    题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...

随机推荐

  1. sqlserver2012——.Net

    1.Connection 属性: ConnectionString:获取或者设置用于打开SQLServer数据库的字符串 Database:获取当前数据库或者连接打开后要使用的数据库名称 State: ...

  2. CodeForces 118C 【模拟】

    思路: 枚举0-9之间的数,然后判断. 然后一鼓作气打成了大模拟....我日啊... 心疼自己. #include <bits/stdc++.h> using namespace std; ...

  3. (PHP)redis String(字符串)操作

    /** * * String操作 * 字符串操作 * */ //设置键值:成功返回true,否则返回false,键值不存在则新建,否则覆盖 $redis->set('string', 'hell ...

  4. django进阶之缓存

    Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据库,并编写数据访问层代码 业务逻辑层去调用数据访问层执行 ...

  5. Vue-multiselect详解(Vue.js选择框解决方案)

    github地址:https://github.com/shentao/vue-multiselect 官网链接:https://vue-multiselect.js.org/#sub-getting ...

  6. JS实现拖拽功能

    本文代码地址(第一节):https://github.com/dirstart/js-exam/blob/master/%E6%8B%96%E6%8B%BDdiv1.html 第二节:https:// ...

  7. std::ref() 与 &

    引言 之前因为调整样式把博客园的样式毁了,所以一直在自己的另一个博客上更新,有兴趣的可以去观望一下:http://blog.yunlambert.top/最近还是把博客园拾起来吧..... 最近看到一 ...

  8. vue 生产模式

    vue.js 最后一行添加... Vue.config.productionTip = false;

  9. git commit之后撤销

    先git log 查看日志,找到需要回退的那次commit的哈希值 然后git reset --soft commit_id ok

  10. aop 切面配置

    <bean id="userServiceImpl" class="com.bj.aop.xml.before.UserServiceImpl">& ...