删除排序数组中的重复数字 II
生活随笔
收集整理的這篇文章主要介紹了
删除排序数组中的重复数字 II
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目連接
http://www.lintcode.com/zh-cn/problem/remove-duplicates-from-sorted-array-ii/
題目大意
跟進“刪除重復數字”:
如果可以允許出現兩次重復將如何處理?
算法思想
因為數組中元素已經排好順序的,直接遍歷比較有幾個是相同的,如果大于2個就直接把兩個的存到一個數組中,最后返回數組的大小即可。
代碼實現
public class Solution {/*** @param A: a array of integers* @return : return an integer*/public int removeDuplicates(int[] nums) {int n = nums.length;int index = 0;for (int i = 0;i<n;i++) {if (i > 0 && i<n-1 && nums[i] == nums[i-1] && nums[i] == nums[i+1]) {continue;} else {nums[index++] = nums[i];}}return index;} }
轉載于:https://www.cnblogs.com/airycode/p/7687155.html
總結
以上是生活随笔為你收集整理的删除排序数组中的重复数字 II的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【习题 5-14 UVA - 1598】
- 下一篇: 常用的方法论-鱼骨图