---
# System prepended metadata

title: '20191218'

---

# 20191218

Given a non-empty array of digits representing a non-negative integer, plus one to the integer.

The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.

You may assume the integer does not contain any leading zero, except the number 0 itself.

Example 1:
Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.

Example 2:
Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.

[0] - [1]
[9] - [1, 0]
[0, 1] - X

[1, 2, 9]

129 <- int
129 + 1 = 130 -> split
[1, 3, 0]

1 * 100
2 * 10
9 * 1
129
1 ^ 3 = 1
```
public int[] plusOne(int[] digits) {
  int n = digits.length;
    for(int i = n-1; i >= 0; i--) {
        if(digits[i] < 9) {
            digits[i]++;
            return digits;
        }
        
        digits[i] = 0;
    }
    
    int[] newNumber = new int [n+1];
    newNumber[0] = 1;
    
    return newNumber;
}
```
Design and implement a TwoSum class. It should support the following operations: add and find.

add - Add the number to an internal data structure.
find - Find if there exists any pair of numbers which sum is equal to the value.

Example 1:
add(1); add(3); add(5);
find(4) -> true
find(7) -> false

Example 2:
add(3); add(1); add(2);
find(3) -> true
find(6) -> false

Exampl 3:
add(1); add(1); add(2); add(2);
find(2) -> true
find(3) -> true
find(4) -> true
find(5) -> false

K : V
1 : 2
2 : 2

[1, 2]
```
class TwoSum {
    
    Map<Integer, Integer> map;
    List<Integer> list;
    
    /** Initialize your data structure here. */
    public TwoSum() {
        map = new HashMap<>();
        list = new ArrayList<>();
    }
    
    /** Add the number to an internal data structure.. */
    public void add(int number) {
        if (map.containsKey(number)) {
            map.put(number, map.get(number) + 1);
        } else {
            map.put(number, 1);
            list.add(number);
        }
    }
    
    /** Find if there exists any pair of numbers which sum is equal to the value. */
    // 1 2 3
    // 1 1 2 2
    // 0 find 0
    public boolean find(int value) { // 2
        for (int i = 0; i < list.size(); i++) {
            int num1 = list.get(i); // 1 
            int num2 = value - num1; // 1
        
            if (num1 == num2 && map.get(num1) > 1)
                return true;
            if (num1 != num2 && map.containsKey(num2))
                return true;
        }
        
        return false;
    }
}
```