2016.6.17——Remove Duplicates from Sorted Array

Remove Duplicates from Sorted Array

本题收获:

1.“删除”数组中元素

2.数组输出

 

  题目:

  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.

  题目的意思是:返回新的数组不重复的个数,且前输出这些数(但是不是要删除重复的数,重复的数赋值放在后面),如果输出的个数正确,但是输出的数组的数不对,也会报错

  思路:

  我的思路:不知道怎么删除数组

  leetcode:将后面的不重复的值前移,重复的值赋值为最后一个值,这样输出的前length个就为不重复的。

  代码

 1 class MyClass
 2 {
 3 public:
 4     int removeDuplicate(vector<int> nums)
 5     {
 6         int j = 0, n = 0;
 7         n = nums.size();
 8         for (size_t i = 1; i < n; i++)
 9         {
10             if (nums[i - 1] == nums[i])
11             {
12                 j++;
13             }
14             else
15                 nums[i - j] = nums[i];         //亮点所在    
16         }
17         return n-j;
18     }

   我的测试代码:

  

 1 // Remove Duplicates from Sorted Array.cpp : 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include "iostream"
 6 #include "vector"
 7 
 8 using namespace std;
 9 
10 class MyClass
11 {
12 public:
13     int removeDuplicate(vector<int> nums)
14     {
15         int j = 0, n = 0;
16         n = nums.size();
17         for (size_t i = 1; i < n; i++)
18         {
19             if (nums[i - 1] == nums[i])
20             {
21                 j++;
22             }
23             else
24                 nums[i - j] = nums[i];
25         }
26         return n-j;
27     }
28 };
29 
30 
31 int _tmain(int argc, _TCHAR* argv[])
32 {
33     vector<int> nums = { 1, 2, 2, 3, 4, 5, 5 };
34     MyClass solution;
35     vector<int> m;
36     int n = 0;
37     n = solution.removeDuplicate(nums);
38     cout << n << endl;
39     //测试输出数组用
40     /*
41     for (size_t i = 0; i < m.size();i++)
42     {
43         cout << m[i] << " ";
44     }
45     cout << endl;*/
46     system("pause");
47     return 0;
48 }

 

转载于:https://www.cnblogs.com/zhuzhu2016/p/5594994.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/418392.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

前端学习(1976)vue之电商管理系统电商系统之解决attr_val为空

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue import Welcome from ./components/Welcome.vue import Users from ./components/user/Users.vue import Right fr…

Git 版本管理工具命令速查

转自&#xff1a;http://www.jb51.net/article/55442.htm 一、 Git 常用命令速查 git branch 查看本地所有分支git status 查看当前状态 git commit 提交 git branch -a 查看所有的分支git branch -r 查看远程所有分支git commit -am "init" 提交并且加注释 git remo…

前端学习(1979)vue之电商管理系统电商系统之让文本框获得焦点

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue import Welcome from ./components/Welcome.vue import Users from ./components/user/Users.vue import Right fr…

java 递归20210408-3

private void insertCityInfo() {JSONArray jsonArray GetCityCodeHttpUtil.getDistrictData1(COUNTRY);List<Map<?, ?>> list this.convertJsonToMap(jsonArray, new ArrayList<Map<?, ?>>());for (Map<?, ?> map : list) {try {this.i…

关于多线程之GCD的一些学习要点

GCD是当前多线程使用最方便的&#xff0c;也是使用比较多的。 学习GCD主要集中在一下几点&#xff1a; 一、队列&#xff0c;同步&#xff0c;异步 1.主队列&#xff1a;dispatch_get_main_queue(); 2.串行队列&#xff1a;dispatch_queue_create("queue", 0); 3.并行…

前端学习(1980)vue之电商管理系统电商系统之实现文本框和按钮的切换

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue import Welcome from ./components/Welcome.vue import Users from ./components/user/Users.vue import Right fr…

C#基础系列 - 抽象类及其方法的学习

在C#中使用关键字 abstract 来定义抽象类和抽象方法。 不能初始化的类被叫做抽象类&#xff0c;它们只提供部分实现&#xff0c;但是另一个类可以继承它并且能创建它们的实例。 "一个包含一个或多个纯虚函数的类叫抽象类&#xff0c;抽象类不能被实例化&#xff0c;进一步…

前端学习(1977)vue之电商管理系统电商系统之按钮与文本框的切换

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue import Welcome from ./components/Welcome.vue import Users from ./components/user/Users.vue import Right fr…

protobuf 数据解析的2种方法

方法1&#xff1a; message person{required int32 age 1;required int32 userid 2;optional string name 3;} message test{required int32 time 1;required int32 userid 2;required float price 3;optional string desc 4;} 1 #include <string>2 #include <…

前端学习(1978)vue之电商管理系统电商系统之为每一行数据提供单独的value

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue import Welcome from ./components/Welcome.vue import Users from ./components/user/Users.vue import Right fr…

软工学习笔记——代码规范

上大学以来写了这几年的代码&#xff0c;却一直没怎么关注过代码规范相关的问题&#xff0c;直到软工课上讲了之后&#xff0c;才开始有所顾及。上课的时候回头看看自己写过的那些代码&#xff0c;真是丑死了&#xff0c;几个月前自己写的代码现在就已经读不懂了。 看了书上的相…

前端学习(1981)vue之电商管理系统电商系统之完成可选项的添加操作

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue import Welcome from ./components/Welcome.vue import Users from ./components/user/Users.vue import Right fr…

ext4文件系统的delalloc选项造成单次写延迟增加的分析

最近我们的服务进程遇到kill -15后处于Z的状态&#xff0c;变为了僵尸进程&#xff0c;经过/proc/{thread_id}/stack查看其上线程的栈&#xff0c;发现是卡在了fwrite的过程中&#xff0c;而我们的系统中所有文件系统挂载参数都使用了delalloc参数&#xff0c;怀疑是这个原因&a…

前端学习(1982)vue之电商管理系统电商系统之删除参数的操作

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue import Welcome from ./components/Welcome.vue import Users from ./components/user/Users.vue import Right fr…

前端学习(1983)vue之电商管理系统电商系统之清空表格数据

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue import Welcome from ./components/Welcome.vue import Users from ./components/user/Users.vue import Right fr…

python 进程和线程

python中的进程、线程&#xff08;threading、multiprocessing、Queue、subprocess&#xff09; Python中的进程与线程 学习知识&#xff0c;我们不但要知其然&#xff0c;还是知其所以然。你做到了你就比别人NB。 我们先了解一下什么是进程和线程。 进程与线程的历史 我们都知…

NYU Hand Pose Dataset

http://cims.nyu.edu/~tompson/NYU_Hand_Pose_Dataset.htm#overview 转载于:https://www.cnblogs.com/guochen/p/5610681.html

前端学习(1984)vue之电商管理系统电商系统之完成静态属性

目录结构 router.js import Vue from vue import Router from vue-router import Login from ./components/Login.vue import Home from ./components/Home.vue import Welcome from ./components/Welcome.vue import Users from ./components/user/Users.vue import Right fr…

java实现文件在线预览

RequestMapping("/preview1") public void er(HttpServletResponse response){File file new File("G:\\桌面\\Thymeleaf3.0中文翻译文档www.java1234.com.pdf");if (file.exists()){byte[] data null;try {FileInputStream input new FileInputStream(…

Python 运维(三):使用 zipapp 将 Python 程序打包成单个可执行文件

大家好&#xff0c;我是水滴~~ 在 Python 开发中&#xff0c;我们经常需要将应用程序打包成可执行文件&#xff0c;以便在不具备 Python 环境的计算机上运行。Python 提供了多种打包工具&#xff0c;其中之一就是 zipapp。zipapp 可以将 Python 应用程序及其依赖打包成一个单独…