VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > C#编程 >
  • C#教程之两数之和

本站最新发布   C#从入门到精通
试听地址  
https://www.xin3721.com/eschool/CSharpxin3721/

两数之和

 
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
 
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
 
示例:
 
给定 nums = [2, 7, 11, 15], target = 9
 
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
 
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/two-sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
 
 
复制代码
using System;
using System.Collections.Generic;

namespace Wrox
{
    public class TwoNum
    {
        static void Main()
        {
            Solution s = new Solution();
            int[] result = new int[2];
            int[] nums = new int[10];
            int target = 9;
            for (int i = 0; i < 10; i++)
            {
                nums[i] = int.Parse(Console.ReadLine());
            }
            result = s.soluteByHash(nums, target);
            Console.WriteLine("[" + result[0] + "," + result[1] + "]");
            Console.ReadLine();
            return;
        }
    }
    public class Solution
    {
        public int[] solueTwoNum(int[] nums, int target)            //暴力破解
        {
            int Length = nums.Length;
            int[] result = new int[2];
            int flag = 1;
            for (int i = 0; i < Length - 1; i++)
            {
                for (int j = 1; j < Length; j++)
                {
                    if (nums[i] + nums[j] == target)
                    {
                        result[0] = i;
                        result[1] = j;
                        flag = 0;
                    }
                    if (flag == 0)
                        break;
                }
                if (flag == 0)
                    break;
            }
            return result;
        }
        public int[] soluteByHash(int[] nums,int target)           //一遍哈希表
        {
            var dic = new Dictionary<int, int>();
            for(int i = 0; i < nums.Length; i++)
            {
                var currentNum = nums[i];
                var temp = target - nums[i];                       //target减去当前数据得到temp
                if (dic.ContainsKey(temp))                         //temp若在字典中存在则有匹配值,返回下标
                {
                    return new int[] { dic[temp], i };
                }
                else if (!dic.ContainsKey(currentNum))             //当前数据不在字典中,则添加进字典
                {
                    dic.Add(currentNum,i);
                }
            }
            return null;
        }
    }
}
复制代码

 

 

 

 
分类: (C#) 数据结构和算法练习
标签: C#, 算法和数据结构练习题
好文要顶 关注我 收藏该文  
AsahiLock
关注 - 0
粉丝 - 2
 
 
+加关注
0
0
 
 
 
« 上一篇: 直线DDA,直线和圆的Bresenham算法
» 下一篇: Matlab图像处理基础知识
posted @ 2019-09-24 20:23  AsahiLock  阅读(119)  评论(3)  编辑  收藏
 

 
  
#1楼 2019-09-30 15:19 酷学大叔
你这种是暴力破解,其实你可以使用hashtable来做,C#的话可以使用Dictionary
支持(1) 反对(0)
  
#2楼 [楼主2019-09-30 20:15 AsahiLock
@ 酷学大叔
我复习了一下哈希表,也看了C#的Dictionary泛型的作用,但没想到怎样在这题使用有更好的效率,如果有更好的算法可以告诉我么,麻烦的话简单的说一下就好,可以的话非常感谢。
支持(0) 反对(0)
  
#3楼 2019-10-02 13:02 酷学大叔
@ AsahiLock
public int[] TwoSum(int[] nums, int target) {
var dic = new Dictionary<int, int>();
for (int i = 0; i < nums.Length; i++)
{
var x = nums[i];
var temp = target - x;
if (dic.ContainsKey(temp))
return new int[] { dic[temp], i };
else
if(!dic.ContainsKey(x))
dic.Add(x, i);

}
return null;
}

LeeCode上有题解,您可以看看

相关教程