leetcode题1Two sum 练习
给一个整数数组, 返回数组中的两数之和等于指定值的两数在数组中的下标.
Example:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
public int[] TwoSum(int[] nums, int target) {
int[] result=new int[];//创建一个返回数组;
for (int j = ; j < nums.Length; j++)//{1,4,5,7,9,10};//{9,6,5,3,1,0}外层循环,传进的数组赋值
{
for (int k = j+; k < nums.Length; k++)//创建内层循环,内层循环主要是让temp[i]=target-nums[i];让temp[i]的值与数组nums[i]比自己大的元素比较看是否相等。
{
if (target - nums[j] == nums[k])
{
result[] = j;//相等就记录该位置。
result[] = k;//记录相等的位置。
}
}
}
return result;
}
附上该例源码:
namespace ConsoleApplication1
{
class addtwonum
{
public static void Main(string[] args)
{
int[] num = { , , , , , };//{9,6,5,3,1,0}
int[] result = getnum(num, );
for (int i = ; i < result.Length; i++)
{
Console.WriteLine(result[i]);
}
Console.ReadLine();
}
//
//{1,2,3,4,6} tagart =5
//a[0]+a[3] ,a[1]+a[2] [0,3],[1,2]
private static int[] getnum(int []nums,int tagart)
{
int[] result=new int[];
int[] temp=new int[nums.Length];
//for (int i = 0; i < nums.Length; i++)
// temp[i] = tagart - nums[i];
for (int j = ; j < nums.Length; j++)//{2, 4, 4, 7, 9, 10};//{6,4,4,-1,-2,-3}|{7,5,5,2,0,-1}
{
for (int k = j+; k < nums.Length; k++)
{
//result[0] = j;
//result[1] = k;
if (tagart - nums[j] == nums[k])
{
//j = nums.Length - 1;
//k = nums.Length;
result[] = j;
result[] = k;
} }
} return result;
} }
}
leetcode题1Two sum 练习的更多相关文章
- LeetCode算法题-Two Sum II - Input array is sorted
这是悦乐书的第179次更新,第181篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第38题(顺位题号是167).给定已按升序排序的整数数组,找到两个数字,使它们相加到特定 ...
- 乘风破浪:LeetCode真题_040_Combination Sum II
乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum ...
- 乘风破浪:LeetCode真题_039_Combination Sum
乘风破浪:LeetCode真题_039_Combination Sum 一.前言 这一道题又是集合上面的问题,可以重复使用数字,来求得几个数之和等于目标. 二.Combination Sum ...
- 1-Two Sum @LeetCode
1-Two Sum 题目 思路 题目中得到的信息有: 都是整数,并且可正可负,也可一个值包含多个: 只有一个正确的结果. 方法一: 最直接的思路就是两重循环遍历,时间复杂度是O(n^2),这样肯定不行 ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- [LeetCode] #167# Two Sum II : 数组/二分查找/双指针
一. 题目 1. Two Sum II Given an array of integers that is already sorted in ascending order, find two n ...
- [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针
一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...
- [LeetCode] Design Excel Sum Formula 设计Excel表格求和公式
Your task is to design the basic function of Excel and implement the function of sum formula. Specif ...
- LeetCode one Two Sum
LeetCode one Two Sum (JAVA) 简介:给定一个数组和目标值,寻找数组中符合求和条件的两个数. 问题详解: 给定一个数据类型为int的数组,一个数据类型为int的目标值targe ...
随机推荐
- plupload上传控件错误exec(this.uid, component, action, args)
plupload上传控件错误exec(this.uid, component, action, args) --undefined is not a function 原因:Flash元素隐藏后调用控 ...
- UVALive 4223 Trucking 二分+spfa
Trucking 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8& ...
- cdoj 1141 酱神寻宝 状压dp
酱神寻宝 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1141 Descri ...
- cdoj 题目简单分类
如有错误请联系我,下面题的题解,除了傻逼题均可以在我blog中查找 1 傻逼题 3 傻逼题 4 傻逼题 15 dfs 24 傻逼题 25傻逼题 26 傻逼题 30 flyod 31 01背包 42 k ...
- C# 图片裁剪代码
/// <summary> /// 缩小裁剪图片 /// </summary> /// <param name="int_Width">要缩小裁 ...
- SIP入门(二):建立SIPserver
在我的上一篇文章中已经介绍怎样通过SIP软电话直接通话,可是假设须要支持很多其它用户互相通话,同一时候基于安全考虑,须要对用户帐户登录进行验证控制,这些情况下就须要建立SIPserver. SIPse ...
- [Ramda] Filter, Reject and Partition
We'll learn how to get a subset of an array by specifying items to include with filter, or items to ...
- 把你的旧笔记本变成 Chromebook
导读 Linux 之年就在眼前.根据报道,Google 在 2016 年第一季度卖出了比苹果卖出的 Macbook 更多的 Chromebook.并且,Chromebook 即将变得更加激动人心.在 ...
- Laravel自学第一课:laravel下载与安装
本地安装laravel,php环境要配置好,推荐xmapp一键搭建. 1.程序包直接从官方下载,官方开源地址:https://github.com/laravel/laravel(当然也可从此网站:h ...
- web开发人员须知的web缓存知识–将数据缓存到浏览器端Net实现
现实中,服务器在向浏览器发送的数据中,一部分数据是不经常更新的,如果能将这部分数据缓存到浏览器端,将会大大降低传输的数据,提高应用的性能.通过Expires策略,可以使用HTTP 协议定义的缓存机制将 ...