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. Linux的.run文件简单制作

    run程序安装包实质上是一个安装脚本加要安装的程序,如下图所示: |-----------------|| || 安装脚本 || ||-----------------|| || 程序 || ||-- ...

  2. Error mounting / dev / sdb1 in Ubuntu

    Uncommon users of Ubuntu OS, when connecting USB with NTFS file system, can observe the error: " ...

  3. 前端编码规范 -- html篇

    文档类型 推荐使用 HTML5 的文档类型申明: <!DOCTYPE html> (建议使用 text/html 格式的 HTML.避免使用 XHTML.XHTML 以及它的属性,比如 a ...

  4. Docker:容器与主机时间不同步问题解决

    在Docker容器运行后,可能会发现容器时间与宿主机时间不一致,一般会差8个小时.这样会造成在容器中运行的web程序打出的日志时间与实际时间不一致,如果web程序中有定时任务也会造成影响等,需要对宿主 ...

  5. Trie[字典树] 数据结构及基本操作集

    #include <iostream> #include <stdio.h> #include <cstring> #include <algorithm&g ...

  6. elasticsearch 基础特点

    1.Elasticsearch对复杂分布式机制的透明隐藏特性 Elasticsearch是一套分布式的系统,分布式是为了应对大数据量,隐藏了复杂的分布式机制 分片机制(我们之前随随便便就将一些docu ...

  7. springmvc json 简单例子

    1.控制器层: @RequestMapping("/json.do") @ResponseBody //将会把返回值 转换为json对象 public List<User&g ...

  8. myclipse运行web的一些问题

    一.修改项目访问路径 项目右键>properties(属性)>输入web搜索>双击web>修改Web-Content root内容即可 二. myeclipse中web项目不自 ...

  9. [USACO14MAR]破坏Sabotage 二分答案

    题目描述 Farmer John's arch-nemesis, Farmer Paul, has decided to sabotage Farmer John's milking equipmen ...

  10. thinkphp实现登录后返回原界面

    主要思路还是用session记录原地址,在登录后再跳转回原界面 先保存请求login方法界面的url public function savelogin(){ session('returnUrl', ...