LeetCode / Two Sum

LeetCode / Two Sum

Problem

Solution 1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class Solution {
    public int[] twoSum(int[] nums, int target) {
        int i = 0, j = 0;
        
        loop:
        for (i = 0; i < nums.length; i++) {
            for (j = i + 1; j < nums.length; j++) {
                if (nums[i] + nums[j] == target) {
                   break loop; 
                }
            }
        }
        
        return new int[] {i, j};
    }
}
Solution 1
  • Description
    • Performs brute force search without duplicates
  • Time Complexity
    • O(len(nums)^2)
    • Two nested for loops of size len(nums)
  • Space Complexity
    • O(len(nums))
    • Memory usage proportional to len(nums) for function input