LeetCode 001_Two Sum Java

public class Solution {
	    public int[] twoSum(int[] nums, int target) {
	        int temp;
	        boolean flag = false;
	        int[] index = new int[2];
	        for(int i = 0; i < nums.length; i++){
	            temp = target - nums[i];
	            for(int j = 0; j < nums.length; j++){
	            	if(i == j) continue;
	                if(temp == nums[j]){
	                    index[1] = j+1;
	                    flag = true;
	                    break;
	                }
	            }
	            if(flag){
	                index[0] = i+1;
	                break;
	            }
	        }
	        return index;
	    }

思路二:
把数组的值作为map的key, 由于满足 nums[i] + nums[j] = target ,故必存在key值满足nums的元素
	    public int[] twoSum2(int[] nums, int target) {
	        int[] index=new int[2];
	        HashMap<Integer,Integer> hm = new HashMap<Integer,Integer>();  

	        for(int i = 0; i<nums.length; i++)
	        {
	            if(hm.containsKey(target - nums[i]))
	            {
	                index[1] = i+1;
	                index[0] = hm.get(target-nums[i])+1;
	                return index;
	            }else
	            {
	                 hm.put(nums[i],i);
	            }
	        }
	        return index;
	    }
	}

时间: 2024-10-09 20:55:40

LeetCode 001_Two Sum Java的相关文章

LeetCode OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro

leetcode -- 3 sum

3-sum 题目描述: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. 题目要求: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ 

[leetcode]Combination Sum @ Python

原题地址:https://oj.leetcode.com/problems/combination-sum/ 题意: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited

Leetcode:Path Sum 二叉树路径和

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

LeetCode: Combination Sum [038]

[题目] Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target)

LeetCode: Combination Sum II [039]

[题目] Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including target) will be

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:Combination Sum 子集和问题

Combination Sum: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (includ

LeetCode: Path Sum II [113]

[题目] 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] ] [题意] 判断二叉树中是否存在一条从根到