本人只会python,学算法遇阻。发现市面上几乎都是C,C++,JAVA的算法。求大侠指教,这几段代码是什么语言。其实把慕课网的相关视频都看一些差不多也知道了,想节省时间,就只有请教各位了。谢谢!!!
public int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[j] == target - nums[i]) {
return new int[] { i, j };
}
}
}
throw new IllegalArgumentException("No two sum solution");
}
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], i);
}
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement) && map.get(complement) != i) {
return new int[] { i, map.get(complement) };
}
}
throw new IllegalArgumentException("No two sum solution");
}
onemoo
习惯受伤
幕布斯1840991
zeng_建军
慕先生7708552
rubyc
rubyc
MR帽子先生
IMOOC学习
翊人蝶舞