// Code language: C++ classSolution { public: intminDeletion(vector<int>& nums){ vector<int> ans; for (int a: nums) { if (ans.size() % 2 == 0 || ans.back() != a) { ans.emplace_back(a); } } return nums.size() - ans.size() / 2 * 2; } };
1 2 3 4 5 6 7 8 9 10
/* Code language: Go */ funcminDeletion(nums []int)int { var ans []int for _, a := range nums { iflen(ans)%2 == 0 || ans[len(ans)-1] != a { ans = append(ans, a) } } returnlen(nums) - len(ans) / 2 * 2 }
# Code language: Python classSolution: defminDeletion(self, nums: List[int]) -> int: ans, n = 0, len(nums) for i inrange(n): if ans % 2 == 1or i + 1 >= n or nums[i] != nums[i + 1]: ans += 1 return n - ans // 2 * 2
1 2 3 4 5 6 7 8 9 10 11 12
// Code language: Java classSolution { publicintminDeletion(int[] nums) { intans=0; for (inti=0, n = nums.length; i < n; i++) { if (ans % 2 == 1 || i + 1 >= n || nums[i] != nums[i + 1]) { ans++; } } return nums.length - ans / 2 * 2; } }
1 2 3 4 5 6 7 8 9 10 11 12 13
// Code language: C++ classSolution { public: intminDeletion(vector<int>& nums){ int ans = 0; for (int i = 0, n = nums.size(); i < n; i++) { if (ans % 2 == 1 || i + 1 >= n || nums[i] != nums[i + 1]) { ans++; } } return nums.size() - ans / 2 * 2; } };
1 2 3 4 5 6 7 8 9 10
/* Code language: Go */ funcminDeletion(nums []int)int { n, ans := len(nums), 0 for i := 0; i < n; i++ { if ans % 2 == 1 || i + 1 >= n || nums[i] != nums[i + 1] { ans++ } } return n - ans / 2 * 2 }