本人只会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");}
- 18 回答
- 0 关注
- 6523 浏览
添加回答
举报
0/150
提交
取消