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 ...
随机推荐
- javascript优化
javaScript是一门解释性的语言.它不像java.C#等程序设计语言.由编译器先进行编译再运行.而是直接下载到用户的客户端进行执行.因此代码本身的优劣就直接决定了代码下载的速度以及执行的效率. ...
- js正則表達式语法
1. 正則表達式规则 1.1 普通字符 字母.数字.汉字.下划线.以及后边章节中没有特殊定义的标点符号,都是"普通字符".表达式中的普通字符,在匹配一个字符串的时候,匹配与之同样的 ...
- Quartz中Cron表达式使用方法
Quartz中CronTrigger支持日历相关的反复时间间隔(比方每月第一个周一运行),而不是简单的周期时间间隔. 它的调度规则基于 Cron 表达式. 以下就来说一下Cron表达式的规则及使用方法 ...
- IOS之以UIBezierPath绘制饼状图
1.绘制的饼状图是通过多个扇形拼和而成,绘制一个扇形也是比较简单的,核心代码如下: 先画一条圆弧,再画半径,接着再画一条圆弧,最后闭合路径: UIBezierPath* aPath = [[UIBe ...
- alue of type java.lang.String cannot be converted to JSONObject
/** * 4.0以下系统处理掉返回json的BOM头 * * @param jsonStr * @return */ public static String getJson(String json ...
- iOS开发——实用技术OC篇&8行代码教你搞定导航控制器全屏滑动返回效果
8行代码教你搞定导航控制器全屏滑动返回效果 前言 如果自定了导航控制器的自控制器的leftBarButtonItem,可能会引发边缘滑动pop效果的失灵,是由于 self.interactivePop ...
- 《UCD火花集1-2》读后感
一个多月的时间都没有更新博客了,只因为9月份有了其他的任务,耽搁了一 ...
- Network Link Conditioner模拟不同网络环境
在Xcode4.1中有一个工具叫Network Link Conditioner,可以让用户模拟不同的网络连接和带宽,可供Mac和iOS开发者测试自己的程序在不同网络环境下的表现. 在Xcode4.3 ...
- mybatis 关于 Parameter Maps collection does not contain value for
当*mapper.XML 文件中出现任何错误,该xml文件都不能使用.也就是说不管出错的那个标签是不是你当前调用的,都会报错误 .Parameter Maps collection does not ...
- C. Ilya and Sticks
C. Ilya and Sticks time limit per test 2 seconds memory limit per test 256 megabytes input standard ...