Remove Duplicates from Sorted Array II
来源:互联网 发布:网络试衣间 编辑:程序博客网 时间:2024/06/08 11:06
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?
For example,
Given sorted array A = [1,1,1,2,2,3]
,
Your function should return length = 5
, and A is now [1,1,2,2,3]
.
java
public class Solution { /** * @param A: a array of integers * @return : return an integer */ public int removeDuplicates(int[] nums) { // write your code here if (nums == null || nums.length == 0) { return 0; } int index = 0; int count = 0; for (int i = 1; i < nums.length; i++) { if (nums[index] == nums[i]) { if (count < 1) { nums[++index] = nums[i]; count++; } } else { nums[++index] = nums[i]; count = 0; } } return index + 1; }}
python
class Solution: """ @param: nums: An ineger array @return: An integer """ def removeDuplicates(self, nums): # write your code here if nums is None or len(nums) == 0: return 0 index, count = 0, 0 for i in range(1, len(nums)): if nums[index] == nums[i]: if count < 1: index += 1 nums[index] = nums[i] count += 1 else: index += 1 nums[index] = nums[i] count = 0 return index + 1
阅读全文
0 0
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- LeetCode: Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- [LeetCode] Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- [leetcode] Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- Remove duplicates from sorted array II
- Remove Duplicates from Sorted Array II
- [LeetCode]Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- [leetcode]Remove Duplicates from Sorted Array II
- LeetCode-Remove Duplicates from Sorted Array II
- thinking in java——持有对象
- JDBC PreparedStatement实现数据库增删改查案例
- openstack 创建虚拟机
- jupyter notebook 多个python kernel配置
- 设计模式-组合模式
- Remove Duplicates from Sorted Array II
- Session 和 Cookie 的比较
- Servlet处理流程分析
- C#学习笔记
- [bzoj5042][ST表]LWD的分科岛
- A SQLiteConnection object for database '/data/user/0/com.google.android.gms/
- 列表渲染
- Messages:This method: edit for action XXX is not allowed!-Struts2.5动态方法调用错误解决方法
- 【deeplearning.ai】第二门课:提升深层神经网络——偏差和方差