leetcode26
public class Solution {
public int RemoveDuplicates(int[] nums) {
var len = nums.Length;
if (len == )
{
return ;
}
else
{
var pre = nums[];
var diffindex = ;
for (int i = ; i < nums.Length; i++)
{
if (pre != nums[i])
{
nums[++diffindex] = nums[i];
pre = nums[i];
}
}
return diffindex + ;
}
}
}
python实现如下:
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
s = set()
n = len(nums)
count =
i,j = ,
while i < n:
if len(s) == or nums[i] not in s:
s.add(nums[i])
nums[j] = nums[i]
i +=
j +=
count +=
else:
i +=
return count
不实用额外的空间,实现如下:
class Solution:
def removeDuplicates(self, nums: 'List[int]') -> int:
n = len(nums)
i =
j = i +
count =
while i < n:
while j < n and nums[i] == nums[j]:
j +=
count +=
if i + < n and j < n:
nums[i+] = nums[j]
i +=
else:
return count
return count
leetcode26的更多相关文章
- leetcode-26.删除重复数组中的重复项
leetcode-26.删除重复数组中的重复项 题意 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数 ...
- Leetcode26——删除有序数组中的重复项(双指针法)
Leetcode26--删除有序数组中的重复项(双指针法) 1. 题目简述 给你一个升序排列的数组 nums ,请你原地 删除重复出现的元素,使每个元素只出现一次 ,返回删除后数组的新长度.元素的相对 ...
- LeetCode26 Remove Duplicates from Sorted Array
题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- [Swift]LeetCode26. 删除排序数组中的重复项 | Remove Duplicates from Sorted Array
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
- 删除排序数组中的重复项-leetcode-26
public: int removeDuplicates(vector<int>& nums) { int size=nums.size(); i ...
- leetcode26: 删除排序数组中的重复项
给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 示例 1 ...
- leetCode26.删除排序数组中的重复项
给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. 示例 1 ...
- [array] leetCode-26. Remove Duplicates from Sorted Array - Easy
26. Remove Duplicates from Sorted Array - Easy descrition Given a sorted array, remove the duplicate ...
- scala刷LeetCode--26 删除排序数组中的重复项
一.题目描述 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完 ...
随机推荐
- 剑指 offer面试题20 顺时针打印矩阵
[题目描述] 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1, ...
- React中父子组件间的通信问题
1.https://blog.csdn.net/sinat_17775997/article/details/59103173 (React中父子组件间的通信问题)
- 004——php字符串中处理函数(三)
<?php /** * 字符串替换函数: * str_replace(); 替换字符串或数组元素,区分大小写,第四个参数可选,用于统计替换次数 * str_ireplace()不区分大小写替换 ...
- TextView上下滑动
LocationResult = (TextView) findViewById(R.id.textView1);LocationResult.setMovementMethod(ScrollingM ...
- CentOS6.5系统服务
服务名称 功能 默认 建议 备注说明 NetworkManager 用于自动连接网络,常用在Laptop上 开启 关闭 对服务器无用 abrt-ccpp 开启 自定 对服务器无用 abrt-oop ...
- android中自定义view构造函数ContentItemView(Context context, AttributeSet paramAttributeSet)的用处
自己定义一个view <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...
- .net 面试题总结
1. DataSet和DataReader的区别? DataReader:和数据库处于一直连接状态.只读只能向前读取,一次只能读取一行信息.DataReader每次只在内存中加载一条数据,内存占用少, ...
- Unix网络编程第三版源码编译
配置: $ cd Unix-Network-Programming/ $ chmod 755 configure $ ./configure 主要的工作是检查系统是否有源码编译所依赖的各种资源(系统版 ...
- scikit-learn 学习笔记-- Generalized Linear Models (三)
Bayesian regression 前面介绍的线性模型都是从最小二乘,均方误差的角度去建立的,从最简单的最小二乘到带正则项的 lasso,ridge 等.而 Bayesian regression ...
- CentOS6下源码安装mysql-5.6.25
1.1.系统环境检查 1)检查系统版本 mkdir -p /server/tools/ cd /server/tools/ cat /etc/redhat-release 2)配置域名解析 vim / ...