`

1. Two Sum

阅读更多

1. Two Sum

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.

Example:

 

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

java实现

public class Solution {
    public int[] twoSum(int[] nums, int target) {
       List<Integer> intList=new ArrayList();
        for(int i=0;i<nums.length;i++){
            for(int j=0;j<i;j++){
                if(nums[i]+nums[j]==target){
                    intList.add(j);
                    intList.add(i);
                }
            }

        }
        Integer[] ints=intList.toArray(new Integer[intList.size()]);
        int[] intArray = new int[ints.length];
        for(int i=0; i < ints.length; i ++)
        {
            intArray[i] = ints[i].intValue();
        }
        return intArray;
        
    }
}

 

分享到:
评论

相关推荐

    C++-leetcode题解之001. Two Sum.cpp

    c++ C++_leetcode题解之001. Two Sum.cpp

    js代码-1. Two Sum

    【压缩包子文件的文件名称列表】中的`main.js`通常包含实际的程序代码,即上面提供的`twoSum`函数的实现。`README.txt`文件通常用于提供项目说明,包括如何运行代码、项目目的等信息,但在这里没有具体的内容,因此...

    程序员面试宝典LeetCode刷题手册

    1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 7. Reverse Integer 9. Palindrome Number 11. Container With Most Water 13. Roman to ...

    LeetCode 1.Two Sum

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

    Go 零基础2000题 从入门到精通

    说明 ⽬录 第⼀章 序章 关于 LeetCode 什么是 Cookbook 为什么会写这个开源书 关于书的封⾯ 关于作者 ...1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. ......

    Two Sum算法调试小demo

    在这个`TwoSum.java`文件中,我们定义了一个名为`TwoSum`的类,包含一个`twoSum`方法来解决Two Sum问题。`main`方法用于测试,给出了一个示例输入数组`nums`和目标值`target`,并打印出结果。 在调试过程中,我们...

    leetcode2sumc-Two-Sum:我的LeetCodeC解决方案:二和

    Sum. Memory Usage: 5.8 MB, less than 99.36% of C online submissions for Two Sum. #1. Two Sum Given an array of integers nums and an integer target, return indices of the two numbers such that they add...

    twoSum0.zip

    print(twoSum(nums, target)) # 输出: [0, 1] ``` 以上就是"twoSum0.py"文件可能包含的内容,它展示了如何用Python解决两数之和问题。这个程序通过利用哈希表的特性,可以在O(n)的时间复杂度内找到答案,大大提高了...

    Two Sum leetcode c++

    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)...

    leetcode1004-leetcode:leetcode

    1. Two Sum (E) 167. Two Sum II - Input array is sorted (E) 653. Two Sum IV - Input is a BST (E) -&gt; 2 26. Remove Duplicates from Sorted Array (E) 27. Remove Element (E) 31. Next Permutation (M) * -&gt; ...

    lrucacheleetcode-leetcode:leetcode

    1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 5. Longest Palindromic Substring 7. Reverse Integer 9. Palindrome Number 11. Container ...

    1 two sum_SUM_leetcodec++代码_

    Leetcode 1 two sum C++ code

    leetcode打不开-leetcode:leetcode

    Tip1: Two pointer for sorted array (#Array 1. Two Sum) Tip2: Sum[i:j] = Sum[0:j] - Sum[0:i] for continuous array (# Array 560. Subarray Sum Equals K) Tip3: Knapsack Problem (0/1, unbounded) (#DP 322. ...

    twoSum问题的核心思想.md

    twoSum问题的核心思想.md

    leetcode2sumc-leetcode:leetcode使用单元测试框架练习

    Two Sum " 生成的文件结构如下: $id. $problem_title ├── solution.hpp # implement the solution └── TEST.cpp # UNIT Testcases 现在。 编写用于测试和解决方案的代码! /* * 1. Two Sum/TEST.cpp * */ # ...

    leetcode338-LeetCode:LeetCode刷题总结

    1.Two Sum 2.Add Two Numbers 3.Longest Substring Without Repeating Characters 4.Median of Two Sorted Arrays 5.Longest Palindromic Substring (Manacher算法待完成) 6.ZigZag Conversion 7.Reverse Integer 8....

    js-leetcode题解之1-two-sum.js

    js js_leetcode题解之1-two-sum.js

    lrucacheleetcode-Algorithm:一些常见的算法的解题报告

    1.Two Sum 2.Add Two Numbers 3.Longest Substring Without Repeating Characters 4.Median of Two Sorted Arrays 7.Reverse Integer 8.String to Integer (atoi) 9.Palindrome Number 11.Container With Most Water...

    Leetcode two sum java 解法

    Leetcode two sum java 解法

Global site tag (gtag.js) - Google Analytics