My LeetCode grinding. Trying to do a problem a day.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

12345678910111213141516171819202122232425262728293031
  1. #include <stdio.h>
  2. int removeDuplicates(int*, int);
  3. int removeDuplicates(int* nums, int numsSize) {
  4. if (numsSize == 0) {
  5. return 0;
  6. }
  7. int shift = 0;
  8. int last = nums[0] + 1; // This way the first item will never be a duplicate
  9. for (int i = 0; i < numsSize; i++) {
  10. int value = nums[i];
  11. nums[i - shift] = value;
  12. if (value == last) {
  13. shift++;
  14. }
  15. last = value;
  16. }
  17. return numsSize - shift;
  18. }
  19. int main() {
  20. printf("Expetected: Length 5, [0, 1, 2, 3, 4]\n");
  21. int arr[10] = {0,0,1,1,1,2,2,3,3,4};
  22. int newLength = removeDuplicates(&arr[0], 10); // &arr[0] is a hack to stop warning about static sized pointer being passed
  23. printf("Got: Length: %d,", newLength);
  24. for (int i = 0; i < newLength; i++) {
  25. printf(" %d ", arr[i]);
  26. }
  27. printf("\n");
  28. return 0;
  29. }