原题链接

原题中文链接

一、题目描述

二、题目分析

1,常规解法

这道题目的意思是给定一个数组和一个值,要求出这个数组中两个值的和等于这个给定值target。

输出是有要求的:

  • 坐标较小的放在前面,较大的放在后面。
  • 这俩坐标不能为零。

因此我们可以用两个for循环遍历整个数组,找到这个数组中两个值的和等于这个给定值的数组下标并输出。

三、Go代码

//1_常规解法
func twoSum(nums []int, target int) []int {
var result = []int {,}
if len(nums) < {
return nil
} for i := ; i < len(nums) - ; i++ {
for j := i + ; j < len(nums); j++ {
if(nums[i] + nums[j] == target){
result[] = i
result[] = j
return result[:] //返回结果
}
}
}
return nil
}

四、C代码

int* twoSum(int* nums, int numsSize, int target) {
int *a = (int*)malloc( * sizeof(int));
for(int i = ;i < numsSize;i++){
for(int j = i + ;j < numsSize;j++){
if(nums[j] == target - nums[i]){
a[] = i;
a[] = j;
}
}
} return a;
}

五、小结

本题主要考察循环语句的掌握和对数组的理解。

Github代码链接

LeetCode_1. Two Sum_Solution的更多相关文章

  1. leetcode_1. Two Sum

    leetcode_1. Two Sum 前言: 这段时间开始敲leetcode.我认为这并不仅仅只是为了应付笔试,面试.而是确实有着一定的意义. 尤其,你提交代码后,网站会多方面验证你的答案. 另外, ...

  2. [LeetCode_1] twoSum

    LeetCode: 1. twoSum 题目描述 Given an array of integers, return indices of the two numbers such that the ...

  3. Java--剑指offer(10)

    46.每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此.HF作为牛客的资深元老,自然也准备了一些小游戏.其中,有个游戏是这样的:首先,让小朋友们围成一个大圈.然后,他随机指定一 ...

  4. 剑指offer习题集1

    1.打印二叉树 程序很简单,但是其中犯了一个小错误,死活找不到,写代码要注意啊 这里左右子树,要注意是node->left,结果写成root->left vector<int> ...

  5. 剑指offer题目41-50

    面试题41:和为S的连续正整数序列 import java.util.ArrayList; public class Solution { public ArrayList<ArrayList& ...

  6. 求1+2+3+...+n

    求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 卧槽,剑指Offer竟然有这样的题... public ...

  7. 求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)

    代码如下: public int Sum_Solution(int n) { int temp = n; boolean b = (temp>0)&&(temp += Sum_S ...

  8. 剑指offer 练习题目

    #include <iostream> #include<vector> #include <stack> #include<map> #include ...

  9. 剑指Offer-求1+2+3+...+n

    package Other; /** * 求1+2+3+...+n * 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句( ...

随机推荐

  1. jQuery File Upload 判断图片尺寸,限定图片宽高的办法

    1.必须熟读jQuery File Upload 文档,在add方法中进行判断,如果不符合条件,就用 data.abort()方法取消上传动作. $("file").fileupl ...

  2. 使用 IntraWeb (31) - IntraWeb 的 Xml 操作使用的是 NativeXml

    在 IWNativeXml 单元. 知道了这个, 以后在其他 Delphi 程序中也可以直接 Uses IWNativeXml 了. TNativeXml (IWNativeXml.TNativeXm ...

  3. 最近无意中看到一个讲解spring mvc的系列,从源码的角度讲解,特记录下来,供以后反复学习

    SpringMVC深度探险(一) —— SpringMVC前传 SpringMVC深度探险(二) —— SpringMVC概览 SpringMVC深度探险(三) —— DispatcherServle ...

  4. 《es6标准入门》chapter11中关于Proxy的一个错误例子的纠正

    在原书第二版的p120,这里有一个使用Proxy实现管道化调用的例子,想法很好,但是代码有问题,下面是更正之后的代码. 由于我是在node环境下运行,所以我把几个全局函数定义到global内了,如果是 ...

  5. [Canvas]Bombman v1.00

    爆破小人Canvas版,请点此下载,并用浏览器打开试玩. 图例: 源码: <!DOCTYPE html> <html lang="utf-8"> <m ...

  6. 【Mac使用系列】【转载】十几个Mac实用工具

    本文摘自:https://www.jianshu.com/p/15c7b3711005 经过验证,这几个不存在: CleanMyMac.OmniGraffle,我将可用的放在云盘里,有需要的话,可以从 ...

  7. C# System.Threading.ReaderWriterLockSlim

    using System; using System.Threading; using System.Threading.Tasks; using System.Collections.Generic ...

  8. Java 基础【17】 异常与自定义异常

    1.异常的分类 Throwable 是所有异常类的基类,它包括两个子类:Exception 和 Error. a. 错误 (Error) 错误是无法难通过程序来解决的,所以程序不应该抛出这种类型的对象 ...

  9. OpenCV学习代码记录——人脸检测

    很久之前学习过一段时间的OpenCV,当时没有做什么笔记,但是代码都还在,这里把它贴出来做个记录. 代码放在码云上,地址在这里https://gitee.com/solym/OpenCVTest/tr ...

  10. zabbix监控k8s出现的pod error status

    配置zabbix客户端配置文件 vim /etc/zabbix/zabbix_agentd.conf 添加  Include=/etc/zabbix/zabbix_agentd.d/ #!/bin/b ...