当前位置:网站首页>Leetcode005 -- delete duplicate elements in the array in place
Leetcode005 -- delete duplicate elements in the array in place
2022-04-23 04:39:00 【singularityDZF】
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class test05 {
/**
* Here's an ordered array nums , Would you please In situ Delete duplicate elements , Make each element Only once , Returns the new length of the deleted array .
* Don't use extra array space , You must be there. In situ Modify input array And using O(1) Complete with extra space .
*
* explain :
* Why is the return value an integer , But the output answer is array ?
* Please note that , The input array is based on 「 quote 」 By way of transmission , This means that modifying the input array in a function is visible to the caller .
* You can imagine the internal operation as follows :
* // nums In order to “ quote ” By way of transmission . in other words , Do not make any copies of the arguments
* int len = removeDuplicates(nums);
*
* // Modifying the input array in a function is visible to the caller .
* // Based on the length returned by your function , It prints out the array Within this length All elements of .
* for (int i = 0; i < len; i++) {
* print(nums[i]);
* }
*
* Example 1:
* Input :nums = [1,1,2]
* Output :2, nums = [1,2]
* explain : Function should return the new length 2 , And the original array nums The first two elements of are modified to 1, 2 . You don't need to think about the elements in the array that follow the new length .
* Example 2:
* Input :nums = [0,0,1,1,1,2,2,3,3,4]
* Output :5, nums = [0,1,2,3,4]
* explain : Function should return the new length 5 , And the original array nums The first five elements of are modified to 0, 1, 2, 3, 4 . You don't need to think about the elements in the array that follow the new length .
*
* Tips :
* 0 <= nums.length <= 3 * 104
* -104 <= nums[i] <= 104
* nums In ascending order
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(" Please enter the size of the array :");
int size = sc.nextInt();
System.out.println(" Please enter an array :");
int[] a = new int[size];
for(int i=0; i<size; i++){
a[i] = sc.nextInt();
}
// Remove duplicate elements
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://yzsam.com/2022/04/202204230438035418.html
边栏推荐
- Improving 3D object detection with channel wise transformer
- [pytoch foundation] torch Split() usage
- win10, mysql-8.0.26-winx64. Zip installation
- A heavy sword without a blade is a great skill
- VHDL implementation of 32-bit binary to BCD code
- eksctl 部署AWS EKS
- MYSQL查询至少连续n天登录的用户
- QML进阶(四)-绘制自定义控件
- Huawei machine test -- high precision integer addition
- Go reflection - go language Bible learning notes
猜你喜欢
Jetpack 之 LifeCycle 组件使用详解
HMS Core Discovery第14期回顾长文|纵享丝滑剪辑,释放视频创作力
win10, mysql-8.0.26-winx64. Zip installation
IDE Idea 自动编译 与 On Upate Action 、 On Frame Deactivation 的配置
[paper reading] [3D target detection] point transformer
Common string processing functions in C language
MYSQL50道基础练习题
Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
【论文阅读】【3d目标检测】Improving 3D Object Detection with Channel-wise Transformer
Bacterial infection and antibiotic use
随机推荐
zynq平臺交叉編譯器的安裝
Single chip microcomputer serial port data processing (2) -- ucosiii + cyclic queue receiving data
leetcode002--将有符号整数的数字部分反转
補:注解(Annotation)
Basic operation of sequence table
[AI vision · quick review of NLP natural language processing papers today, issue 31] Fri, 15 APR 2022
Installation of zynq platform cross compiler
Mysql, binlog log query
A heavy sword without a blade is a great skill
1个需求的一生,团队协作在云效钉钉小程序上可以这么玩
leetcode009--用二分查找在数组中搜索目标值
Record the blind injection script
Migrate from MySQL database to AWS dynamodb
2021数学建模国赛一等奖经验总结与分享
IEEE Transactions on Industrial Informatics(TII)投稿须知
Go reflection rule
【Echart】echart 入門
leetcode008--实现strStr()函数
Go reflection - go language Bible learning notes
记录一下盲注脚本