题目:

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 1122 and 3. It doesn't matter what you leave beyond the new length.

思路:记录每个数字的次数,并且维护新数组的位置。

public class Solution {public int removeDuplicates(int[] nums) {if(nums.length == 0)return 0;int walker = 1; //每个的数量int count = 1; //总类数量int loc = 1; //定位for(int i = 1 ; i < nums.length ; i++){            if(nums[i] == nums[i-1]){if(walker < 2){walker++;count++;}else{    //出现次数大于2次walker++;continue;}}else{walker = 1;    //出现新数字count++;}nums[loc] = nums[i];loc++;}return count;}
}

转载于:https://www.cnblogs.com/TinyBobo/p/4510441.html

[LeetCode-JAVA] Remove Duplicates from Sorted Array II相关推荐

  1. LeetCode 80. Remove Duplicates from Sorted Array II

    80. Remove Duplicates from Sorted Array II My Submissions QuestionEditorial Solution Total Accepted: ...

  2. LeetCode之Remove Duplicates from Sorted Array II

    1.题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ...

  3. 【Leetcode】Remove Duplicates from Sorted Array II

    题目:对上一题的延伸,每个数字可以出去2次. 思路:还是设置两个下标.第一个lenxb标记已去重的地方,第二个i标记待处理的位置.每次比较时,比较lenxb和lenxb-1两个位置,如果都相等,说明出 ...

  4. Leetcode OJ: Remove Duplicates from Sorted Array I/II

    删除排序数组重复元素,先来个简单的. Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates i ...

  5. 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++> 给出排序好的 ...

  6. Remove Duplicates from Sorted Array II -- LeetCode

    原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/  这道题跟Remove Duplicates ...

  7. [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

    Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For examp ...

  8. Remove Duplicates from Sorted Array II leetcode java

    题目: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For e ...

  9. [LeetCode] Remove Duplicates from Sorted Array II

    Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For examp ...

  10. 【leetcode】Remove Duplicates from Sorted Array

    题目:Given a sorted array, remove the duplicates in place such that each element appear only once and ...

最新文章

  1. 极客新闻——07、团队管理方法,让员工做事效率翻倍
  2. JavaScript 3D图表
  3. error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏的解决方案
  4. 织梦自定义字段html,织梦dedecms自定义字段,首页怎么调用
  5. 11-分步查询懒加载
  6. python语言输入两个数_python的函数输入两个参数吗
  7. 智慧园区主要功能及典型案例分析
  8. 1152Google Recruitment
  9. Cocos Creator 编辑器扩展
  10. django的命令, 配置,以及django使用mysql的流程
  11. 3D建模场景怎么做?
  12. 位深度8位什么水平_佳能1DX3视频12位RAW拍摄和8位mp4拍摄的色彩有多大差别
  13. RTKLIB单点定位处理流程之一(postpos/后处理)
  14. 69、Android获取每日运动步数
  15. SPLAYER中文字幕乱码
  16. [BZOJ2742/Luogu4598][HEOI2012]Akai的数学作业
  17. RMAN Crosscheck/Delete
  18. pymysql 插入错误:Warning(1265, “Data truncated for column ‘XXXX‘ at row xxx“)
  19. (转)操作系统实现多线程的几种模式
  20. make install - make uninstall - make clean 命令介绍

热门文章

  1. 计算机视觉未有深度学习之前
  2. Python安装以及简单使用
  3. 微信端php 开发技术要求,PHP微信开发技术 - WebFalse文档托管平台
  4. php-java-bridge 作用_PHP-Java-Bridge的使用(平安银行支付功能专版)
  5. 深度学习知识抽取:属性词、品牌词、物品词
  6. 基于依存句法规则的篇章级情感分析demo
  7. Catboost:超越Lightgbm和XGBoost的又一个boost算法神器
  8. java与can总线开发_CANdbc编辑器的下载和入门介绍
  9. java js对象转字符串数组_js 转json格式的字符串为对象或数组(前后台)的方法
  10. 提高python 程序运行速度_3个Python函数帮程序员们避免编写循环,提高运行速度...