Leetcode Array 1 twoSum

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

C++:
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> ans;
        for(int i = 0;i < nums.size()-1;i++){
            for(int j = i+1;j<nums.size();j++){
                if(nums[i]+nums[j] == target){
                    ans.push_back(i);
                    ans.push_back(j);
                    break;
                }
            }
        }
        return ans;
    }
};

Java:

 1 public class Solution {
 2     public int[] twoSum(int[] nums, int target) {
 3         int []a = new int[2];
 4         for(int i =0;i<nums.length-1;i++){
 5             for(int j=i+1;j<=nums.length-1;j++){
 6                 if(nums[i]+nums[j] == target){
 7                     a[0] = i;
 8                     a[1] = j;
 9                     break;
10                 }
11             }
12         }
13         return a;
14     }
15 }

附加一下c++ vector 的简单用法:

1.push_back   在数组的最后添加一个数据

2.pop_back    去掉数组的最后一个数据

3.at                得到编号位置的数据

4.begin           得到数组头的指针

5.end             得到数组的最后一个单元+1的指针

6.front        得到数组头的引用

7.back            得到数组的最后一个单元的引用

8.max_size     得到vector最大可以是多大

9.capacity       当前vector分配的大小

10.size           当前使用数据的大小

11.resize         改变当前使用数据的大小,如果它比当前使用的大,者填充默认值

12.reserve      改变当前vecotr所分配空间的大小

13.erase         删除指针指向的数据项

14.clear          清空当前的vector

15.rbegin        将vector反转后的开始指针返回(其实就是原来的end-1)

16.rend          将vector反转构的结束指针返回(其实就是原来的begin-1)

17.empty        判断vector是否为空

18.swap         与另一个vector交换数据

 
时间: 2024-10-18 08:19:57

Leetcode Array 1 twoSum的相关文章

LeetCode[Array]: Rotate Image

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? 参考LeetCode[Array]: Spiral Matrix II的迭代思路,先完成最外环的旋转,然后依次旋转内环.我的C++代码如下: void rotate(vector<vector<int> >

LeetCode[Array]: Pascal&#39;s Triangle II

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 这个问题比较简单. vector<int> getRow(int rowIndex) { vector<int> row, prev

LeetCode[Array]: Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 这个题目跟LeetCode[Array]: Spiral Matrix不同的是:这个题目并不

Leetcode Array 4 Median of Two Sorted Arrays

做leetcode题目的第二天,我是按照分类来做的,做的第一类是Array类,碰见的第二道题目,也就是今天做的这个,题目难度为hard.题目不难理解,但是要求到了时间复杂度,就需要好好考虑使用一下算法了.刚开始没啥思路,就用暴力的方法,用双层循环遍历的一下两个已经排好序的数组 ,在中间位置停止找道中位数.这样时间复杂度是肯定不能满足题目要求的,但是程序测试还是过了. 苦于自己没有思路,又不甘心就这样水过一道题,还是搜了一下博客,膜拜了一下大神.最好的方法是将中位数  -- 两个数组数据排好序之后

LeetCode[Array]: Trapping Rain Water

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation map is represented by a

【LeetCode】001 TwoSum

题目: 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,

[LeetCode]Array主题系列

1.内容介绍 开一篇文章记录在leetcode中array主题下面的题目和自己的思考以及优化过程,具体内容层次按照{题目,分析,初解,初解结果,优化解,优化解结果,想法}的格式来记录,供日后复习和反思.题目的顺序按照leetcode给出的题目顺序,有些题目在并不是按照题目本身序号顺序排列的,也不是严格按照难易程度来排列的. 因此,这篇文章并不具有很强的归类总结性,归类总结性知识将会在其他文章记录,本篇重点在记录解题过程中的思路,希望能对自己有所启发. 2.题目和解题过程 2.1 Containe

LeetCode # Array # Easy # 167. Two Sum II - Input array is sorted

Given an array of integers that is already sorted in ascending order, 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 m

Leetcode Array 16 3Sum Closest

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2