RemoveDuplicatesFromSortedArrayI II,移除有序数组里的重复元素以及移除数组里的某个元素
RemoveDuplicatesFromSortedArrayI:
问题描述:给定一个有序数组,去掉其中重复的元素。并返回新数组的长度。不能使用新的空间。
[1,1,2,3] -> [1,2,3] 3
算法思路:用一个数字记录新数组的最后一个元素的位置
public class RemoveDuplicatesFromSortedArray { public int removeDuplicates(int[] nums)
{
if(nums.length == 0)
{
return 0;
}
int key = nums[0];
int count = 0;//记录新数组最后一个元素的位置,即新数组长度
for(int i = 0; i < nums.length; i ++)
{
if(nums[i]!=key)
{
nums[count++] = key;
key = nums[i];
}
}
nums[count++] = key;
return count;
}
}
与之类似的就是移除数组里某个元素。
public int removeElement(int[] nums, int val) {
if(nums.length == 0)
{
return 0;
}
int count = 0;
for(int i = 0; i < nums.length; i ++)
{
if(nums[i]!=val)
{
nums[count++] = nums[i];
}
}
return count;
}
RemoveDuplicatesFromSortedArrayII:
问题描述:可以重复一次。
For example,
Given sorted array nums = [1,1,1,2,2,3]
,
Your function should return length = 5
, with the first five elements of nums being 1
, 1
, 2
, 2
and 3
. It doesn't matter what you leave beyond the new length.
算法分析:这种题目,要巧妙利用数组的下标,和上一题解法相似,只不过,循环里的判断条件变了。
//[1,1,1,2,2,3] -> [1,1,2,2,3] 5 允许重复一次
public class RemoveDuplicatesfromSortedArrayII
{
public int removeDuplicates(int[] nums)
{
if(nums.length == 0 || nums.length == 1)
{
return nums.length;
}
int count = 1, temp = nums[1];
for(int i = 2; i < nums.length; i ++)
{
if(nums[i] != nums[i - 2])
{
nums[count++] = temp;
temp = nums[i];
}
}
nums[count++] = temp;
return count;
}
}
RemoveDuplicatesFromSortedArrayI II,移除有序数组里的重复元素以及移除数组里的某个元素的更多相关文章
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项 II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- 每日一道 LeetCode (8):删除排序数组中的重复项和移除元素
每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...
- [LeetCode] Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] 82. Remove Duplicates from Sorted List II 移除有序链表中的重复项之二
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- [LeetCode] Remove Duplicates from Sorted List 移除有序链表中的重复项
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- [LeetCode] 83. Remove Duplicates from Sorted List 移除有序链表中的重复项
Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...
- 82. Remove Duplicates from Sorted List II(删除有序链表中的重复元素)
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- 【LeetCode每天一题】Remove Duplicates from Sorted List(移除有序链表中的重复数字)
Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...
- 算法练习之合并两个有序链表, 删除排序数组中的重复项,移除元素,实现strStr(),搜索插入位置,无重复字符的最长子串
最近在学习java,但是对于数据操作那部分还是不熟悉 因此决定找几个简单的算法写,用php和java分别实现 1.合并两个有序链表 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两 ...
- LeetCode(26): 删除排序数组中的重复项
Easy! 题目描述: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间 ...
随机推荐
- 导出Excel功能的3种实现
项目中总会用到Excel的导出功能,接触过好几个项目,发现有个项目的导出实现特别值得学习.这里学习顺带总结一下. 一.三种方法 我遇到的导出目前有3种处理: 每个功能一个导出方法: 写一个通用的Exp ...
- Django设置中文,和时区、静态文件指向
#========================================================== # 设置时区 注意注释上面的:LANGUAGE_CODE.TIME_ZONE.U ...
- FineReport----日期处理
日期处理:http://help.finereport.com/doc-view-819.html DAYSOFMONTH(date):返回当日的该月天数 DATEDELTA(Today(),-1): ...
- confirm() event.target.getAttribute('id')
w <?php $wecho = '<form id="del' . $wid . '" method="POST" action="&q ...
- python类的相关知识第二部分
类的继承.多态.封装 一.类的继承 1.应用场景: 类大部分功能相同,大类包含小类的情况 例如: 动物类 共性:都要吃喝拉撒.都有头有脚 特性: 猫类.走了很轻,叫声特别,喜欢白天睡觉 狗类.的叫声很 ...
- Java 之图形验证码
图形验证码作用 防止恶意注册 防暴力破解 Java 与图片相关的类: Image, ImageIO, BufferedImage, Icon, ImageIcon public static void ...
- 2015-03-22——js常用的String方法
String string.charAt(pos); //返回字符串中pos位置处的字符.如果pos小于0或大于等于string.length返回空字符串.模拟实现:Function.prototy ...
- tomcat 介绍
Tomcat简介 Tomcat是Apache软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache.Sun和其他一些公司及个人共同开发 ...
- rtsp over udp
#include <stdio.h> #include <netinet/in.h> #include <sys/socket.h> #include <st ...
- MySQL查询操作
查询执行路径中的组件:查询缓存.解析器.预处理器.优化器.查询执行引擎.存储引擎SELECT语句的执行流程: FROM Clause --> WHERE Clause --> GROUP ...