Press "Enter" to skip to content

Posts tagged as “easy”

花花酱 LeetCode 905. Sort Array By Parity

Problem

Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.

You may return any answer array that satisfies this condition.

Example 1:

Note:

  1. 1 <= A.length <= 5000
  2. 0 <= A[i] <= 5000

Solution 1: Split Odd/Even

Time complexity: O(n)

Space complexity: O(n)

C++

Solution 2: Stable sort by key % 2

Time complexity: O(nlogn)

Space complexity: O(1) in-place

C++

花花酱 LeetCode 9. Palindrome Number

Problem

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example 1:

Input: 121
Output: true

Example 2:

Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.

Example 3:

Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.

Follow up:

Could you solve it without converting the integer to a string?

Solution 1: Convert to string (cheating)

Time complexity: O(log10(x))

Space complexity: O(log10(x))

C++

Solution 2: Digit by Digit

Every time we compare the first and last digits of x, if they are not the same, return false. Otherwise, remove first and last digit and continue this process.

How can we achieve that via int math?

e.g. x = 9999, t = pow((10, int)log10(x)) = 1000

first digit: x / t, last digit: x % 10

then x = (x – x / t * t) / 10 removes first and last digits.

t /= 100 since we removed two digits.

x / t = 9 = 9 = x % 10, 9999 => 99

9 = 9, 99 => “”

Time complexity: O(log10(x) / 2)

Space complexity: O(1)

C++

花花酱 LeetCode 7. Reverse Integer

Problem

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output: 321

Example 2:

Input: -123
Output: -321

Example 3:

Input: 120
Output: 21

Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

Solution: Simulation

Reverse digit by digit. Be careful about the overflow and negative numbers (especially in Python)

Time complexity: O(log(x)) ~ O(1)

Space complexity: O(log(x)) ~ O(1)

C++

Java

Python3

花花酱 LeetCode 896. Monotonic Array

An array is monotonic if it is either monotone increasing or monotone decreasing.

An array A is monotone increasing if for all i <= jA[i] <= A[j].  An array A is monotone decreasing if for all i <= jA[i] >= A[j].

Return true if and only if the given array A is monotonic.

Solution: 

C++

Java

Python

花花酱 LeetCode 70. Climbing Stairs

Problem

You are climbing a stair case. It takes n steps to reach to the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

Note: Given n will be a positive integer.

Example 1:

Input: 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps

Example 2:

Input: 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step

 

 

Solution: DP

Time complexity: O(n)

Space complexity: O(n)

C++ O(n)

C++ O(1)