1. Two Sum两数之和(Easy,LeetCode)

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target  的那 两个 整数,并返回它们的数组下标。

You may assume that each input would have exactly one solution, and you may not use the same element twice.

你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。

You can return the answer in any order.

你可以按任意顺序返回答案。

Example 1:

示例 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。

Example 2:

示例 2:

Input: nums = [3,2,4], target = 6
Output: [1,2]

Example 3:

示例 3:

Input: nums = [3,3], target = 6
Output: [0,1]

Constraints:

  • 2 <= nums.length <= 104
  • -109 <= nums[i] <= 109
  • -109 <= target <= 109
  • Only one valid answer exists.

提示:

  • 2 <= nums.length <= 104
  • -109 <= nums[i] <= 109
  • -109 <= target <= 109
  • 只会存在一个有效答案

Follow-up: Can you come up with an algorithm that is less than O(n^{^{2}}) time complexity?

进阶:你可以想出一个时间复杂度小于 O(n2) 的算法吗?

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/two-sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

C#

public class Solution {
    public int[] TwoSum(int[] nums, int target) {
        for(int i=0;i<nums.Length-1;i++)
        {
            for(int j=i+1;j<nums.Length;j++)
            {
                if(nums[i]+nums[j]==target)
                {
                    return new int[] {i,j};
                }                    
            }
        }
        return new int[] {0,0};
    }
}

执行结果:通过

执行用时:200 ms, 在所有 C# 提交中击败了9.54%的用户

内存消耗:43.1 MB, 在所有 C# 提交中击败了58.42%的用户

通过测试用例:57 / 57

我的C语言答案:(未做pfqw)

//
//  main.c
//  TwoSum
//
//  Created by douxiaobo on 2022/1/23.
//

#include <stdio.h>

int num[104];
int target;
int length;
int a,b;

int input(){
    length=0;
    int temp;
    printf("num=");
    do{
        scanf("%d",&temp);
        num[length]=temp;
        length++;
        if(temp==110)
            break;
    }while(length>=2&&length<=104);
    printf("Target:");
    do{
        scanf("%d",&target);
        if(target>=-109&&target<=109)
            break;
    }while(1);
    return 0;
}
int  TwoSum(){
    int i,j;
    for(i=0;i++;i<length-1){
        for(j=i+1;j++;j<length){
            if(target==(num[i]+num[j])){
                a=num[i];
                b=num[j];
            }else{
                a=110;b=110;
            }
        }
    }
    return 0;
}
int output(){
    if(a==110&&b==110){
        printf("No\n");
    }else{
        printf("%d %d\n",a,b);
    }
    return 0;
}
int main(int argc, const char * argv[]) {
    // insert code here...
    input();
    TwoSum();
    output();
    return 0;
}

我的Go语言的答案:

package main

import (
	"fmt"
)

const Num_Length_Min = 2
const Num_Length_Max = 104
const Min = -109
const Max = 109

var Num = make([]int, 104)
var Target int
var Nums int = 0
var a, b int

func Num_Length(length int) bool {
	if length < Min {
		return false
	} else if length > Max {
		return false
	} else {
		return true
	}
}

func Input() {
	var temp int
	fmt.Println("Please input your nums(-109<=num<=109):")
	for {
		fmt.Scanln(&temp)
		if (temp == 1111) && (Nums >= 2) {
			fmt.Println("Thanks for finishing input nums!")
			break
		} else if Num_Length(temp) == false {
			fmt.Println("Sorry,Please input your num(-109<=num<=109) again:")
		} else {
			Num[Nums] = temp
			fmt.Println("Thanks, please input your nums(-109<=num<=109) again:")
			Nums++
		}
	}
	fmt.Println("Please input the Target(-109<=target<=109):")
	for {
		fmt.Scanln(&Target)
		if (Target < Min) && (Target > Max) {
			fmt.Println("Plaege input the Taget again:")
		} else {
			break
		}
	}

}
func TwoSum() {
	for i := 0; i <= Nums-1; i++ {
		for j := i + 1; j <= Nums; j++ {
			if (Num[i] + Num[j]) == Target {
				a = Num[i]
				b = Num[j]
				break
			}
		}
	}

}
func Output() {
	fmt.Printf("OutPut:%d and %d\n", a, b)
}

func main() {
	Input()
	TwoSum()
	Output()
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值