题目:两数之和
采用的是暴力枚举法
leetcode中代码为
using System;
using System. Collections. Generic;
using System. Linq;
using System. Text;
using System. Threading. Tasks;
namespace 两数之和
{
public class Class1
{
public int [ ] TwoSum ( int [ ] nums, int target)
{
int [ ] index = new int [ 2 ] ;
for ( int i = 0 ; i < nums. Length - 1 ; i++ )
{
for ( int j = i + 1 ; j < nums. Length; j++ )
{
if ( nums[ j] == target - nums[ i] )
{ index[ 0 ] = i;
index[ 1 ] = j;
break ;
}
}
}
return index;
} } }
main()函数代码块
static void Main ( string [ ] args)
{ int length = Convert. ToInt32 ( Console. ReadLine ( ) ) ;
int [ ] a = new int [ length] ;
for ( int i= 0 ; i< length; i++ )
{
a[ i] = Convert. ToInt32 ( Console. ReadLine ( ) ) ;
}
Class1 class1 = new Class1 ( ) ;
Console. WriteLine ( "[{0}]" , string . Join ( "," , class1. TwoSum ( a, 9 ) ) ) ;
Console. ReadKey ( ) ;
}
}
}