原题链接

原题中文链接

一、题目描述

二、题目分析

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. BZOJ.2639.矩形计算(二维莫队)

    题目链接 二维莫队,按x,y坐标一起分块.(x,y)的所属的块为 x/sq(n)*sq(m) + y/sq(m) 排序时按照(左下点所在块,右上点的标号)排序 排序后 先得出一个询问的答案,然后利用上 ...

  2. 【转载】实用VC++6.0插件

    [转自]http://www.cnblogs.com/witxjp/archive/2011/04/03/2004556.html Visual Assist(强烈推荐)网址:http://www.w ...

  3. .Net Core URL编码和解码

    一.URL说明 .Net Core中http 的常用操作封装在 HttpUtility 中 命名空间 using System.Web; // // 摘要: // Provides methods f ...

  4. SpringBoot日志logback-spring.xml分环境log4j logback slf4j区别 springboot日志设置

    转载. https://blog.csdn.net/qianyiyiding/article/details/76565810 springboot按照profile进行打印日志log4j logba ...

  5. 移动电源、3G路由拆机

    这款电源4400mAh,淘宝也就八十元左右,可以作为无线路由使用,可以插3G网卡,总的来说还算不错,关键是外观精美,网上一堆和华美A100那样的,可惜实在太丑,这款外观虽然漂亮,但是和一百多的相比不支 ...

  6. 奇怪吸引子---NewtonLeipnik

    奇怪吸引子是混沌学的重要组成理论,用于演化过程的终极状态,具有如下特征:终极性.稳定性.吸引性.吸引子是一个数学概念,描写运动的收敛类型.它是指这样的一个集合,当时间趋于无穷大时,在任何一个有界集上出 ...

  7. How to measure IOPS for VMware

    http://blog.synology.com/blog/?p=2225 Executive SummaryThis article, intended towards IT Professiona ...

  8. Spark GraphX实例(3)

    7. 图的聚合操作 图的聚合操作主要的方法有: (1) Graph.mapReduceTriplets():该方法有一个mapFunc和一个reduceFunc,mapFunc对图中的每一个EdgeT ...

  9. void java.lang.System.gc()

    void java.lang.System.gc() Runs the garbage collector. Calling the gc method suggests that the Java ...

  10. 移除list中null元素

    查询结果为null, list.size()却是1 移除该null元素 totalList.removeAll(Collections.singleton(null));