NC61 两数之和
两数之和
http://www.nowcoder.com/practice/20ef0972485e41019e39543e8e895b7f
NC61 两数之和
python
##
#
# @param numbers int整型一维数组
# @param target int整型
# @return int整型一维数组
#
class Solution:
def twoSum(self , numbers , target ):
# write code here
for i in range(len(numbers)-1):
for j in range(i+1, len(numbers)):
if (numbers[i]+numbers[j]==target):
return [i+1,j+1]
Java
import java.util.*;
public class Solution {
/**
*
* @param numbers int整型一维数组
* @param target int整型
* @return int整型一维数组
*/
public int[] twoSum (int[] numbers, int target) {
for(int i=0; i< numbers.length-1; i++)
{
for (int j=i+1; j<numbers.length;j++)
{
if(numbers[i]+numbers[j]==target)
{
return new int[]{i+1,j+1};
}
}
}
return null;
}
}


查看11道真题和解析