Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Remove Duplicates from Sorted Array

Code Block
languagepy
class Solution(object):
    def removeDuplicates(self, nums):
        if not nums:
            return 0
        
        i = 1
        bound = len(nums)
        prev = nums[0]
        
        while i < bound:
            if prev == nums[i]:
                nums.pop(i)
                bound = len(nums)
            else:
                prev = nums[i]
                i += 1
        
        return len(nums)


Longest common prefix

Info

Input: ["flower", "flow", "flight" ] → Output: "fl"

Input: ["dog", "racecar", "car"] → Output: ""


Code Block
class Solution:
    def longestCommonPrefix(self, strs: List[str]) -> str:
        if not strs:
            return ""
        
        result=""
        cnt=len(strs)
        for i in range(len(strs[0])):
            j=1
            while j<cnt and i<len(strs[j]) and strs[j][i]==strs[0][i]: j+=1
                
            if j==cnt:
                result += strs[0][i]
            else:
                break;
        
        return result;