In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how to delete duplicates in a sorted array in java", which is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to delete duplicates in a sorted array in java".
Given a sorted array, you need to delete the repeating elements in place so that each element appears at most twice, returning the new length of the removed array.
Do not use extra array space, you must modify the input array in place and do it using O (1) extra space.
Example 1:
Given nums = [1, 1, 1, 1, 1, 2, 2, 3]
The function should return the new length length = 5, and the first five elements of the original array are modified to 1, 1, 2, 2, 3.
You don't need to consider the elements in the array that exceed the new length.
Example 2:
Given nums = [0jin0jing1jing1jing1jing1jinjinjingjinshengyue 3]
The function should return the new length length = 7, and the first five elements of the original array are modified to 0,0,1,1,2,3,3.
You don't need to consider the elements in the array that exceed the new length.
Answer:
1public int removeDuplicates (int [] nums) {
2 int n = nums.length
3 if (n
< 2) 4 return n; 5 int pos = 2; 6 for (int i = 2; i < n; ++i) 7 if (nums[i] != nums[pos - 2]) 8 nums[pos++] = nums[i]; 9 return pos; 10} 解析: 题目中说的是排序的数组,那这就简单了。代码比较简单就不再介绍,下面再来看一下其他的解法。 1public int removeDuplicates(int[] nums) { 2 int i = 0; 3 for (int n : nums) 4 if (i < 2 || n >Nums [I-2])
5 nums [iTunes +] = n
6 return i
7}
eight
9public int removeDuplicates (int [] nums) {
10 int n = nums.length, count = 0
11 for (int I = 2; I < n; iTunes +)
12 if (nums [I] = = nums [I-2-count])
13 count++
14 else
15 nums [I-count] = nums [I]
16 return n-count
17}
The solutions are more or less the same, and the basic ideas remain the same.
What are the characteristics of Java? what are the characteristics of Java? what is the 1.Java language that represents the static object-oriented programming language? it implements the object-oriented theory and allows programmers to carry out complex programming in an elegant way of thinking. 2.Java has the characteristics of simplicity, object-oriented, distributed, security, platform independence and portability, dynamic and so on. 3. Using Java, you can write desktop applications, Web applications, distributed systems, embedded system applications, and so on.
The above is all the content of the article "how to remove duplicates in a sorted array in java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.