일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- C++
- generic
- condition
- docker
- var
- vue.js
- vuetify
- security
- nginx
- webpack
- property
- npm
- machine learning
- dotenv
- 보안
- BOJ
- AI
- Python
- Clone
- scss
- git
- TypeScript
- C#
- bash
- VUE
- loop
- JavaScript
- 앙상블
- type
- leetcode
Archives
- Today
- Total
ice rabbit programming
[LeetCode] Remove Duplicates From Sorted Array 본문
728x90
https://leetcode.com/problems/remove-duplicates-from-sorted-array/
Remove Duplicates from Sorted Array - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
특이하게 반환값 자체가 답이 아니라, 반환값 만큼 배열을 검사해서 답을 체크하는 문제였다. 아마 이 때문에 비추가 많은 듯..
이 문제도 C#으로 풀었다.
// C#
public class Solution {
public int RemoveDuplicates(int[] nums) {
if(nums.Length<1)
return 0;
int count=0, result=1;
for(int i=1;i<nums.Length;i++) {
if(nums[i]==nums[i-1]) {
count++;
continue;
}
nums[i-count]=nums[i];
result++;
}
return result;
}
}
in-place로 풀어야 하기 때문에 값을 제거한 숫자만큼 앞으로 당겼고, 제거 하지 않은 만큼을 return하였다.
728x90
'PS > LeetCode' 카테고리의 다른 글
[LeetCode] Longest Common Prefix (0) | 2020.04.15 |
---|---|
[LeetCode] Remove Element (0) | 2020.04.15 |
[LeetCode] 9. Palindrome Number (0) | 2020.04.12 |
[LeetCode] 7. Reverse Integer (0) | 2020.04.12 |
[LeetCode] 21. Merge Two Sorted Lists (0) | 2020.04.12 |