Can someone modify my C# code to get me the correct answer using my code?


  • 0
    A

    public class Solution {

    public int[] TwoSum(int[] nums, int target) {
        int Target = target;
        int [] Nums = nums;
        int [] output = new int[2];
        for(int i= 0;i<= Nums.Length;i++)
            for(int j=Nums.Length;j>=0;j++)
             { 
                 int sum = Nums[i]+Nums[j];
                 if(sum == Target)
                   output[0] = i;
                   output[1] = j;
                
             }
         return output;
    }
    

    }


  • 0
    N

    你的代码太神奇了!
    public int[] TwoSum(int[] nums, int target) {
    int Target = target;
    int [] Nums = nums;
    int [] output = new int[2];
    for(int i= 1;i<= Nums.Length;i++)
    for(int j=Nums.Length;j>=1;j--)
    {
    int sum = Nums[i-1]+Nums[j-1];
    if(sum == Target)
    {
    output[0] = j;
    output[1] = i;
    break;
    }
    }
    return output;
    }


  • 0
    V

    you can use operator ?: to sort your indices


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.