Remove Duplicates From Sorted Array leetcode java
算法描述:
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 in place with constant memory.
For example,
Given input array nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.
算法分析:一个有序数组,出去其中出现次数大于1的数,返回剩余元素个数
代码:设置两个指针,i 遍历原数组,j 是去除重复后的下标
public int removeDuplicates(int[] nums) {
if(nums == null || nums.length == 0)
return 0;
int len = nums.length;
int j = 1; //数组第一个元素是不需要变的,所以长度至少为1
for(int i = 1; i < len; i++){ //数组遍历从下标1开始
if(nums[i] == nums[i - 1])
continue;
else {
nums[j++] = nums[i];
}
}
return j;
}
Remove Duplicates From Sorted Array leetcode java的更多相关文章
- Remove Duplicates from Sorted Array [LeetCode]
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- Remove Duplicates from Sorted List leetcode java
题目: Given a sorted linked list, delete all duplicates such that each element appear only once. For e ...
- Remove Duplicates From Sorted Array
Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>
LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
随机推荐
- kubeadm 生成的token过期后,集群增加节点
通过kubeadm初始化后,都会提供node加入的token: You should now deploy a pod network to the cluster. Run "kubect ...
- Windows环境下32位汇编语言程序设计笔记-基础篇
内存模式 .386 .model flat,stdcall ;子程序调用模式,win32中只能用stdcall,因为win32api调用使用的这个 option casemap:none ;定义了程序 ...
- hihoCoder week23 最短路径·一
spfa 最短路 #include <bits/stdc++.h> using namespace std; #define pb push_back #define INF 1e16; ...
- Winform异步解决窗体耗时操作(Action专门用于无返回值,Func专门用于有返回值)
http://blog.csdn.net/config_man/article/details/25578767 #region 调用timer控件实时查询开关机时间 private void tim ...
- 配置和运行 MatchNet CVPR 2015 MatchNet: Unifying Feature and Metric Learning for Patch-Based Matching
配置和运行 MatchNet CVPR 2015 GitHub: https://github.com/hanxf/matchnet 最近一个同学在配置,测试这个网络,但是总是遇到各种问题. 我也尝试 ...
- 2、ansilbe常用模块详解及inventory介绍
Ansible ansible格式: ansible <host-pattern> [-f forks] [-m module_name] [-a args] args: 用法 key=v ...
- Leetcode88_Merge Sorted Array_Easy
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...
- Echarts 地图上显示数值
Echarts 地图上展示数值,效果如下: 上代码:关键代码用红色 series: [ { //name: '香港18区人口密度', type: 'map', mapType: 'jiangsu', ...
- MyBatis数据库测试代码自动生成
<!-- generatorConfig.xml配置,其中:<plugin type="org.mybatis.generator.plugins.ToStringPlugin& ...
- JAVA之经典算法
package Set.Java.algorithm; import java.util.Scanner; public class algorithm { /** * [程序1] 题目:古典问题:有 ...