leetcode 1 Two Sum
来源:互联网 发布:sd卡数据修复 编辑:程序博客网 时间:2024/06/06 15:27
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
返回值为一个长度为2的数组,
public class Solution { public int[] twoSum(int[] nums, int target) {int[] re = {-1,-1};int m = 0; //标志用于跳出两层循环for(int i=0;i<nums.length-1;i++){int a = target - nums[i];for(int j=i+1;j<nums.length;j++){if(nums[j] == a){ re[0] = i+1; re[1] = j+1; m=1; break;}}if(m == 1)break;}return re; }}
0 0
- LeetCode 1 - Two Sum
- leetcode 1 Two Sum
- Leetcode【1】:Two Sum
- [leetcode 1] Two Sum
- 【leetcode-1】Two Sum
- [leetcode 1] Two Sum
- [Leetcode] 1 - Two Sum
- LeetCode (1) Two Sum
- LeetCode 1:《Two Sum》
- LeetCode | #1 Two Sum
- leetcode-1 Two Sum
- Two Sum | LeetCode(1)
- [Leetcode]1Two Sum
- leetcode 1 Two Sum
- leetcode #1 Two Sum
- leetcode 1:Two Sum
- LeetCode 1 Two Sum
- leetcode #1 two sum
- C++primer第五版笔记-第九章顺序容器
- DBus 进程间的通信
- LeetCode-Invert Binary Tree
- hdu 1423 Greatest Common Increasing Subsequence 最长公共上升子序列
- 什么是EMC?说出至少三种测试实验项目,如何解决
- leetcode 1 Two Sum
- [Django入门知识浅介]再写一个Django动态程序
- UVa 1709 Amalgamated Artichokes
- CodeForce 584D Dima and Lisa(哥德巴赫猜想)
- HDU 5437 Alisha’s Party 优先队列
- MySQL技术之旅-MySQL的前世今生
- 协程
- SABER 最强大的数模混合信号仿真软件
- HDU 1026 BFS