Leetcode 1——twosum
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].
Solution:
public class Solution {
public int[] twoSum(int[] nums, int target) {
int[] result=new int[2];
for(int i=0;i<nums.length-1;i++){
for(int j=i+1;j<nums.length;j++)
{
if(target==nums[i]+nums[j]){
result[0]=i;
result[1]=j;
}
}
}
return result;
} }
一个简单的java程序,两次for循环,时间复杂度是O(n^2),空间复杂度为O(1)。
Leetcode 1——twosum的更多相关文章
- LeetCode #1 TwoSum
Description Given an array of integers, return indices of the two numbers such that they add up to a ...
- LeetCode 之 TwoSum
题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...
- leetcode之twosum
class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { vector& ...
- leetcode ----ARRAY TWOSUM
代码的(判断nums[i]或者是target-nums[i]都可以):
- [LeetCode_1] twoSum
LeetCode: 1. twoSum 题目描述 Given an array of integers, return indices of the two numbers such that the ...
- LeetCode 算法题解 js 版 (001 Two Sum)
LeetCode 算法题解 js 版 (001 Two Sum) 两数之和 https://leetcode.com/problems/two-sum/submissions/ https://lee ...
- ANDROID学习书单
Skip to content PersonalOpen sourceBusinessExplore Sign upSign in PricingBlogSupport This reposito ...
- Android技能树
第一部分:Android(安卓)Android基础知识Android内存泄漏总结Handler内存泄漏分析及解决Android性能优化ListView详解RecyclerView和ListView的异 ...
- LeetCode初体验—twoSum
今天注册了大名鼎鼎的LeetCode,做了一道最简单的算法题目: Given an array of integers, return indices of the two numbers such ...
随机推荐
- 异常-----Template user.ftl not found
freemarker 1.错误描述 java.io.FileNotFoundException: Template user.ftl not found. at freemarker.template ...
- visual studio 2010 Error: IntelliSense: identifier "DWORD" is undefined
在自己工程里,添加别的工程文件时,出现改错误 解决方法 在文件前添加: using namespace std; 参考: http://www.programgo.com/article/502412 ...
- SQL Server 扩展事件
SQL Server 扩展事件(Extended Event)是用于服务器的常规事件处理系统,是追踪SQL Server系统运行状态的神器,同时也是一个日志记录工具,扩展事件完全可以取代SQL追踪(S ...
- c++ STL容器适配器
一.标准库顺序容器适配器的种类 标准库提供了三种顺序容器适配器:queue(FIFO队列).priority_queue(优先级队列).stack(栈) 二.什么是容器适配器 &q ...
- 【Spring源码分析】非懒加载的单例Bean初始化过程(上篇)
代码入口 上文[Spring源码分析]Bean加载流程概览,比较详细地分析了Spring上下文加载的代码入口,并且在AbstractApplicationContext的refresh方法中,点出了f ...
- python数据类型:序列(字符串,元组,列表,字典)
序列通常有2个特点: 1,可以根据索引取值 2,可以切片操作 字符串,元组,列表,字典,都可以看做是序列类型 我的操作环境:Ubuntu16.04+python2.7 一.字符串类型 >按索引获 ...
- css学习の第一弹—格式创建
构成结构:选择符(又称为选择qi器){声明(属性:值):}*****注意:大括号,冒号,每个声明后的分号. 注释:/*注释内容写在这里*/ 一.css样式 css样式写的地方的不同分类:内联式.嵌入式 ...
- IIS前端页面不显示详细错误解决方法
要想解决这个问题,有三种方法可以考虑: 1.Internet信息服务(IIS)管理器 2.Web.config文件 3. 命令行 在IIS的"错误页"右边的"编辑功能设置 ...
- 403 forbidden 错误解决方案
在本机启动程序,访问手机移动端(wap)的程序时,返回404无法访问,控制台报错403 forbidden,网上找问题所在: [ 以下引用百度知道:https://zhidao.baidu.com/q ...
- 题目1023:EXCEL排序
//都是泪啊,搞了半天,竟然是成绩的数据类型搞成了string,输出测试用例的次数竟然搞成了排序的类别...细节决定成败!!! 题目描述: Excel可以对一组纪录按任意指定列排序.现请你编写程序实现 ...