Two Sum

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have *exactly* one solution, and you may not use the same element twice.

You can return the answer in any order.

Example 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

题解

  • 双 for 循环
  • for 循环 + HashMap,利用 containsKey 来处理 target - x 带来的一次 for 循环

Java实现,LC里面暴力双 for 执行用时更短。

package LC.hash;

import java.util.HashMap;

public class LC1 {
public static void main(String[] args) {
int[] nums = {2, 7, 11, 15};
int target = 9;
int[] res = twoSum(nums, target);
for (int item :
res) {
System.out.println(item);
}
} public static int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> integerHashMap = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (integerHashMap.containsKey(target - nums[i])) {
return new int[]{integerHashMap.get(target - nums[i]), i};
}
integerHashMap.put(nums[i], i);
}
return new int[0];
}
}
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] result = new int[2];
for(int i = 0; i < nums.length; i++){
for(int j = nums.length - 1; j > i; j--){
if(nums[i] + nums[j] == target){
result[0] = i;
result[1] = j;
return result;
}
}
}
return result;
}
}

Python实现

from typing import List

class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
hashtable = dict()
for i, num in enumerate(nums):
if target - num in hashtable:
return [hashtable[target - num], i]
hashtable[nums[i]] = i
return [] test_nums = [2, 7, 11, 15]
teat_target = 9
res = Solution()
print(res.twoSum(test_nums, teat_target)) # class C(object):
# @staticmethod
# def f():
# print('runoob');
#
#
# C.f(); # 静态方法无需实例化
# cobj = C()
# cobj.f() # 也可以实例化后调用

LC-1的更多相关文章

  1. 四种比较简单的图像显著性区域特征提取方法原理及实现-----> AC/HC/LC/FT。

    laviewpbt  2014.8.4 编辑 Email:laviewpbt@sina.com   QQ:33184777 最近闲来蛋痛,看了一些显著性检测的文章,只是简单的看看,并没有深入的研究,以 ...

  2. “LC.exe”错误

    错误“LC.exe”已退出,代码为 -1. 可能的原因是: 这个第三方组件是个商业组件,他在组件的主使用类定义了 LicenseProvider(typeof(LicFileLicenseProvid ...

  3. 解决VS下“LC.exe已退出,代码为-1”问题

    今天使用VS2015开发一个Winform程序,手一抖拖错了一个第三方控件,然后将其去掉并删除相关的引用,结果导致了LC.exe错误:"Lc.exe已退出,代码为-1 ". 经过上 ...

  4. 解析.NET 许可证编译器 (Lc.exe) 的原理与源代码剖析

    许可证编译器 (Lc.exe) 的作用是读取包含授权信息的文本文件,并产生一个可作为资源嵌入到公用语言运行库可执行文件中的 .licenses 文件. 在使用第三方类库时,经常会看到它自带的演示程序中 ...

  5. Lc.exe已退出,代码为-1

    编译项目,出现提示"Lc.exe已退出,代码为-1" .   解决办法: 意思就是把licenses.licx这个文件里的内容删除,但是文件还在(此时是个空文件),发生这个问题的原 ...

  6. "LC.exe" exited with code -1 错误

    当打开一个VS程序时出现"LC.exe" exited with code -1错误,解决方法是: 删除licenses.licx文件即可

  7. LC.exe exited with code -1

    昨天从win8.1升级到win10之后, 一切还算顺利, 就是升级时间比较长. 但是快下班的时候 遇到一个问题, 是之前在win8.1上没遇到的, 首先代码win8.1 vs2013 上跑的时候一切正 ...

  8. vs2012编译出错“LC.exe”已退出解决方法

    “LC.exe”已退出,代码为 -1. 解决方法: 将项目Properties下的licenses.licx文件删除,重新编译即可.

  9. TT付款方式、前TT和后TT、LC信用证+TT付款方式

    TT付款方式是以外汇现金方式结算,由您的客户将款项汇至贵公司指定的外汇银行账号内,可以要求货到后一定期限内汇款. .T/T属于商业信用,也就是说付款的最终决定权在于客户.T/T分预付,即期和远期.现在 ...

  10. 错误"Lc.exe 已退出,代码 -1 "

    今天做项目的时候突然出现编译不通过,错误为Lc.exe已退出,代码为-1.这让我郁闷了至少30分钟,后来上网查了一下,才知道原因所在,我们项目中使用了第三方组件(Infragistics)造成的,至于 ...

随机推荐

  1. LGP5653口胡

    操作好像比较神秘. 发现 \(k\) 很小,考虑和 \(k\) 有关的 DP,考虑不出来. 费用提前计算,对 \(w_i\) 做后缀和,那么序列的权值就是 \(\sum_{i=1}^nyw_i\). ...

  2. 今天我自己第一次写了一个Windows批处理bat脚本,一起学习一下吧。

    今天我自己第一次写了一个Windows批处理bat脚本,备注一下 事情原由:自己使用Java开发了一个加解密的工具.但是当把工具给别人使用的时候,别人还需要把代码编译打包, 然后还需要看一下代码里面的 ...

  3. kubernetes内yaml格式

    yaml格式的pod定义文件完整内容: apiVersion: v1 #必选,版本号,例如v1 可通过 kubectl api-versions 获取 kind: Pod #必选,Pod metada ...

  4. 内网渗透----使用mimikatz获取windows登陆口令

    使用mimikatz提取windows密码 以管理员运行后,可以随机打一些字符,进入如下界面 输入aaa::aaa,可展示所有模块: 可采用log命令,保存日志 获取hash与明文用户口令 privi ...

  5. python练习册 每天一个小程序 第0011题

    1 # -*-coding:utf-8-*- 2 3 4 def test(content): 5 flag = 0 6 with open('filtered_words.txt') as fp: ...

  6. SpringCloudAlibaba 微服务讲解(三)Nacos Discovery-服务治理

    3.1 服务治理 先来思考一个问题,通过上一章的操作,我们已经实现微服务之间的调用,但是我们把服务提供者的网络地址(ip,端口)等硬编码到了代码中,这种做法存在许多问题: 一旦服务提供者地址变化,就需 ...

  7. SP接口的全双工首发接口整合

    unsigned char bits = 8; unsigned int speed = 50000; unsigned short delay; static void spi_transfer_d ...

  8. 配置Django环境后,运行时报错

    (背景)安装完Django,并配置完成. 在setting.py中设置了数据库时,出现的报错. 点击查看 数据库配置 DATABASES = { 'default': { # 'ENGINE': 'd ...

  9. session监听器和Attribute监听器

    session监听器 有一个web项目,每次一个新的浏览器链接,都会走下面SessionListerenr 方法,该技术可用于网站当前用户的统计 package com.cisst.controlle ...

  10. C# 如何让new 出来的form显示在最外层?

    /// <summary> /// 显示比对不同点的位置 /// </summary> public void showDiffImage() { //在此处弹出不一样图 Bi ...