Problems related to base

This is a commonly used pattern.

Problem 0: 67. Add Binary

LeetCode #67. Add Binary
Given two binary strings, return their sum (also a binary string).

The input strings are both non-empty and contains only characters 1 or 0.

Example 1:

Input: a = “11”, b = "1"
Output: “100”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public String addBinary(String a, String b) {
StringBuilder sb = new StringBuilder();
int i = a.length() - 1, j = b.length() - 1, carry = 0;

while (i >= 0 || j >= 0) {
int sum = carry;
if (i >= 0) sum += a.charAt(i--) - '0';
if (j >= 0) sum += b.charAt(j--) - '0';
sb.append(sum % 2);
carry = sum / 2;
}

if (carry != 0) sb.append(carry);
return sb.reverse().toString();
}
}

Problem 1: Base 7

LeetCode #504. Base 7

Given an integer, return its base 7 string representation.

Example 1:
Input: 100
Output: “202”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public String convertToBase7(int num) {
StringBuilder sb = new StringBuilder();
if (num == 0) return "0";

boolean negative = false;
if (num < 0) {
num = -num;
negative = true;
}

while (num / 7 != 0) {
sb.append(num % 7);
num /= 7;
}

if (num != 0) sb.append(num);
if (negative) sb.append('-');
return sb.reverse().toString();
}
}

Problem 2: 231. Power of Two

LeetCode #231. Power of Two
Given an integer, write a function to determine if it is a power of two.

Example 1:

Input: 1
Output: true
Explanation: 2^0 = 1

1
2
3
4
5
6
7
class Solution {
public boolean isPowerOfTwo(int n) {
if (n == 0) return false;
while (n % 2 == 0) n /= 2;
return n == 1;
}
}

326. Power of Three

LeetCode 326. Power of Three
Given an integer, write a function to determine if it is a power of three.

Example 1:

Input: 27
Output: true

1
2
3
4
5
6
7
class Solution {
public boolean isPowerOfThree(int n) {
if (n == 0) return false;
while (n % 3 == 0) n /= 3;
return n == 1;
}
}

342. Power of Four

LeetCode 342. Power of Four

Given an integer (signed 32 bits), write a function to check whether it is a power of 4.

Example 1:

Input: 16
Output: true

1
2
3
4
5
6
7
class Solution {
public boolean isPowerOfFour(int num) {
if (num == 0) return false;
while (num % 4 == 0) num /= 4;
return num == 1;
}
}

Problem 3: Add Two Numbers

LeetCode #2. Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if (l1 == null) return l2;
if (l2 == null) return l1;

int carry = 0;
ListNode res = new ListNode(0);
ListNode dummy = res;
while (l1 != null && l2 != null) {
int sum = l1.val + l2.val + carry;
carry = sum / 10;
res.next = new ListNode(sum % 10);
res = res.next;
l1 = l1.next;
l2 = l2.next;
}

while (l2 != null) {
int sum = l2.val + carry;
carry = sum / 10;
res.next = new ListNode(sum % 10);
res = res.next;
l2 = l2.next;
}

while (l1 != null) {
int sum = l1.val + carry;
carry = sum / 10;
res.next = new ListNode(sum % 10);
res = res.next;
l1 = l1.next;
}

if (carry != 0) {
res.next = new ListNode(carry);
res = res.next;
}

return dummy.next;
}
}

Problem 4: Add Strings

LeetCode #415. Add Strings

Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.

Note:

The length of both num1 and num2 is < 5100.
Both num1 and num2 contains only digits 0-9.
Both num1 and num2 does not contain any leading zero.
You must not use any built-in BigInteger library or convert the inputs to integer directly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public String addStrings(String num1, String num2) {
if (num1 == null || num1.length() == 0) return num2;
if (num2 == null || num2.length() == 0) return num1;

StringBuilder sb = new StringBuilder();

int i = num1.length() - 1, j = num2.length() - 1;
int carry = 0;
while (i >= 0 && j >= 0) {
int sum = num1.charAt(i--) - '0' + num2.charAt(j--) - '0' + carry;
carry = sum / 10;
sb.append(sum % 10);
}

while (i >= 0) {
int sum = num1.charAt(i--) - '0' + carry;
carry = sum / 10;
sb.append(sum % 10);
}

while (j >= 0) {
int sum = num2.charAt(j--) - '0' + carry;
carry = sum / 10;
sb.append(sum % 10);
}

if (carry != 0) sb.append(carry);
return sb.reverse().toString();
}
}