LeetCode(62)-Two Sum
题目:
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
UPDATE (2016/2/13):
The return format had been changed to zero-based indices. Please read the above updated description carefully.
思路:
- 题意:给定一个互不相同的整数数组nums,给定一个目标值target,求两个索引值,相应的值的和等于target
- 首先排除nums == null和nums.length < 2,返回null,至于其他的情况可以用两个循环解决
代码:
public class Solution {
public int[] twoSum(int[] nums, int target) {
int[] result = new int[2];
int n = nums.length;
if(n < 2 || nums == null){
return null;
}
for(int i = 0;i < n;i++){
for(int j = i+1;j < n;j++){
if(nums[i] + nums[j] == target){
result[0] = i;
result[1] = j;
return result;
}
}
}
return null;
}
}
LeetCode(62)-Two Sum的更多相关文章
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- [leetCode][013] Two Sum 2
题目: Given an array of integers that is already sorted in ascending order, find two numbers such that ...
- [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 ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- LeetCode one Two Sum
LeetCode one Two Sum (JAVA) 简介:给定一个数组和目标值,寻找数组中符合求和条件的两个数. 问题详解: 给定一个数据类型为int的数组,一个数据类型为int的目标值targe ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
随机推荐
- FORM触发器
FORM级触发器 PRE-FORM该触发器是在用户双击功能后,进入form前 WHEN-NEW-FORM-INSTANCE该触发器是在用户一进入form时执行 WHEN-FORM-NAVIGAT ...
- Dynamics CRM 安装Microsoft Dynamics CRM Reporting Extensions
在装完CRM Server 后这个组件是必须安装的,但今天由于我的大意在客户安装生产环境时,告诉客户这个组件装在APP服务器上,导致客户安装时 SSRSInstance怎么都是空的,害的人家找了半天原 ...
- 直接内存访问(DMA)
1. 什么是DMA 直接内存访问是一种硬件机制,它允许外围设备和主内存之间直接传输它们的I/O数据,而不需要系统处理器的参与.使用这种机制可以大大提高与设备通信的吞吐量. 2. DMA数据传输 有 ...
- 显示图像的SIFT flow描述子
close all; % 模拟figure 5 im = zeros(401,401,3); im(:,:,:) = 0; im(2:200, 2:200, 2) = 255; im(202:400, ...
- MySQL设计软件登录模块
学了一段时间的Java了,思量着做一点简单的小模块的东西吧,于是就有了下面的这个简单的小案例. 大致实现的功能就是注册于登录还有就是用到了一点,分层思想.仅此而已,所以非常的适合新手围观. 建立好数据 ...
- [java面试]javascript中dom取值问题radio名字一样归属于同一个组,求点击的是哪一个
题目描述: 看如下的html文件,里面定义了一些radio类型的元素,请完成parse()函数的内容,要求能够弹出对话框提示当前选中的是第几个单选框. </pre><pre code ...
- 网页搜索之后的APP搜索
搜索技术是互联网最核心的技术之一.但是移动互联网迅猛发展的今天,互联网产生的数据已经不是简单的网页搜索所能涵盖的了.比如微信公众号,产生了许多优质的内容,如果这些公众号仅仅将这些内容发布到微信平台,那 ...
- quartz 时间设置(定时任务scheduler)
quartz用来设置定时任务的作业调度程序.在linux的crontab中用到. 格式为: * * * * * * * 其从左到右顺序代表 :[秒] [分] [小时] [日] [月] [周] [年] ...
- (NO.00004)iOS实现打砖块游戏(十四):3球道具的实现
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 反弹棒变化道具实现前面已经介绍过了,我们下面可以在小球上做些文章 ...
- Android 文件操作心得体会
android 的文件操作说白了就是Java的文件操作的处理.所以如果对Java的io文件操作比较熟悉的话,android的文件操作就是小菜一碟了.好了,话不多说,开始今天的正题吧. 先从一个小项目入 ...