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.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
随机推荐
- ios之UIScrollView
UIScrollView 类负责所有基于 UIKit 的滚动操作. 一.创建 [java] view plaincopy CGRect bounds = [ [ UIScreen mainSc ...
- windows下使用gcc完成头文件和目标文件编译
环境要求 安装了gcc win+r然后输入cmd , dos界面输入 gcc -v 查看有没有安装gcc 进入正题 新建 text.c文件键入如下代码: #include <stdio.h> ...
- DP入门练习
T1 题目:codevs4815江哥的dp题a codevs4815 一个简单的DP,注意开long long(不然会全WA),以及初始条件(这题有负数,所以要把f设成极小值.还要保证转移正确). # ...
- PAT 乙级 1037
题目 题目地址:PAT 乙级 1037 题解 本题有两个版本的代码,初版因为种种问题写得比较繁琐,具体的分析见后文,更新的之后的版本相对来说要好很多,代码也比较清晰简洁. 初版的代码主要有如下几方面的 ...
- 004 html常用标签
html常用标签 1.无语义标签 <div></div> <span></span> 2.常用语义标签 <hn></hn> 标题 ...
- ACM-ICPC 2018 沈阳赛区网络预赛 F. Fantastic Graph
"Oh, There is a bipartite graph.""Make it Fantastic." X wants to check whether a ...
- 快速入门Pandas
教你十分钟学会使用pandas. pandas是python数据分析的一个最重要的工具. 基本使用 # 一般以pd作为pandas的缩写 import pandas as pd # 读取文件 df = ...
- Ubuntu 16.04如何使用无线网卡上网
我使用的无线网卡卡托型号是华为E8372h,网卡是普通电信卡(既可以打电话也可以上网). 按照“芯片朝上.缺口朝外.用最大卡”的方法将网卡装入卡托后,紧接着便将卡托插入笔记本对应的USB接口中. 在这 ...
- bootstap 折叠
data-toggle="collapse" 添加到您想要展开或折叠的组件的链接上. href 或 data-target 属性添加到父组件,它的值是子组件的 id
- 基于FTP服务器搭建yum源
本例以CentOS6.8为试验对象,来搭建基于FTP服务器的yum源. 一.配置本地yum源 1.创建挂载目录/yum mkdir /yum 2.挂载镜像 mount -o loop CentOS- ...