LeetCode_1. Two Sum_Solution
一、题目描述

二、题目分析
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;
}
五、小结
本题主要考察循环语句的掌握和对数组的理解。
LeetCode_1. Two Sum_Solution的更多相关文章
- leetcode_1. Two Sum
leetcode_1. Two Sum 前言: 这段时间开始敲leetcode.我认为这并不仅仅只是为了应付笔试,面试.而是确实有着一定的意义. 尤其,你提交代码后,网站会多方面验证你的答案. 另外, ...
- [LeetCode_1] twoSum
LeetCode: 1. twoSum 题目描述 Given an array of integers, return indices of the two numbers such that the ...
- Java--剑指offer(10)
46.每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此.HF作为牛客的资深元老,自然也准备了一些小游戏.其中,有个游戏是这样的:首先,让小朋友们围成一个大圈.然后,他随机指定一 ...
- 剑指offer习题集1
1.打印二叉树 程序很简单,但是其中犯了一个小错误,死活找不到,写代码要注意啊 这里左右子树,要注意是node->left,结果写成root->left vector<int> ...
- 剑指offer题目41-50
面试题41:和为S的连续正整数序列 import java.util.ArrayList; public class Solution { public ArrayList<ArrayList& ...
- 求1+2+3+...+n
求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 卧槽,剑指Offer竟然有这样的题... public ...
- 求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 ...
- 剑指offer 练习题目
#include <iostream> #include<vector> #include <stack> #include<map> #include ...
- 剑指Offer-求1+2+3+...+n
package Other; /** * 求1+2+3+...+n * 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句( ...
随机推荐
- 洛谷.2754.星际转移问题(最大流Dinic 分层)
题目链接 枚举时间 每一个时间点 对于每个之前的位置像当前位置连边,表示这一时刻可待在原地 每艘船 之前时刻位置向当前时刻连边 注意别漏了0时刻src连向earth的边 #include<cst ...
- 2016年3月11日Android实习日记
1.明天删除orthodotics_design_animation_content_gif.gif文件.(已完成) 2. 如何检测内存泄露? A: 可以通过一些性能监测分析工具,如 JProfile ...
- java中哪些数值不能被初始化
main方法中的变量不能被初始化 final修饰的变量不能被初始化·
- python 爬虫不停换代理
内网看到的一个小技巧,卧槽,感觉真TM厉害 函数均放到main函数内即可 def get_proxy(): url="http://www.xicidaili.com" req=u ...
- 喵哈哈村的魔法考试 Round #18 (Div.2) 题解
喵哈哈村的古怪石碑(一) 题解:暴力check一下是等比数列还是等差数列,然后输出答案即可.注意如果数据范围是1e9的话,就要快速幂了. 代码: #include <cstdio> #in ...
- GemFire 入门篇2:GemFire 8.2 在CentOS & Mac OS X的安装步骤
GemFire目前已经开源,开源项目的主页:http://geode.incubator.apache.org/,商业版本的主页:https://pivotal.io/big-data/pivotal ...
- 迭代函数:zip、enumerate,list解析
#encoding:utf-8 """ 并行迭代: zip enumerate 获取元素及下标 list解析 iter """ #zip # ...
- Spark机器学习(7):KMenas算法
KMenas算法比较简单,不详细介绍了,直接上代码. import org.apache.log4j.{Level, Logger} import org.apache.spark.{SparkCon ...
- pid 控制算法
http://blog.csdn.net/huangkangying/article/details/78129148 https://zh.wikipedia.org/wiki/PID%E6%8E% ...
- JS 判断object是否包含某个键
1. myObj.hasOwnProperty('myKey'); 2. if ('key' in myObj)