DEV Community

Nucu Labs
Nucu Labs

Posted on • Edited on

2

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!

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay