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.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
随机推荐
- CS193p Lecture 4 - Foundation, Attributed Strings
消息机制 调用一个实例(instance)的方法(method),就是向该实例的指针发送消息(message),实例收到消息后,从自身的实现(implementation)中寻找响应这条消息的方法. ...
- iOS开发遇到的坑之一: 开发遇见如下错误:Undefined symbols for architecture arm64
博客处女作,写得不好望谅解! “for architecture arm64”就是说没有支持arm64,在Build settings里architecture相关的几项需要配置正确 在最近升级coc ...
- 自写小函数处理 javascript 0.3*0.2 浮点类型相乘问题
const reg = /^([-+]?)([0-9]+)\.([0-9]*)$/; // 判断是不是浮点数 const isFloat = function(number){ return reg. ...
- link与@import导入css样式区别
XML/HTML代码<link rel="stylesheet" rev="stylesheet" href="CSS文件" type ...
- 解决php7.3 configure: error: off_t undefined
//解决该问题:php7.3 configure: error: off_t undefined; check your library configuration # 添加搜索路径到配置文件echo ...
- 七丶人生苦短,我用python【第七篇】
模块 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个 ...
- wordpress 使用jquery需要主要的问题
wordpress 使用jquery时,不能直接使用$, 而是用jQuery 代替$, 而且wordpress默认调用jquery
- 图论trainning-part-2 C. The Largest Clique
C. The Largest Clique Time Limit: 3000ms Memory Limit: 131072KB 64-bit integer IO format: %lld ...
- python类可以截获Python运算符
类可以截获Python运算符 现在,让我们来看类和模块的第三个主要差别: 运算符重载.简而言之,运算符重载就是让用类写成的对象,可截获并响应用在内置类型上的运算:加法.切片.打印和点号运算等.这只是自 ...
- Educational Codeforces Round 33 (Rated for Div. 2)
A. Chess For Three time limit per test 1 second memory limit per test 256 megabytes input standard i ...