Leetcode第一天:towSum

Leetcode day1

by jojoflyw


Two Sum

tag: array ,easy

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.

Example:

Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1]. 

Accepted
1,647,064
Submissions
3,827,081

中文注解

indices: 下标,index的复数形式
such that : 致使 在这里插入在这里插入代码片代码片
assume that: 假定

给出一个整型的数组,要求返回此数组两数之和与指定的target值一样的的下标;
你需要保证每个输入只能对应一个输出结果,并且不能两次使用数组内的同一元素。

代码实现

//2019年4月8日 16:39:09
public class TwoSum {
	public int[] twoSum(int[] nums,int target) {
		int[] tosum= {0,0};
		boolean flag = false;
		for(int i=0;i<nums.length;i++) {
			for(int j =1;j<nums.length-1;j++) {
				if(nums[i]+nums[j]==target) {
					tosum[0]=i;
					tosum[1]=j;
					flag = true;
					//此处break非常重要
					break;
				}
			}
		}
		if(flag==false) {
			System.out.println("数组中没有等于此数的数");
			System.exit(0);
		}
		return tosum;
	}
	
	public static void main(String[] args) {
		int[] nums = {2,7,11,15};
		int target = 9;
		int[] result = new Solution().twoSum(nums,target);
		for(int i=0;i<result.length;i++) {
			System.out.println(result[i]);
		}
	}
}

以上为jojo的实现方案,供参考,注意到题目要求只能有一个解决方案,所以一旦找到相等就需要break
显然这个版本时间复杂度为O(n^2)
显然如果数组元素过多,这个方法极其不适用,需要想办法降低其时间复杂度

题目透析

分析题目:找到两个数字之和与所给数字相等的两数的下标,本质上其实就是一个查找算法然后找到索引,自然想到HashCode不就是这样吗?并且Java提供了一个HashMap方法,可以直接保存key-valu且Map内的时间复杂度降为O(1),整体降为O(n)
代码如下

import java.util.HashMap;
//2019年4月9日 18:56:37
public class Solution {

	public int[] twoSum(int[] numbers, int target) {
		HashMap<Integer,Integer> map = new HashMap<>();
		int[] result = new int[2];
		for(int i = 0;i<numbers.length;i++) {
			if(map.get(target-numbers[i])!=null) {
				result[0] = map.get(target - numbers[i]);
				result[1] = i;
				break;
			}else {
				map.put(numbers[i], i);
			}
		}
		return result;
	}
	
	public static void main(String[] args) {
		int[] nums = {2,7,11,15};
		int target = 9;
		int[] result = new Solution().twoSum(nums,target);
		for(int i=0;i<result.length;i++) {
			System.out.println(result[i]);
		}
	}
}

补充:
TreeMap类的java.util.TreeMap.get()方法用于检索或获取由参数中提到的特定键映射的值。当地图不包含密钥的这种映射时,它返回NULL。
java源文件的说明

 /**
 * Returns the value to which the specified key is mapped,
 * or {@code null} if this map contains no mapping for the key.
 *
 * <p>More formally, if this map contains a mapping from a key
 * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
 * key.equals(k))}, then this method returns {@code v}; otherwise
 * it returns {@code null}.  (There can be at most one such mapping.)
 *
 * <p>A return value of {@code null} does not <i>necessarily</i>
 * indicate that the map contains no mapping for the key; it's also
 * possible that the map explicitly maps the key to {@code null}.
 * The {@link #containsKey containsKey} operation may be used to
 * distinguish these two cases.
 *
 * @see #put(Object, Object)
 */
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}

4/9/2019 9:09:17 PM

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值