博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode-Two Sum
阅读量:7251 次
发布时间:2019-06-29

本文共 854 字,大约阅读时间需要 2 分钟。

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9

Output: index1=1, index2=2

public class Solution {    public int[] twoSum(int[] nums, int target) {        int[] result = new int[2];        int i, j;        for (i = 0; i < nums.length; i++) {            for (j = i + 1; j < nums.length; j++) {                if (nums[i] + nums[j] == target) {                    result[0] = i+1;                    result[1] = j+1;                }            }        }        return result;    }}

转载地址:http://eehbm.baihongyu.com/

你可能感兴趣的文章
SAP云平台对Kubernetes的支持
查看>>
原来实现GCP用客户端登录这么简单啊
查看>>
PAT A1057 分块思想
查看>>
PAT A1007 动态规划
查看>>
VUE父子组件传递数据
查看>>
前端知识点——图片
查看>>
别人家的程序员是如何使用 Java 进行 Web 抓取的?
查看>>
95%的技术面试必考的JVM知识点都在这,另附加分思路!
查看>>
日期类问题
查看>>
区块链入门之基础知识
查看>>
mysql锁(Innodb)
查看>>
小程序开发之影分身术
查看>>
磨刀霍霍:爬爬爬爬爬爬虫爬起来~
查看>>
RxJava中的Observable,多Subscribers
查看>>
I/O模型和Java NIO源码分析
查看>>
第二天-《企业应用架构模式》-组织领域逻辑
查看>>
日志服务与SIEM(如Splunk)集成方案实战
查看>>
解决packet_write_wait: Connection to...: Broken pipe
查看>>
图学ES6-3.变量的解构赋值
查看>>
web3j的maven插件
查看>>