1 twoSum
来源:互联网 发布:金工网络课程 编辑:程序博客网 时间:2024/04/30 16:26
package LeetCode;import java.util.Arrays;import java.util.HashMap;/*要求:Given an array of integers, return indices of the two numbers such that they add up to a specific target. *You may assume that each input would have exactly one solution, and you may not use the same element twice. *给一个数组然后还有一个目标值,输出相加等于目标值的两个数组的下标*/public class TwoSum { /** * 这个方案最简单,刚开始被系统判断为超时不通过,改进之后就通过 * * @param nums * @param target * @return */ public static int[] twoSum1(int[] nums, int target) { if (nums == null) return null; int[] result = new int[2]; for (int i = 0; i < nums.length - 1; i++) { for (int j = i + 1; j < nums.length; j++) { if (nums[i] + nums[j] == target) { result[0] = i; result[1] = j; } } } return result; } /** * 这种方法只是第一种方法变成了减法,但是通过,不知什么原因。 * * @param nums * @param target * @return */ public static int[] twoSum2(int[] nums, int target) { int[] answer = new int[2]; for (int i = 0; i < nums.length; ++i) { answer[0] = i; int b = target - nums[i]; for (int j = i + 1; j < nums.length; ++j) { if (nums[j] == b) { answer[1] = j; return answer; } } } return null; } /** * 这是hashMap的做法,时间复杂度变为o(n) * * @param nums * @param target * @return */ public static int[] twoSum3(int[] nums, int target) { int[] answer = new int[2]; HashMap<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 b = target - nums[i]; if (map.containsKey(b) && i != map.get(b)) return new int[] { i, map.get(b) }; } return answer; } /** * * @param nums * @param target * @return */ public static int[] twoSum4(int[] nums, int target) { int numsLength = nums.length; if (numsLength < 2) { return nums; } int[] result = new int[2]; HashMap<Integer, Integer> numsInDictionary = new HashMap<>(); for (int i = 0; i < numsLength; i++) { if (numsInDictionary.containsKey(target - nums[i])) { result[1] = i; result[0] = numsInDictionary.get(target - nums[i]); break; } if (!numsInDictionary.containsKey(nums[i])) { numsInDictionary.put(nums[i], i); } } return result; } /** * * @param nums * @param target * @return */ public static int[] twoSum5(int[] nums, int target) { if (nums == null) return null; int[] nums2 = Arrays.copyOf(nums, nums.length); Arrays.sort(nums2); int a = 0, b = 0; int start = 0, end = nums2.length - 1; // find two nums while (start < end) { int sum = nums2[start] + nums2[end]; if (sum < target) start++; else if (sum > target) end--; else { a = nums2[start]; b = nums2[end]; break; } } // find the index of two numbers int[] res = new int[2]; for (int i = 0; i < nums.length; i++) { if (nums[i] == a) { res[0] = i; break; } } if (a != b) { for (int i = 0; i < nums.length; i++) { if (nums[i] == b) { res[1] = i; break; } } } else { for (int i = 0; i < nums.length; i++) { if (nums[i] == b && i != res[0]) { res[1] = i; break; } } } return res; } public static void main(String[] args) { int[] n = new int[] { 3, 2, 4 }; System.out.println(Arrays.toString(twoSum5(n, 6))); }}
阅读全文
0 0
- 1 TwoSum
- 1、TwoSum
- 1TwoSum
- 1 twoSum
- 1 twoSum
- 1-twoSum
- leetcode 1----twoSum
- LeetCode 1 # TwoSum
- LeetCode 1 TwoSum
- leetcode 1 twoSum
- leetcode #1 TwoSum
- leetcode 1 TwoSum
- leetcode-1 TwoSum
- {Leetcode} 1。TwoSum
- [leetcode 1]TwoSum
- leetcode-1-TwoSum 哈希表
- [C++]LeetCode #1 twoSum
- leetcode 1 TwoSum
- 洛谷P3193 [HNOI2008]GT考试
- codeMB轮 送外卖
- ModbusRTU协议的Java Socket
- 数据结构面试整理(4)
- Ubuntu 静态IP设置
- 1 twoSum
- 阿里云服务器漏洞phpmyadmin CVE-2016-6617解决方法
- TextView添加中划线、下划线等
- 安装geth客户端并转账
- tp中 if标签使用函数
- mysql
- WebGL相关框架及网站
- xml中的<![CDATA[]]>和转义字符
- 算法概论 8.3