Press "Enter" to skip to content

Posts tagged as “base”

花花酱 LeetCode 168. Excel Sheet Column Title

Given an integer columnNumber, return its corresponding column title as it appears in an Excel sheet.

For example:

A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28 
...

Example 1:

Input: columnNumber = 1
Output: "A"

Example 2:

Input: columnNumber = 28
Output: "AB"

Example 3:

Input: columnNumber = 701
Output: "ZY"

Example 4:

Input: columnNumber = 2147483647
Output: "FXSHRXW"

Constraints:

  • 1 <= columnNumber <= 231 - 1

Solution: Base conversion

Time complexity: O(logn)
Space complexity: O(logn)

C++

Related Problems

花花酱 LeetCode 1837. Sum of Digits in Base K

Given an integer n (in base 10) and a base k, return the sum of the digits of n after converting n from base 10 to base k.

After converting, each digit should be interpreted as a base 10 number, and the sum should be returned in base 10.

Example 1:

Input: n = 34, k = 6
Output: 9
Explanation: 34 (base 10) expressed in base 6 is 54. 5 + 4 = 9.

Example 2:

Input: n = 10, k = 10
Output: 1
Explanation: n is already in base 10. 1 + 0 = 1.

Constraints:

  • 1 <= n <= 100
  • 2 <= k <= 10

Solution: Base Conversion

Time complexity: O(logn)
Space complexity: O(1)

C++

花花酱 LeetCode 504. Base 7

Problem

Given an integer, return its base 7 string representation.

Example 1:

Input: 100
Output: "202"

Example 2:

Input: -7
Output: "-10"

Note: The input will be in range of [-1e7, 1e7].

Solution: Simulation

Time complexity: O(logn)

Space complexity: O(logn)