TwoSum IN LeetCode

The Post Created(Updated) On 04/26/2020,Please note the timeliness of the article!

quetion

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

Solution

C

int* twoSum(int* nums, int numsSize, int target, int* returnSize){
    int x,y,m,n;
    int* array = (int*)malloc(2 * sizeof(int));
    while(x <= (numsSize - 2)){
        y = x + 1;
        while(y <= (numsSize - 1)){
            if((*(nums + x) + *(nums + y) ) == target ){
                m = x;
                n = y;
                break;
            }         
            y++;
        }
        x++;
    }
    array[0] = m;
    array[1] = n;
    *returnSize = 2;
    return array;
} 

I try to set up an Array and return the fist address of this Array is no ok!

C++

class Solution {
public:
    vector<int> twoSum(vector<int> &nums, int target) {
        int x,y,m,n;
        x = y = m = n = 0;
        //int num = sizeof(nums) / sizeof(int);
        //It's not working on top way!
        int num = nums.size();
        while(x <= num - 2){
            y = x + 1;
            while(y <= num - 1){
                if(nums[x] + nums[y] == target){
                    m = x;
                    n = y;
                    break;
                }
                y++;
            }
            x++;
        }
        vector<int> z(2);
        z[0] = m;
        z[1] = n;
        return z;
    }
}; 

The first time I know there is a vector in C++!

JAVA

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int x,y,m,n;
        x = y = m = n = 0;
        while(x <= nums.length - 2){
            y = x + 1;
            while(y <= nums.length - 1){                
                if(nums[x] + nums[y] == target){
                    m = x;
                    n = y;
                    break;
                }
                y++;
            }
            x++;
        }
        int[] o = {m,n};
        return o;
    }
} 

Long time not using JAVA,Forgeting how tow set up and use an Array!

python

class Solution(object):
    def twoSum(self, nums, target):
        list = []
        for index in range(len(nums)-1):
            for index2 in range(index+1,len(nums)):
                if(nums[index] + nums[index2] == target):
                    list.append(index)   
                    list.append(index2)
                    break
        return list

Try to rember the class in Python!


Copyright

Unless otherwise noted, all work on this blog is licensed under a Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0) License. Reprinted with permission from -
https://blog.emperinter.info/2020/04/26/twosum-in-leetcode


CouPon

alibaba cloud20 dollars
Vultr10 Dollars
Bandwagon HostMaybe some Coupon?
Domain | namesiloemperinter 1Dollars