当前位置:网站首页>1. Sum of two numbers
1. Sum of two numbers
2022-07-26 14:48:00 【superkcl2022】


1. Violence solution
/** * describe Violence solution O(n^2) */
class Solution {
public int[] twoSum(int[] nums, int target) {
for(int i = 0; i < nums.length; i++){
for(int j = i+1; j < nums.length; j++){
if(nums[i] + nums[j] == target){
return new int[]{
i,j};
}
}
}
return new int[]{
};
}
public static void main(String[] args) {
Solution solution = new Solution();
int[] ints = solution.twoSum(new int[]{
2, 7, 11, 15}, 9);
System.out.println(ints[0] + " " + ints[1]);
}
}
2. Hashtable
/** * Project name : leetcode * Package name : com.kcl.L1 * file name Solution * The creator * Creation time : 2022/7/10 10:49 AM * describe hash O(n) */
class Solution2 {
public int[] twoSum(int[] nums, int target) {
Map<Integer,Integer> map = new HashMap<>();
int len = nums.length;
for(int i = 0; i < len; i++){
// Look forward to
if(map.get(target-nums[i]) != null){
return new int[]{
i,map.get(target-nums[i])};
}else{
map.put(nums[i],i);
}
}
return new int[]{
};
}
public static void main(String[] args) {
Solution2 solution = new Solution2();
int[] ints = solution.twoSum(new int[]{
2, 7, 11, 15}, 9);
System.out.println(ints[0] + " " + ints[1]);
}
}
边栏推荐
- Mysql-04 storage engine and data type
- PyTorch中 nn.Conv2d与nn.ConvTranspose2d函数的用法
- C common function integration
- When AI encounters life and health, Huawei cloud builds three bridges for them
- VP视频结构化框架
- SiamRPN++:深层网络连体视觉跟踪的演变
- GOM login configuration free version generate graphic tutorial
- sqlDeveloper工具快速入门
- Some lightweight network models in detection and segmentation (share your own learning notes)
- [Yugong series] July 2022 go teaching course 017 - if of branch structure
猜你喜欢

【愚公系列】2022年7月 Go教学课程 017-分支结构之IF

Canvas mesh wave animation JS special effect

JS wave animation effect menu style

SA-Siam:用于实时目标跟踪的孪生网络

中部“第一城”,长沙“人才引力”从争先到领先

Simple implementation of pytorch

Unity learning notes – infinite map

【2022国赛模拟】白楼剑——SAM、回滚莫队、二次离线
![[Yugong series] July 2022 go teaching course 017 - if of branch structure](/img/e4/b3aa4b8bda738aadded3127a8b3485.png)
[Yugong series] July 2022 go teaching course 017 - if of branch structure

Siamrpn++: evolution of deep network connected visual tracking
随机推荐
UE4 smart pointer and weak pointer
Unity学习笔记–无限地图
C # use shift > > and operation and & to judge whether the two binary numbers have changed
Mysql-03 database operation
Would you please tell me if there is a way for Flink SQL not to output update_ before?
Fill in the questionnaire and receive the prize | we sincerely invite you to fill in the Google play academy activity survey questionnaire
智能家居行业发展,密切关注边缘计算和小程序容器技术
Matlab solution of [analysis of variance]
RPN: region proposal networks
【1.2.投资的收益和风险】
Introduction to C language must brush the daily question of the collection of 100 questions (1-20)
[file upload vulnerability-06] distributed configuration file attack experiment - take upload-labs-4 as an example
Canvas mesh wave animation JS special effect
sp导出贴图到maya
When AI encounters life and health, Huawei cloud builds three bridges for them
Win11 running virtual machine crashed? Solution to crash of VMware virtual machine running in win11
Canvas laser JS special effect code
Winscp transfer file and VNC connection problem
我的创作纪念日-从心出发
TDengine 助力西门子轻量级数字化解决方案 SIMICAS 简化数据处理流程