DEV Community

loading...

5 Easy LeetCode questions to start competitive-coding

nagasaisriya profile image code_bts ・1 min read

These are 5 LeetCode questions which boost my confidence to start competitive programming and there can be many easy ones out, but I started with these. I provided my Python solutions to the problems, these solutions can be optimized too, just sharing my approach :)

1. FizzBuzz

class Solution:
    def fizzBuzz(self, n: int) -> List[str]:
        l=[]
        for i in range(1,n+1):
            if i%3==0 and i%5==0:
                l.append("FizzBuzz")
            elif i%3==0:
                l.append("Fizz")
            elif i%5==0:
                l.append("Buzz")
            else:
                l.append(str(i))
        return l
Enter fullscreen mode Exit fullscreen mode

2. Single Number

class Solution:
    def singleNumber(self, nums: List[int]) -> int:
        return 2*sum(set(nums))-sum(nums)
Enter fullscreen mode Exit fullscreen mode

3. Intersection of two arrays

class Solution:
    def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
        return set(nums1).intersection(set(nums2))
Enter fullscreen mode Exit fullscreen mode

4. Fibonacci Number

class Solution:
    def fib(self, n: int) -> int:
        m=0
        if n==0:
            return 0
        elif n==1:
            return 1
        else:
            m=self.fib(n-1)+self.fib(n-2)
            return m
Enter fullscreen mode Exit fullscreen mode

5. Array Partition-I

class Solution:
    def arrayPairSum(self, nums: List[int]) -> int:
        nums.sort()
        return sum(nums[::2])
Enter fullscreen mode Exit fullscreen mode

Just make sure to start your journey no matter if you're a beginner or pro. I just started off and I can definitely feel the difference, be it in my problem-solving ways or maintaining consistency. Hope this article helps!!

Discussion (0)

Forem Open with the Forem app