leetcode 26. Remove Duplicates from Sorted Array从排序数组中移出重复元素(双指针)
来源:互联网 发布:速卖通翻译软件 编辑:程序博客网 时间:2024/05/21 12:50
问题描述:
Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
思路:
因为不能使用额外空间,所以使用双指针,一个个向后搜索并排列。len代表目前的长度,i去进行一个个的搜索。
代码:
class Solution { public int removeDuplicates(int[] nums) { if(nums == null) return 0; int len = 1; for(int i = 1; i < nums.length; i++){ if(nums[i] != nums[i-1]){ if(nums[i] != nums[len]) nums[len] = nums[i]; len++; } } return len; }}
阅读全文
0 0
- leetcode 26. Remove Duplicates from Sorted Array从排序数组中移出重复元素(双指针)
- LeetCode 26 Remove Duplicates from Sorted Array(从已排序数组中移除重复元素)
- LeetCode 26 Remove Duplicates from Sorted Array(从已排序数组中移除重复元素)
- Leetcode 80. Remove Duplicates from Sorted Array II(从已排序数组中移除重复元素)
- leetcode:Remove Duplicates from Sorted Array去掉排序数组中重复的元素
- LeetCode: Remove Duplicates from Sorted Array II(在排序数组中删除重复元素)
- [leetcode 26]Remove Duplicates from Sorted Array——移除排序数组中重复元素
- 26. Remove Duplicates from Sorted Array(从排序数组中删除重复) —— Java
- LeetCode-26-Remove Duplicates from Sorted Array(消除已排序数组中的重复元素)
- Remove Duplicates from Sorted Array--从有序数组中移除重复元素
- Remove Duplicates from Sorted Array——移除排序数组中重复元素
- Remove Duplicates from Sorted Array 从排序数组中删除重复
- Leetcode刷题记——26. Remove Duplicates from Sorted Array(删除数组重复元素)
- Remove Duplicates from Sorted Array(去掉重复的元素)leetcode
- 【LeetCode-面试算法经典-Java实现】【026-Remove Duplicates from Sorted Array(删除排序数组中的重复元素)】
- Leetcode刷题记—— Remove Duplicates from Sorted Array II(已排序数组移除重复元素2)
- Remove Duplicates from Sorted Array 删除数组中的重复元素
- leetcode 2. 从有序链表和数组中移出重复元素 Remove Duplicates
- MIMXRT1050-EVK开发板在IAR环境下的下载问题
- 实测 CORS 跨域访问之 Access-Control-Allow-Origin 多域名设置
- 你可能不需要vuex
- redis学习笔记五(持久化)
- 深度学习系列笔记之线性代数
- leetcode 26. Remove Duplicates from Sorted Array从排序数组中移出重复元素(双指针)
- 浅谈node ---- (1)
- 有嚼头的逻辑实现
- Codeforces Beta Round #25 (Div. 2 Only)
- windows环境下批处理实现守护进程
- 网页在线翻译
- 基于MSP430内部10位ADC的模数转化并通过串口发送
- java异常
- Java调用其他程序时waitFor()阻塞