It seems that coding tests are conducted overseas in interviews with engineers, and in many cases, the main thing is to implement specific functions and classes according to the theme.
As a countermeasure, it seems that a site called Let Code will take measures.
A site that trains algorithmic power that can withstand coding tests that are often done in the home.
I think it's better to have the algorithm power of a human being, so I'll solve the problem irregularly and write down the method I thought at that time as a memo.
Leet Code Table of Contents Starting from Zero
Last time Leet Code Day47 starting from zero "14. Longest Common Prefix"
Right now, I'm prioritizing the Medium of the Top 100 Liked Questions. I solved all Easy, so if you are interested, please go to the table of contents.
Twitter I'm doing it.
26. Remove Duplicates from Sorted Array The difficulty level is Easy.
The problem is given a sorted array. The problem is that it removes duplicate elements from the array, displays the element to write only once, and returns a new length.
Given nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
It doesn't matter what you leave beyond the returned length.
Given nums = [0,0,1,1,1,2,2,3,3,4],
Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.
It doesn't matter what values are set beyond the returned length.
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
n = 0
for i in range(1,len(nums)):
if nums[n] < nums[i]:
n += 1
nums[n] = nums[i]
return n+1
# Runtime: 84 ms, faster than 77.52% of Python3 online submissions for Remove Duplicates from Sorted Array.
# Memory Usage: 15.6 MB, less than 35.88% of Python3 online submissions for Remove Duplicates from Sorted Array.
I wrote it by licking the elements from the beginning, comparing the index of each nums with the prepared n
, and processing it.
There is no need to twist it, and it can be said that it is an orthodox process.
I think this problem is a good problem for learning how to use arrays, for statements, and if statements, so even beginners who are interested can easily solve it, so I think it's easy to recommend.
Up to here for this time. Thank you for your hard work.
Recommended Posts