LeetCode Remove Duplicates from Sorted Array II
来源:互联网 发布:获取二维码扫描枪数据 编辑:程序博客网 时间:2024/03/29 21:08
Description:
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?
Solution:
和Remove Duplicates from Sorted Array相比,只要多一个步骤,将和当前nums[i]相同的个数计算一下,记录为count,如果count大于2,则跳过,否则就添加到数组中。
import java.util.*;public class Solution {public int removeDuplicates(int[] nums) {if (nums.length == 0)return 0;int tot = 1;int count = 1;for (int i = 1; i < nums.length; i++) {if (nums[i] == nums[i - 1])count++;elsecount = 1;if (count <= 2) {nums[tot++] = nums[i];}}return tot;}}
0 0
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- [LeetCode] Remove Duplicates from Sorted Array II
- [leetcode] Remove Duplicates from Sorted Array II
- [LeetCode]Remove Duplicates from Sorted Array II
- [leetcode]Remove Duplicates from Sorted Array II
- LeetCode-Remove Duplicates from Sorted Array II
- [leetcode] Remove Duplicates from Sorted Array II
- LeetCode - Remove Duplicates from Sorted Array II
- LeetCode:Remove Duplicates from Sorted Array II
- LeetCode | Remove Duplicates from Sorted Array II
- Leetcode: Remove Duplicates from Sorted Array II
- 【leetcode】Remove Duplicates from Sorted Array II
- Leetcode: Remove Duplicates from Sorted Array II
- [LeetCode] Remove Duplicates from Sorted Array II
- Leetcode Remove Duplicates from Sorted Array II
- CentOS 安装 VNC
- VC获取当前程序文件的路径,文件名以及路径+文件名
- 在ASP.NET MVC5应用程序中快速接入QQ和新浪微博OAuth
- AndroidMainfest.xml详解——<meta-data>
- 这两天比较烦躁
- LeetCode Remove Duplicates from Sorted Array II
- 黑马day11 悲观锁&乐观锁
- Java学习日记之方法
- Spring的IOC容器—Bean的自动装配
- 10_Android中通过HttpUrlConnection访问网络,Handler和多线程使用,读取网络html代码并显示在界面上,ScrollView组件的使用
- 黑马程序员-java基础-面向对象总结
- java集合
- Oracle 11.2.0.2 安装条件检查之pdksh 包
- 拉格朗日插值