数据结构与算法
双指针、回溯剪枝、图论与搜索
🎨 视觉封面LeetCode[Algorithms] Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
{% highlight java %} package com.coderli.leetcode.algorithms;
import java.util.HashMap; import java.util.Map;
/**
-
Given an array of integers, find two numbers such that they add up to a specific target number.
-
-
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
-
-
You may assume that each input would have exactly one solution.
-
-
-
Input: numbers={2, 7, 11, 15}, target=9
-
Output: index1=1, index2=2
-
-
@author li.hzh
-
@date 2015-08-26 21:49 */ public class TwoSum {
public static void main(String[] args) { int nums[] = {-2, -7, 11, 15}; int target = -9; Solution solution = new TwoSum().new Solution(); SolutionTwo solutionTwo = new TwoSum().new SolutionTwo(); int result[] = solution.twoSum(nums, target); System.out.println("index1=" + result[0] + ", index2=" + result[1]); result = solutionTwo.twoSum(nums, target); System.out.println("index1=" + result[0] + ", index2=" + result[1]); }
/**
- 最简单的O(n2)的解法 */ public class Solution { public int[] twoSum(int[] nums, int target) { int[] output = new int[2]; int length = nums.length; for (int i = 0; i < length - 1; i++) { for (int j = i + 1; j < length; j++) { if (nums[i] + nums[j] == target) { if (i <= j) { output[0] = i + 1; output[1] = j + 1; } else { output[0] = j + 1; output[1] = i + 1; } } } } return output; } }
/**
- O(n2) runtime, O(1) space – Brute force:
-
- The brute force approach is simple. Loop through each element x and find if there is another value that equals to target – x. As finding another value requires looping through the rest of array, its runtime complexity is O(n2).
-
- O(n) runtime, O(n) space – Hash table:
-
- We could reduce the runtime complexity of looking up a value to O(1) using a hash map that maps a value to its index. */ public class SolutionTwo { public int[] twoSum(int[] nums, int target) { Map map = new HashMap(); int[] output = new int[2]; int length = nums.length; for (int i = 0; i < length; i++) { int j = target - nums[i]; if (map.containsKey(j)) { int index = (int) map.get(j); if (index < i) { output[0] = index + 1; output[1] = i + 1; } else { output[0] = i + 1; output[1] = index + 1; } return output; } map.put(nums[i], i); } return null; } } } {% endhighlight %} 总结:以空间换时间,解决以前关于HashMap的时间复杂度的误区,查询了HashMap实现相关的文章。 参考:http://it.deepinmind.com/%E6%80%A7%E8%83%BD/2014/04/24/hashmap-performance-in-java-8.html
所有代码开源上传至 GitHub:yummy-code 仓库 · GESP 专题站:GESP WIKI
欢迎加入:C++ GESP/CSP 考级答疑群(688906745) 与 Java/Python交流群(982860385),点击可直接加群。
猜你想读 · 相关文章推荐
LeetCode[Algorithms] Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add...
LeetCode[Algorithms] Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcb...
LeetCode[Algorithms] Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(...
OneCoder (lihongzheshuai)
一个中年人的自留地,记录学习 C++、GESP/NOI、Java、Python 与算法架构的心得体会。本站唯一网址:coderli.com