Advertisement
sweet1cris

Untitled

Jan 9th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.64 KB | None | 0 0
  1. public class Solution {
  2.     /**
  3.      * @param A: a array of integers
  4.      * @return : return an integer
  5.      */
  6.     public int removeDuplicates(int[] nums) {
  7.         if (nums == null || nums.length == 0) {
  8.             return 0;
  9.         }
  10.         int index = 0, count = 1;
  11.         for (int i = 1; i < nums.length; i++) {
  12.             if (nums[i] == nums[index]) {
  13.                 if (count < 2) {
  14.                     nums[++index] = nums[i];
  15.                     count ++;
  16.                 }
  17.             } else {
  18.                 nums[++index] = nums[i];
  19.                 count = 1;
  20.             }
  21.         }
  22.         return index + 1;
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement