OneCoder Avatar
OneCodercoderli.com · 937 篇博文
🧮 Algo

数据结构与算法

双指针、回溯剪枝、图论与搜索

🎨 视觉封面

LeetCode[Algorithms] Two Sum

📅 2015-09-06·✍️ OneCoder·计算中...·⏱️ 9 分钟
#算法#LeetCode

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
💡 OneCoder 资源指引

所有代码开源上传至 GitHub:yummy-code 仓库 · GESP 专题站:GESP WIKI

🤝 技术交流与答疑

欢迎加入:C++ GESP/CSP 考级答疑群(688906745)Java/Python交流群(982860385),点击可直接加群。

📚

猜你想读 · 相关文章推荐

OneCoder

OneCoder (lihongzheshuai)

一个中年人的自留地,记录学习 C++、GESP/NOI、Java、Python 与算法架构的心得体会。本站唯一网址:coderli.com

💬 读者留言与交流

0 条讨论
✨ 支持 Markdown 语法格式
还没有留言,快来成为第一个讨论者吧!