001 两数之和 (C#力扣)

一、题目


二、解答

①暴力解,直接上两层循环

public class Solution {
    public int[] TwoSum(int[] nums, int target) {
        int[] result=new int[2];
        int i=0;
        int j=nums.Length-1;
        while(i<nums.Length){
            while(j>i){
                if(nums[i]+nums[j]==target){
                    result[0]=i;
                    result[1]=j;
                    break;            
                }
                else{
                    j--;
                }
            }
            i++;
            j=nums.Length-1;
        }       
        return result;
    }
}

执行用时:184 ms, 在所有 C# 提交中击败了19.76%的用户

内存消耗:43 MB, 在所有 C# 提交中击败了82.94%的用户

知识点

C#声明有固定大小的数组:(1条消息) C# 数组的五种声明方式_李天贵-Gloria的博客-CSDN博客_c#声明数组


②先将数组排序,通过两次for()循环来求解

public class Solution {
    public int[] TwoSum(int[] nums, int target) {
        int[] result=new int[2];
        int[] newArr=new int[nums.Length];
        int[] key=new int[2];
        bool find=false;

        for(int i = 0; i < nums.Length; i++){//给新数组深度赋值
            newArr[i] = nums[i];
        }
        Array.Sort(newArr);//给新数组排序

        for(int i=0,j=nums.Length-1;i<j && !find;){
                if(newArr[i]+newArr[j]==target){
                    result[0]=newArr[i];
                    result[1]=newArr[j];
                    find=true;//找到了就不循环了
                }
                else if(newArr[i]+newArr[j]>target){//两数相加若比目标值大,让大下标j--
                    j--;
                }
                else if(newArr[i]+newArr[j]<target){//两数相加若比目标值小,让小下标i++
                    i++;
                }      
        }

        for(int i=0,count=0;i<=nums.Length-1 && count<2;i++){
            if(result[0]==nums[i] || result[1]==nums[i]){ //看结果里的值对应原数组的哪个位置
                key[count]=i;
                count++;
            }
        }

        return key;
    }
}

执行用时:140 ms, 在所有 C# 提交中击败了78.73%的用户

内存消耗:43.3 MB, 在所有 C# 提交中击败了42.84%的用户

知识点

- C#数组排序 Array.Sort()方法:

(1条消息) C# 数组Array.Sort()、List.Sort()排序使用方法_永恒星的博客-CSDN博客_c# 数组排序

- 数组赋值,需要通过深度遍历的方法,一个一个给,如果是单单的“=”,新数组仍然指向同一个地址


三、其他方法

③使用了哈希表,即C#中的Dictionary()键值对,内存换时间了属于是

public class Solution {
    public int[] TwoSum(int[] nums, int target) {
        //new一个哈希表实例
        Dictionary<int,int> kv=new Dictionary<int,int>();
        for(int i=0;i<nums.Length;i++){
            int needNum=target-nums[i];
            //如果哈希表里包含了我们所需要的值
            //且如果存在2*needNum=target的情况,排除自己的下标,输出第二个进行比较的下标
            if(kv.ContainsKey(needNum) && kv[needNum] != i){ 
                return new int[] { i, kv[needNum] };
            }
            //如果哈希表里没有这个差值needNum的键值对,且先前没有存过该键值对(nums[i],i)不为空将其放进去  
            //key:值     value:下标
            if(!kv.ContainsKey(needNum) && !kv.ContainsKey(nums[i])){   
                kv.Add(nums[i],i);
            }
        }
        //数组长度为1的情况
        return new int[] { 0, 0 };
    }
}

执行用时:136 ms, 在所有 C# 提交中击败了86.47%的用户

内存消耗:43.9 MB, 在所有 C# 提交中击败了11.26%的用户

(好像还不太如我自己写的两层for()哈哈哈哈哈)

知识点

- C# Dicitionary<int,int>类/哈希表/键值对:

(1条消息) C#中Dictionary的作用及用法讲解_lfw2019的博客-CSDN博客_c#中dictionary

LeetCode 1. Two Sum 两数之和,C# 哈希表 Dictionary_Fanstorm丶的博客-CSDN博客

- C#中的Dictionary.ContainsKey()方法:

C#中的Dictionary.ContainsKey()方法 - 经验笔记 (nhooo.com)

- C#中的Dictionary.Add()方法:

C# Dictionary.Add()用法及代码示例 - 纯净天空 (vimsky.com)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值