clueless coding // TODO: be smarter

LeetCode 258. Add Digits: C++ Solution



Problem Statement


Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.

Function Signature (C++):
  int addDigits(int num)

Inputs:
  num = 38

Outputs:
  2

Running 38 through addDigits would give something like this:
38 => 3 + 8
11 => 1 + 2
2



TL;DR Code Solution


int addDigitsHelper(int num) {
    int sum = 0;
    while (num > 0) {
        int current = num % 10;
        sum += current;
        num /= 10;
    }
    return sum;
}

int addDigits(int num) {
    while (num > 9) {
        num = addDigitsHelper(num);    
    }
    return num;
}

LeetCode Add Digits: Java Solution



Problem Statement


Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.

Function Signature (Java):
  int addDigits(int num)

Inputs:
  num = 38

Outputs:
  2

Running 38 through addDigits would give something like this:
38 => 3 + 8
11 => 1 + 2
2



TL;DR Code Solution



public int getNext(int num) {
    int res = 0;
    while (num > 0) {
        res += num % 10;
        num /= 10;
    }
    return res;
}

public int addDigits(int num) {
    while (num > 9) {
        num = getNext(num);
    }
    return num;
}

LeetCode 415. Add Strings: CPP Solution



Problem Statement


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

The value of each number is < 5100.

Num1 and num2 only contain 0-9 as digits.

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.

Function Signature (C++):
  string addStrings(string num1, string num2)

Inputs:
  num1 = "5"
  num2 = "193"

Outputs:
  "198"



TL;DR Code Solution


string addStrings(string num1, string num2) {

    string result;

    int indexOne = num1.length() - 1;
    int indexTwo = num2.length() - 1;
    int carry = 0;

    while (indexOne >= 0 || indexTwo >= 0 || carry) {
        int current = carry;

        if (indexOne >= 0) {
            current += num1[indexOne] - '0';
        }

        if (indexTwo >= 0) {
            current += num2[indexTwo] - '0';
        }

        carry = current / 10;
        current = current % 10;

        result = (char)(current + '0') + result;

        indexOne--;
        indexTwo--;
    }

    return result;

}

LeetCode Add Strings: Java Solution



Problem Statement


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

The value of each number is < 5100.

Num1 and num2 only contain 0-9 as digits.

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.

Function Signature (Java):
  String addStrings(String num1, String num2)

Inputs:
  num1 = "10"
  num2 = "198"

Outputs:
  "208"



TL;DR Code Solution


public String addStrings(String num1, String num2) {

    StringBuilder sb = new StringBuilder();
    int carry = 0;

    for(int i = num1.length() - 1, j = num2.length() - 1; i >= 0 || j >= 0 || carry == 1; i--, j--){
        int x = i < 0 ? 0 : num1.charAt(i) - '0';
        int y = j < 0 ? 0 : num2.charAt(j) - '0';
        sb.append((x + y + carry) % 10);
        carry = (x + y + carry) / 10;
    }

    return sb.reverse().toString();

}

LeetCode 413. Arithmetic Slices: C++ Solution



Problem Statement


Arithmetic sequences consist of at least three elements and the difference between any two consecutive elements in the array is the same.

For reference, some arithmetic sequences:

1, 2, 3, 4, 5, 6 - difference: 1
7, 7, 7, 7 - difference: 0
1, 3, 5, 7 - difference: 2

The following sequence is not arithmetic.

1, 3, 7, 15

Given an array of integers A, write a function that returns the number of arithmetic slices in it.

Function Signature (C++):
  int numberOfArithmeticSlices(vector<int>& A)

Inputs:
  A = [1, 2, 3, 4]

Outputs:
  3

We can create three arithmetic sequences from A:
[1, 2, 3]
[2, 3, 4]
[1, 2, 3, 4]



TL;DR Code Solution


int numberOfArithmeticSlices(vector<int>& A) {

    int cur = 0;
    int result = 0;

    for (int i = 2; i < A.size(); i++) {
        if (A[i] - A[i-1] == A[i-1] - A[i-2]) {
            cur += 1;
        }
        else {
            cur = 0;
        }
        result += cur;
    }

    return result;

}

LeetCode 413. Arithmetic Slices: Java Solution



Problem Statement


Arithmetic sequences consist of at least three elements. Furthermore, the difference between any two consecutive elements in the array is the same.

For reference, here are some example arithmetic sequences:

0, 5, 10, 15 - difference: 5
5, 5, 5, 5 - difference: 0

The following sequence is not arithmetic.

2, 3, 6, 15

Given an array of integers A, create a function that returns the total number of arithmetic slices that you can create from it.

Function Signature (Java):
  int numberOfArithmeticSlices(int[] A)

Inputs:
  A = [2, 3, 4, 5]

Outputs:
  3

We can create three different arithmetic sequences from A:
[2, 3, 4]
[3, 4, 5]
[2, 3, 4, 5]



TL;DR Code Solution


public int numberOfArithmeticSlices(int[] A) {

    int result = 0;
    int curr = 0;

    for (int i = 2; i < A.length; i++) {

        if (A[i] - A[i-1] == A[i-1] - A[i-2]) {
            curr += 1;
        }
        else {
            curr = 0;
        }
        result += curr;
    }

    return result;

}