import java.util.HashMap;/*** @Author :田宇寒.* @Date :Created in 14:19 2021年5月28日* @Description:leetcode1题 求两数组和* @Modified By:* @Version: 1.0$*/public class code1 {class SolutionWithN2 {public int[] twoSum(int[] nums, int target) {/*** create by: 田宇寒* description: 两数和暴力穷举 时间复杂度为O(N^2) 空间复杂度为O(1)* create time: 14:26 2021年5月28日* @Param: nums* @Param: target* @return int[]*/int[] result = new int[]{0, 0};for (int i = 0; i < nums.length; i++) {for (int j = i + 1; j < nums.length; j++) {if (nums[i] + nums[j] == target) {result[0] = i;result[1] = j;}}}return result;}}class SolutionWithN_1 {public int[] twoSum(int[] nums, int target) {/*** create by: 田宇寒* description: 两数和 利用hashmap 时间复杂度为O(N) 空间复杂度为O(N)* create time: 14:27 2021年5月28日* @Param: nums* @Param: target* @return int[]*/HashMap<Integer, Integer> hashMap = new HashMap<>(nums.length);for (int i = 0; i < nums.length; i++) {if (hashMap.containsKey(target - nums[i])) {return new int[] {hashMap.get(target - nums[i]), i};}hashMap.put(nums[i], i);}return new int[0];}}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。