1: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. 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

java:

public int[] twoSum(int[] numbers, int target) {
        if(null!=numbers && numbers.length>0){
            int index1 =0,index2=0;
            boolean findResult = false;
            for(int i=0,len=numbers.length-1;i<len;i++){
                index1=i;
                for(int j=i+1,len2=numbers.length;j<len2;j++){
                    index2=j;
                    if(numbers[index1]+numbers[index2]==target){
                        findResult = true;
                        break;
                    }
                }
                if(findResult){
                    break;
                }
            }
            
            if(0!=index2){
                return new int[]{index1,index2};
            }
        }
        return null;
    }
    
    public int[] twoSum2(int[] numbers, int target) {
        
        int lenL = numbers.length/2;
        int lenR = numbers.length/2+1;
        
        if(numbers.length==2){
            lenL=0;
            lenR=1;
        }
        
        if(numbers[lenL]+numbers[lenR]==target){
            return new int[]{lenL,lenR};
        }else if(numbers[lenL]+numbers[lenR]>target){
            int [] numCopy = new int[lenR];
            System.arraycopy(numbers, 0, numCopy, 0, lenR);
            return this.twoSum2(numCopy, target);
        }else{
            int [] numCopy = new int[lenR];
            System.arraycopy(numbers, lenL, numCopy, 0, numbers.length-lenL);
            int [] numResult = this.twoSum2(numCopy, target);
            numResult[0]+=lenL;
            numResult[1]+=lenL;
            return numResult;
        }
        
    }

时间: 2024-12-12 20:45:29

1:Two Sum的相关文章

每日算法之三十一:Combination Sum

给定一个整数序列,求解一个子序列,子序列之和等于给定目标值.子序列满足以下条件: 1)子序列是有序的 2)子序列的元素个数不限,可以是给定元素的重复元素. 3)结果中的子序列是唯一的 原题描述如下: 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 repeat

HDU1244:Max Sum Plus Plus Plus

题目链接:Max Sum Plus Plus Plus 题意:在n个数中取m段数使得这m段数之和最大,段与段之间不能重叠 分析:见代码 //dp[i][j]表示前i个数取了j段的最大值 //状态转移:dp[i][j]=max(dp[k][j-1]+(sum[k+l[j]-sum[k]或者sum[i]-sum[i-l[j]) (0<=k<=i-l[j]) // 这种做法是O(n^2)的 //如果改成O(n)的呢? //需要加一个数组max_dp[i][j]表示前i个数取j段的dp最大值 //如果

LeetCode1: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. Please note t

HDU 1003:Max Sum(DP)

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 142742    Accepted Submission(s): 33225 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max s

HDU4961:Boring Sum

Problem Description Number theory is interesting, while this problem is boring. Here is the problem. Given an integer sequence a1, a2, -, an, let S(i) = {j|1<=j<i, and aj is a multiple of ai}. If S(i) is not empty, let f(i) be the maximum integer in

通过位运算求两个数的和(求解leetcode:371. Sum of Two Integers)

昨天在leetcode做题的时候做到了371,原题是这样的: 371. Sum of Two Integers Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Example: Given a = 1 and b = 2, return 3. 因为之前完全没有在实际练习中使用过位运算,所以刚看到这道题目的时候我的第一反应是 1.用乘除代替加减,但是一想,

SQL-W3School-函数:SQL SUM() 函数

ylbtech-SQL-W3School-函数:SQL SUM() 函数 1.返回顶部 1. SUM() 函数 SUM 函数返回数值列的总数(总额). SQL SUM() 语法 SELECT SUM(column_name) FROM table_name SQL SUM() 实例 我们拥有下面这个 "Orders" 表: O_Id OrderDate OrderPrice Customer 1 2008/12/29 1000 Bush 2 2008/11/23 1600 Carter

leetcode笔记:Combination Sum II

一. 题目描述 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 once number of times. Note: ? All numbers (including target)

leetcode笔记:Range Sum Query 2D - Immutable

一. 题目描述 Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). The above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (

Project Euler 83:Path sum: four ways 路径和:4个方向

Path sum: four ways NOTE: This problem is a significantly more challenging version of Problem 81. In the 5 by 5 matrix below, the minimal path sum from the top left to the bottom right, by moving left, right, up, and down, is indicated in bold red an