LeetCode第一题以及时间复杂度的计算
问题描述:给定一组指定整数数组,找出数组中加和等于特定数的两个数。
函数(方法)twoSum返回这两个数的索引,index1必须小于index2。
另外:你可以假设一个数组只有一组解。
一个栗子:
Input: numbers={2, 7, 11, 15}, target=9 Output: index1=1, index2=2
算法实现如下:
/**
* 时间复杂度O(n)
* @param array
* @param target
* @return Map<Integer,Integer>
*/
public static Map<Integer, Integer> twoSum(int[] array, int target) { //Map<value,index>
Map<Integer, Integer> result = new HashMap<Integer, Integer>(); Map<Integer, Integer> container = new HashMap<Integer, Integer>();
for (int i = 0; i < array.length; i++) {
if (!container.containsKey(target - array[i])) {
container.put(array[i], i + 1);
} else {
result.put(target - array[i], container.get(target - array[i]));
result.put(array[i], i + 1);
break ;
}
} return result;
}
另有双层循环判断的算法实现,时间复杂度为O(n²),在此就不列出。
关于时间复杂度的计算
一个栗子:
int value = 0 ; //该代码执行1次
for(int i = 0 ; i < n ; i++){
value += n ; //该代码执行n次
}
该算法执行1+n次,如果n趋近于无穷大,1便可忽略不计,也就是说该算法执行了n次。时间复杂度常用O符号表示,这个算法的时间复杂度为O(n)。
当一个算法的计算时间复杂度时,可以遵循这样的规则:
1).去掉运行时间中的所有加法常数。
2).只保留最高阶项。
3).如果最高阶项存在且不是1,去掉与这个最高阶相乘的常数得到时间复杂度
再一个栗子
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
// do something
}
}
当 i = 0 时 里面的fo循环执行了n次,当i等待1时里面的for循环执行了n - 1次,当i 等于2里里面的fro执行了n - 2次........所以执行的次数是:
n + (n-1) + (n-2) + ...+ 1
= n(n+1)/2
= n²/2 + n/2
根据我们上边的时间复杂度算法
1.去掉运行时间中的所有加法常数: 没有加法常数不用考虑
2.只保留最高阶项: 只保留 n²/2
3. 去掉与这个最高阶相乘的常数: 去掉 1/2 只剩下 n²
最终这个算法的时间复杂度为O(n²)
LeetCode第一题以及时间复杂度的计算的更多相关文章
- leetcode第一题(easy)
第一题:题目内容 Given an array of integers, return indices of the two numbers such that they add up to a sp ...
- 有人相爱,有人夜里开车看海,有人leetcode第一题都做不出来。
第一题 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数 ...
- LeetCode第一题:Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- leetcode第一题--two sum
Problem:Given an array of integers, find two numbers such that they add up to a specific target numb ...
- leetcode第一题两数之和击败了 98.11% 的用户的答案(C++)
虽然题目简单,但我这好不容易优化到前2%,感觉也值得分享给大家(方法比较偷机) 题目: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们 ...
- leetcode 第一题 Two Num java
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- LeetCode第一题—— Two Sum(寻找两数,要求和为target)
题目描述: Given an array of integers, return indices of the two numbers such that they add up to a speci ...
- LeetCode 第一题 两数之和
题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组 ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
随机推荐
- Bootstrap历练实例:链接样式按钮
<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...
- 【二分 贪心】bzoj3477: [Usaco2014 Mar]Sabotage
科学二分姿势 Description Farmer John's arch-nemesis, Farmer Paul, has decided to sabotage Farmer John's mi ...
- [LUOGU] P2679 子串
一开始用一个f数组转移,发现不太对,状态有重叠部分 f[i][j][k]表示考虑了s的前i位,匹配到t的第j位,用了k个子串,且s的第i位必选 g[i][j][k]表示考虑了s的前i位,匹配到t的第j ...
- verilog behavioral modeling --loop statement
1.forever 2.repeat 3.while 4.for The for statement accomplishes the same results as the following ps ...
- linux下ls出现文件的后缀有@,* ,/之类的解释
ls -Fafptool* img_maker* lzcmp@ lzfgrep@ lzma* lzmore* node-pre-gyp@bower@ ...
- grep理解
http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856896.html部分摘录于此 grep与正规表达式 字符类 字符类的搜索:如果我想要搜 ...
- 【MySQL】浅谈 varchar(N)
一 前言 varchar(N) N代表什么意思,能存放多少个中文字符?属于老生常谈的问题了,今天又被一个开发同事问我关于这个问题,索性写一篇文章来具体介绍一下.二 理论知识 先说明一下 MySQL ...
- 交换机VLAN的定义、意义以及划分方式
什么是VLAN 虚拟网技术(VLAN,Virtual Local Area Network)的诞生主要源于广播.广播在网络中起着非常重要的作用,如发现新设备.调整网络路径.IP地址租赁等等,许多网络协 ...
- Configure Always On Availability Group for SQL Server on Ubuntu
下面简单介绍一下如何在Ubuntu上一步一步创建一个SQL Server AG(Always On Availability Group),以及配置过程中遇到的坑的填充方法. 目前在Linux上可以搭 ...
- oracle 安装 启动listener 建库相关
安装 参考 http://www.cnblogs.com/gaojun/archive/2012/11/22/2783257.html 几个问题: 1. 用户删除问题 p001:~ # userdel ...