当前位置:网站首页>leetcode005--原地删除数组中的重复元素
leetcode005--原地删除数组中的重复元素
2022-04-23 04:38:00 【singularityDZF】
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class test05 {
/**
* 给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。
* 不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。
*
* 说明:
* 为什么返回数值是整数,但输出的答案是数组呢?
* 请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。
* 你可以想象内部操作如下:
* // nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
* int len = removeDuplicates(nums);
*
* // 在函数里修改输入数组对于调用者是可见的。
* // 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
* for (int i = 0; i < len; i++) {
* print(nums[i]);
* }
*
* 示例 1:
* 输入:nums = [1,1,2]
* 输出:2, nums = [1,2]
* 解释:函数应该返回新的长度 2 ,并且原数组 nums 的前两个元素被修改为 1, 2 。不需要考虑数组中超出新长度后面的元素。
* 示例 2:
* 输入:nums = [0,0,1,1,1,2,2,3,3,4]
* 输出:5, nums = [0,1,2,3,4]
* 解释:函数应该返回新的长度 5 , 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4 。不需要考虑数组中超出新长度后面的元素。
*
* 提示:
* 0 <= nums.length <= 3 * 104
* -104 <= nums[i] <= 104
* nums 已按升序排列
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入数组的大小:");
int size = sc.nextInt();
System.out.println("请输入一个数组:");
int[] a = new int[size];
for(int i=0; i<size; i++){
a[i] = sc.nextInt();
}
//去除重复元素
int count = 1;
for(int i=1; i<size; i++){
if(a[i] != a[count-1]){
a[count] = a[i];
count++;
}
}
for(int i=0; i<count; i++){
System.out.print(a[i]+" ");
}
}
}
版权声明
本文为[singularityDZF]所创,转载请带上原文链接,感谢
https://blog.csdn.net/dangzefei/article/details/124356914
边栏推荐
- Brushless motor drive scheme based on Infineon MCU GTM module
- Why recommend you to study embedded
- 2020 is coming to an end, special and unforgettable.
- 为什么推荐你学嵌入式
- 优麒麟 22.04 LTS 版本正式发布 | UKUI 3.1开启全新体验
- 补充番外14:cmake实践项目笔记(未完待续4/22)
- Youqilin 22.04 lts version officially released | ukui 3.1 opens a new experience
- Jetpack 之 LifeCycle 组件使用详解
- Stm32f4 MCU ADC sampling and FFT of ARM-DSP Library
- Supplément: annotation
猜你喜欢

Installation of zynq platform cross compiler

Youqilin 22.04 lts version officially released | ukui 3.1 opens a new experience

Go reflection rule

test

无线键盘全国产化电子元件推荐方案
![[echart] Introduction to echart](/img/40/e057f4ac07754fe6f3500f3dc72293.jpg)
[echart] Introduction to echart
![[mapping program design] coordinate inverse artifact v1 0 (with C / C / VB source program)](/img/12/de3b2c6ea98be57a8abe2790debfb5.png)
[mapping program design] coordinate inverse artifact v1 0 (with C / C / VB source program)

RC低通滤波器的逆系统

针对NFT的网络钓鱼

Supplément: annotation
随机推荐
[AI vision · quick review of NLP natural language processing papers today, No. 32] wed, 20 APR 2022
Basic use of shell WC (counting the number of characters)
win10, mysql-8.0.26-winx64. Zip installation
FAQ of foreign lead and alliance Manager
电钻、电锤、电镐的区别
A new method for evaluating the quality of metagenome assembly - magista
VHDL implementation of 32-bit binary to BCD code
Interaction of diet gut microbiota on cardiovascular disease
【时序】基于 TCN 的用于序列建模的通用卷积和循环网络的经验评估
Go reflection - go language Bible learning notes
补充番外14:cmake实践项目笔记(未完待续4/22)
[AI vision · quick review of today's sound acoustic papers, issue 2] Fri, 15 APR 2022
Single chip microcomputer serial port data processing (1) -- serial port interrupt sending data
Chlamydia infection -- causes, symptoms, treatment and Prevention
thymeleaf th:value 为null时报错问题
HMS Core Discovery第14期回顾长文|纵享丝滑剪辑,释放视频创作力
IEEE Transactions on Systems, Man, and Cybernetics: Systems(TSMC)投稿须知
C language character constant
Redis command Encyclopedia
[AI vision · quick review of today's sound acoustic papers, issue 3] wed, 20 APR 2022