慕移動2572363
2017-01-06 13:49:33
本人只會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 關注
- 6984 瀏覽
添加回答
舉報
0/150
提交
取消
