Leetcode第1题:两数之和
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的 两个 整数。
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
Python:
nums = [2, 7, 11, 15, 29]
target = 17 # print(nums[0]) def calcOrder(nums, target):
m = 0
n = 0
for i in range(len(nums) - 1):
for j in range(i + 1, len(nums)):
# print("i+j= ",nums[i]+nums[j])
if nums[i] + nums[j] == target:
# print(i, j)
m, n = i, j
# print("result1", m, n)
return m, n print(calcOrder(nums, target))
Java
public class SumOf2Nums {
public static void main(String args[]) {
int[] array1 = { 1, 4, 7, 11 };
int target = 12;
SumOf2Nums s2n = new SumOf2Nums();
int[] orders = s2n.calcOrder(array1, target);
for (int i = 0; i < orders.length; i++) {
System.out.print(" " + orders[i]);
} } public int[] calcOrder(int[] arr, int target) {
int[] result = { 0, 0 };
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] + arr[j] == target) {
result[0] = i;
result[1] = j;
}
}
}
return result;
}
}
今晚学习成果:
1、Python面试110题看到第25题https://www.cnblogs.com/lmx123/p/9230589.html
2、自己独立实现了leetcode第1题
Leetcode第1题:两数之和的更多相关文章
- leetcode每日一题——两数之和
题目: 两数之和 难度: 简单 描述: 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 解法: class Solutio ...
- 【leetcode】 算法题 两数之和
问题 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 1 ...
- leetcode刷题--两数之和(简单)
一.序言 第一次刷leetcode的题,之前从来没有刷题然后去面试的概念,直到临近秋招,或许是秋招结束的时候才有这个意识,原来面试是需要刷题的,面试问的问题都是千篇一律的,只要刷够了题就差不多了,当然 ...
- 【JavaScript】Leetcode每日一题-平方数之和
[JavaScript]Leetcode每日一题-平方数之和 [题目描述] 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c . 示例1: 输入:c = 5 ...
- Leetcode:0002(两数之和)
LeetCode:0002(两数之和) 题目描述:给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表.你可以假设除了数字 0 之外,这两 ...
- Leetcode(1)两数之和
Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...
- leetCode刷题 | 两数之和
两数之和: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数 ...
- [LeetCode] 1. Two Sum 两数之和
Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...
- LeetCode Golang实现 1. 两数之和
1. 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这 ...
- Leetcode(一)两数之和
1.两数之和 题目要求: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重 ...
随机推荐
- Unity 打开其他exe文件
using UnityEngine; using System.Collections; using System.Diagnostics;///// public class FeiYuZhu : ...
- sourceTree 生成公钥和私钥 然后拉项目代码
第一步 用sourceTree生成公钥pub和私钥文件ppk 打开sourceTree -> 工具 -> 创建或导入ssh密钥 -> 点击generate -> 上面会显 ...
- postman带上token对接口进行测试
根据验证码进行登陆,登录之后返回token.然后请求其他接口时在header头中带上token访问其他接口进行测试 账户信息输入验证码 登陆成功,看到返回的token 我们这个项目可以刷新一下toke ...
- 【pwnable.kr】blackjack
又一道pwnable nc pwnable.kr 9009 读题找到源代码在:http://cboard.cprogramming.com/c-programming/114023-simple-bl ...
- python 输出六行星号✳
for i in range(1,6): for j in range(5-i): print(" ",end=" ") for j in range(1,2* ...
- Vulkan SDK 之 Graphics Pipeline
A graphics pipeline consists of shader stages, a pipeline layout, a render pass, and fixed-function ...
- Java笔记: protected的真正含义
关于protected关键字,即使是Java初学者也能够说出它的含义:protected修饰的成员可以被子类访问.但是这样理解并不完全准确,下面考虑它的真正含义. Java访问控制回顾 Java语言定 ...
- Node.js NPM 管理包
章节 Node.js NPM 介绍 Node.js NPM 作用 Node.js NPM 包(Package) Node.js NPM 管理包 Node.js NPM Package.json 根据安 ...
- C# NPOI的数据批量导入数据库
public ActionResult Upload(HttpPostedFileBase Namefile) { //判断文件是否存在 if ...
- SOA--基于银行系统实例分析
阅读以下关于 Web 系统设计的叙述 [说明] 某银行拟将以分行为主体的银行信息系统,全面整合为由总行统一管理维护的银行信息系统,实现统一的用户账户管理.转账汇款.自助缴费.理财投资.贷款管理.网上支 ...