My LeetCode grinding. Trying to do a problem a day.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

problem.txt 1.2KB

123456789101112131415161718192021222324252627282930313233343536
  1. Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.
  2. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
  3. Example 1:
  4. Given nums = [1,1,2],
  5. Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
  6. It doesn't matter what you leave beyond the returned length.
  7. Example 2:
  8. Given nums = [0,0,1,1,1,2,2,3,3,4],
  9. Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.
  10. It doesn't matter what values are set beyond the returned length.
  11. Clarification:
  12. Confused why the returned value is an integer but your answer is an array?
  13. Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
  14. Internally you can think of this:
  15. // nums is passed in by reference. (i.e., without making a copy)
  16. int len = removeDuplicates(nums);
  17. // any modification to nums in your function would be known by the caller.
  18. // using the length returned by your function, it prints the first len elements.
  19. for (int i = 0; i < len; i++) {
  20. print(nums[i]);
  21. }