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) 额外空间 ...
随机推荐
- 【Charles】使用教程+破解+Windows版本https乱码+https证书安装注意
一.使用教程参考: 这一篇就够了,其他都是大同小异.Windows版和MAC版使用没太多区别. Charles 从入门到精通 | 唐巧的博客 https://blog.devtang.com/2015 ...
- 最全的Eclipse使用快捷键
Eclipse 是一种基于 Java 的可扩展开源开发平台.尽管 Eclipse 是使用 Java 语言开发的,但它的用途并不限于 Java 语言,Eclipse 还包括插件开发环境等,下面将为大家介 ...
- MongoDB的Python客户端PyMongo(转)
原文:https://serholiu.com/python-mongodb 这几天在学习Python Web开发,于是做准备做一个博客来练练手,当然,只是练手的,博客界有WordPress这样的好玩 ...
- 001-ant design pro安装、目录结构、项目加载启动【原始、以及idea开发】
一.概述 1.1.脚手架概念 编程领域中的“脚手架(Scaffolding)”指的是能够快速搭建项目“骨架”的一类工具.例如大多数的React项目都有src,public,webpack配置文件等等, ...
- JetBrains ReSharper 8.2 Build 8.2.0.2160 && StyleCop
先安装 StyleCop 再安装 JetBrains ReSharper 8.2 Build 8.2.0.2160
- 编译原理课后习题答案令A,B和C是任意正规式,证明以下关系成立(A|B)*=(A*B*)*=(A*|B*)*
题目: 令A.B和C是任意正规式,证明以下关系成立: A∣A=A (A*)*= A* A*=ε∣A A* (AB)*A=A(BA)* (A∣B)*=(A*B ...
- 转:用unix socket加速php-fpm、mysql、redis的连接
图虫的服务器长期是单机运行.估计除了mysql之外,php-fpm和redis还可以在单机上共存很长时间.(多说服务器早就达成了单机每日2000万+动态请求,所以我对单机搞定图虫的大流量非常乐观) 如 ...
- SharePoint 2010 以Jquery Ajax方式更新SharePoint列表数据!
之前本人的博客介绍了<sharepoint 2010自定义访问日志列表设置移动终端否和客户端访问系统等计算列的公式>,那如何通过Jquery提交访问日志到自定义的SharePoint的访问 ...
- oracle 建立新的表空间
创建数据表空间 注意粗斜体部分 create tablespace 表空间名称logging datafile 'D:\app\Administrator\oradata\orcl\XXXX.dbf' ...
- 9. Palindrome Number(判断整型数字是否是回文,直接暴力即可)
Determine whether an integer is a palindrome. Do this without extra space. class Solution: def isPal ...