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.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
随机推荐
- 一. python基础知识
第一章.变量与判断语句 1.第一个python程序 # -*- coding:utf-8 -*- # Author: Raymond print ("hello world") p ...
- ABAQUS用户子程序一览表
说明 ABAQUS用户子程序一览表 ABAQUSStandard subroutines Refence 说明 本系列文章本人基本没有原创贡献,都是在学习过程中找到的相关书籍和教程相关内容的汇总和梳理 ...
- luoguP1164 小A点菜(背包问题)
题目背景 uim神犇拿到了uoi的ra(镭牌)后,立刻拉着基友小A到了一家……餐馆,很低端的那种. uim指着墙上的价目表(太低级了没有菜单),说:“随便点”. 题目描述 不过uim由于买了一些辅(e ...
- 【meet in middle】poj1840Eqs
震惊!map的常数居然如此之大 Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43 ...
- (37)zabbix snmp类型 无需安装agent也能监控
概述 如果我们需要监控打印机.路由器.UPS等设备,肯定不能使用zabbix agentd,因为他们不能安装软件的,还好他们一般都支持SNMP协议,这样我可以使用SNMP来监控他们.如果你希望使用SN ...
- Spring,Mybatis,Springmvc框架整合项目(第一部分)
一.说在前面的话 本篇博文实现一个注册登录小项目,使用spring,mybatis,springmvc框架进行整合,我们创建的是一个maven工程,主要是方便jar包版本的管理.项目使用eclispe ...
- PAT Basic 1040
1040 有几个PAT 字符串APPAPT中包含了两个单词“PAT”,其中第一个PAT是第2位(P),第4位(A),第6位(T):第二个PAT是第3位(P),第4位(A),第6位(T). 现给定字符串 ...
- redis配置cluster分布式集群
#下载最新的redis5. wget http://download.redis.io/releases/redis-5.0.3.tar.gz .tar.gz cd redis- make make ...
- redis安装与安全设置
redis Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件 yum安装redis 1.yum安装 #前提得配置好阿里云yum源,epel源 # ...
- Html + Css 小知识点
选择器 根据选择器来对html内的内容做css修饰 样式: 找到一个元素{ 样式:值: } 找到一个元素:选择器 css都在style标签内部写 1.标签选择器: 根据标签名查找. 小丽: 2.id选 ...