LeetCode/Algorithms

1. Two Sum

챠오위 2021. 10. 11. 22:53

 

이 문제에서 주의할 점은 Example 2: 에서 Output: [0,0] 이 나오면 안된다는 것

 

 

class Solution {
    public int[] twoSum(int[] nums, int target) {
        for( int i=0; i<nums.length; i++ ){
            for( int j=i+1; j<nums.length; j++ ){
                if( nums[i] + nums[j] == target ){
                    int[] ans = {i, j};
                    return ans;
                }
            }
        }
        return nums;
    }
}

 

솔직히 이중 for문 밖에 생각나지 않았다.

그 결과는...

 

 

성공을 보기는 했지만 생각을 좀 더 해야겠다.