Leetcode题1

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

import java.util.HashMap;
import java.util.Vector;

public class Solution {
    public  int[] twoSum(int[] numbers, int target)
    {
        int result[]= new int[2];
        HashMap<Integer,Vector<Integer>> aaa = new HashMap<Integer,Vector<Integer>>();
        for(int i=0;i<numbers.length;i++)
        {
            if( aaa.containsKey(numbers[i]) )
            {
                Vector<Integer> index = aaa.get(numbers[i]);
                index.add(i+1);
            }
            else
            {
                Vector<Integer> index = new Vector<Integer>();
                index.add(i+1);
                aaa.put(numbers[i], index);
            }
        }
        for(int i=0;i<numbers.length;i++)
        {
            int subTarget = target - numbers[i];
            if(aaa.containsKey(subTarget))
            {
                Vector<Integer> index = aaa.get(subTarget);
                for(int j = 0;j<index.size();j++)
                {
                    if(index.get(j)!=i+1)
                    {
                        result[0] = i+1;
                        result[1] = index.get(j);
                        return result;
                    }
                }
            }
        }
        return result;
    }
}

上面是个很笨的解法,下面是Discuss里面一个很酷的解法:

import java.util.Hashtable;

public class Solution {

public int[] twoSum(int[] numbers, int target) {

   int[] result = new int[2];
   Hashtable<Integer, Integer> table = new Hashtable<Integer, Integer>();

   for(int i = 0; i < numbers.length; i++){
      if(table.containsKey(numbers[i])){
             result[0] = table.get(numbers[i]) + 1;
             result[1] = i + 1;
       }
         table.put(target - numbers[i], i);
     }
     return result;
    }
}
时间: 2024-07-30 18:30:40

Leetcode题1的相关文章

算法面试课程笔记000 玩转算法面试 leetcode题库分门别类详细解析

算法面试课程笔记 =============================================================================== 本文地址 : =============================================================================== liuyubobobo老师 <<玩转算法面试 leetcode题库分门别类详细解析>> 为了面试,更为了提升你的算法思维 http:/

LeetCode题218——The Skyline Problem

https://leetcode.com/problems/the-skyline-problem/description/ A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the

Python语言,leetcode题库刷题记录 (四)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(log (m+n)). Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1,

LeetCode题828 —— Unique Letter String

https://leetcode.com/problems/unique-letter-string/description/ A character is unique in string S if it occurs exactly once in it. For example, in string S = "LETTER", the only unique characters are "L" and "R". Let's define 

leetcode题1Two sum 练习

题目为: 给一个整数数组, 返回数组中的两数之和等于指定值的两数在数组中的下标. Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]. public int[] TwoSum(int[] nums, int target) { int[] result=new int[2];//创建一个返回数组: for (int j = 0; j < num

LeetCode题679 —— 24 Game

You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated through *, /, +, -, (, ) to get the value of 24. Example 1: Input: [4, 1, 8, 7] Output: True Explanation: (8-4) * (7-1) = 24 Example 2: Input: [1, 2,

LeetCode题301—Remove Invalid Parentheses

Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may contain letters other than the parentheses ( and ). Example 1: Input: "()())()" Output: ["()()

LeetCode题库13. 罗马数字转整数(c++实现)

问题描述: 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并列的 1.12 写做 XII ,即为 X + II . 27 写做  XXVII, 即为 XX + V + II . 通常情况下,罗马数字中小的数字在大的数字的右边.但也存在特例,例如 4 不写做 IIII,而是 IV.数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得

leetcode题 寻找两个有序数组的中位数

题目描述: 我的成绩: 成绩不是很好,代码本身写的也很乱,本文只是提供一种解题思路. 题目分析: 要求两有序数组的中位数并不难,简单粗暴的方法就是得到两数组合并后的新数组,取其中位数即可,但是难度在于这个时间复杂度有限制,为 O(log(m + n)). 一看这个log,那么很容易想到二分查找算法什么的,而本文的解题过程也正是按照二分的思路来的.通过每次比较两数组的二分位点,来进行中位数的筛选. 解题步骤: 例:有两有序数组L1=[1, 2, 6, 12, 34, 36, 39, 43, 51,