[C#][LeetCode][Easy] 1. Two Sum

心得:

滿基本的一題,只是我的方法有點爛XD

 

題目:

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution.

答案:

public 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){
                    return new int[]{i, j};
                }
            }
        }
        
        throw new Exception("error");
    }
}

 



這裡的資訊對您有用嗎?歡迎斗內給我