DEV Community

Nucu Labs
Nucu Labs

Posted on • Updated on

LeetCode: Arrays 101: Introduction – Solutions

Hello,

LeetCode is a good place to practice programming by solving problems of all levels!

After you solve a problem, you’ll get access to all submitted solutions, sorted by time and memory usage. That’s a nice way to compare your code to others and see what you did well and where you can improve.

Here’s my solutions for the Arrays 101: Introduction card:

Max Consecutive Ones

https://leetcode.com/problems/max-consecutive-ones/

class Solution:
    def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
        max_counter = 0
        counter = 0
        for i in nums:
            if i == 1:
                counter += 1
            else:
                if counter > max_counter:
                    max_counter = counter
                counter = 0
        if counter > max_counter:
            max_counter = counter
        return max_counter
Enter fullscreen mode Exit fullscreen mode

Find Numbers with Even Number of Digits

https://leetcode.com/problems/find-numbers-with-even-number-of-digits/

class Solution:

    @staticmethod
    def is_even(value):
        return len(str(value)) % 2 == 0

    def findNumbers(self, nums: List[int]) -> int:
        return_value = 0
        for number in nums:
            if self.is_even(number):
                return_value += 1
        return return_value
Enter fullscreen mode Exit fullscreen mode

Squares of a Sorted Array

https://leetcode.com/problems/squares-of-a-sorted-array/

When writing code like this be careful because you’re altering the A list in place.

class Solution:
    def sortedSquares(self, A: List[int]) -> List[int]:
        for index, num in enumerate(A):
            A[index] = num ** 2
        return sorted(A)
Enter fullscreen mode Exit fullscreen mode

Thanks for reading!

Top comments (0)