LeetCode 80
来源:互联网 发布:注射水银 知乎 编辑:程序博客网 时间:2024/05/22 00:06
原题:
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?
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.
题意:删除数组中的重复数字,要求同一个数字有两次重复,并返回长度
代码和思路:
class Solution { public int removeDuplicates(int[] nums) { int i = 0; for(int n:nums){ //只有在i小于2(小于两个数) 和 后面的数n大于前面第二个数(因为两数重复)时(因为数组是123递增),才进1,否则i指针不动,判断下一个数。 if(i<2 || n > nums[i-2]){ nums[i++] = n; } } return i; }}同理如果是要求只能有一个数重复的,那么就 i<1 || n>[i-1]即可
阅读全文
0 0
- LeetCode 80
- leetcode 80: Spiral Matrix
- LeetCode 80 Edit Distance
- Leetcode no. 80
- *leetcode #80 in cpp
- leetCode练习(80)
- 80 leetcode - Subsets II
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- tkinter的消息框练习
- 栈相关功能的实现(C语言+C++)
- 欢迎使用CSDN-markdown编辑器
- Go语言进行web开发(一) 搭建一个简单的web服务器
- 快速排序-java
- LeetCode 80
- 使用adb相关命令,获取文件的读写权限以及修改文件权限
- TextView判断是否在省略状态(在列表情况下)
- Java private、protected、默认、public修饰符区别及场景应用
- 最新Ubuntu 16.04 安装配置 ownCloud教程
- Jmeter中文乱码问题
- Requests加正则表达式抓取猫电影信息
- Lucene索引的详细结构
- 简单购物车应用案例