> For the complete documentation index, see [llms.txt](https://emmaguo100.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emmaguo100.gitbook.io/leetcode/01-18-2020-1.md).

# 01/18/2020 1

![](/files/Bwqx4jCRELQUB2TXKOgL)

Brutal force:

Using two for Loops to go through each element in the array to find if there is another value = sum - nums\[i];

Time O(n^2) Space O(1)

class Solution {&#x20;

public int\[] twoSum(int\[] nums, int target) {

```
   if (nums == null || nums.length < 2){
       return null;
   }
    
   for (int i = 0; i < nums.length; i++){
       for (int j = i +1; j < nums.length; j++){
           if (nums[i] + nums[j] == target){
               return new int[]{i , j};
           }
       }
   }
    return null;
```

} }

Hashmap:

Using hashmap.  While we are iterating and inserting elements into the hash table, we also look back to check if the current element's complement already exists in the hash table. If it exists, we have found a solution and return the indices immediately.

Time O(n) Space O(n)

![](/files/Lh3aNfdInJ0pBFJj8sPA)
