【leetcode系列】Two Sum

解法一,我自己想的,有点弱,时间复杂度O(n^2)。

public class Solution {
	public int[] twoSum(int[] numbers, int target) {
		int[] result = new int[2];
		for (int i = 0; i < numbers.length; i++) {
			for (int j = i + 1; j < numbers.length; j++) {
				if (numbers[i] + numbers[j] == target) {
					result[0] = i;
					result[1] = j;
				}
			}
		}
		if (result[0] == 0) {
			if (result[1] != 1) {
				int tmp = numbers[0];
				numbers[0] = numbers[1];
				numbers[1] = tmp;
				result[0] = 1;
			} else {
				int tmp = numbers[1];
				numbers[1] = numbers[2];
				numbers[2] = tmp;
				result[1] = 2;
				tmp = numbers[0];
				numbers[0] = numbers[1];
				numbers[1] = tmp;
				result[0] = 1;
			}
		}
		return result;
	}

	public void arrayPrint(int[] array) {
		for (int i = 0; i < array.length; i++) {
			System.out.print(array[i] + " ");
		}
		System.out.println();
	}

	public static void main(String[] args) {
		int[] numbers = { 11, -22, 12, 7, 2 };
		int target = 9;
		Solution mSolution = new Solution();
		mSolution.arrayPrint(numbers);
		int[] result = mSolution.twoSum(numbers, target);
		mSolution.arrayPrint(result);
		mSolution.arrayPrint(numbers);
	}
}

解法二,网上大牛的,点击打开链接,很巧妙,用java的hashtable,典型的空间换时间,时间复杂度O(n),但是要满足下标的要求,必须在找出满足条件的数字后再重新排个序,下面是我稍微修改的代码:

public int[] twoSum_2(int[] numbers, int target) {
	int[] result = new int[2];
	HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
	for(int i = 0; i < numbers.length; i++) {
		if(hm.get(target - numbers[i]) != null) {
			result[0] = hm.get(target - numbers[i]);
			result[1] = i;
		} else {
			hm.put(numbers[i], i);
		}
	}
	if (result[0] == 0) {
		if (result[1] != 1) {
			int tmp = numbers[0];
			numbers[0] = numbers[1];
			numbers[1] = tmp;
			result[0] = 1;
		} else {
			int tmp = numbers[1];
			numbers[1] = numbers[2];
			numbers[2] = tmp;
			result[1] = 2;
			tmp = numbers[0];
			numbers[0] = numbers[1];
			numbers[1] = tmp;
			result[0] = 1;
		}
	}
	return result;
}

【leetcode系列】Two Sum

时间: 2024-11-05 11:49:19

【leetcode系列】Two Sum的相关文章

【leetcode系列】3Sum

这个题我最开始的思路是:先一个数定下来,然后在除这个数之外的集合里面找另外两个数,最后计算和.如此反复,对于N个数,需要进行N-2次循环. 我遇到的问题就是怎么找另外两个数,其实我想过参照Two Sum里面的解法,就是用Hashtable存,键值对的结构是<任意两个数的和,<下标1,下标2>>,但是构造这个Hashtable就需要O(N^2),后面真正解的时候有需要O(N^2). 参考了大牛的解法后,明白了找两个数还是用两个下标同时往中间移动比较好,下面上代码. import ja

LeetCode --- 1. Two Sum

题目链接: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. Plea

LeetCode:Range Sum Query - Immutable - 数组指定区间内的元素和

1.题目名称 Range Sum Query(数组指定区间内的元素和) 2.题目地址 https://leetcode.com/problems/range-sum-query-immutable/ 3.题目内容 英文:Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. 中文:给定一个数组nums,求出索引i和j之间元素的和,i一定是小于或等于j

【Leetcode】Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 思路:与[Leetcode]Path Sum 不同

LeetCode:Path Sum - 树的根节点到叶节点的数字之和

1.题目名称 Path Sum(树的根节点到叶节点的数字之和) 2.题目地址 https://leetcode.com/problems/path-sum/ 3.题目内容 英文:Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. 中文:给定一颗二叉树,如

Java for LeetCode 216 Combination Sum III

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example 1

[LeetCode系列]卡特兰数(Catalan Number) 在求解独特二叉搜寻树(Unique Binary Search Tree)中的应用分析

本文原题: LeetCode. 给定 n, 求解独特二叉搜寻树 (binary search trees) 的个数. 什么是二叉搜寻树? 二叉查找树(Binary Search Tree),或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值: 它的左.右子树也分别为二叉排序树. 举个栗子,给定 n = 3, 共有 5 个. 1 3 3 2 1 \ / / / \ 3 2 1 1

LeetCode &quot;Minimum Path Sum&quot; - 2D DP

An intuitive 2D DP: dp[i][j] = min(grid[i-1][j-1] + dp[i-1][j], grid[i-1][j-1] + dp[i][j+1]) class Solution { public: int minPathSum(vector<vector<int> > &grid) { // dp[i][j] = min(dp[i-1][j] + dp[i][j], dp[i][j-1] + dp[i][j]); int n = gri

【LeetCode】Path Sum

题目 Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true,