麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

leetcode1. Two Sum

2019-11-10 19:10:55
字體:
來源:轉載
供稿:網友

1. Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

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

Example: Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].

解法一

兩個循環遍歷,取兩個序號

public 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 = i + 1; j < nums.length; j++) { if (nums[i] + nums[j] == target) { result[0] = i; result[1] = j; return result; } } } return result; }}

Runtime: 44 ms

解法二

利用hashmap,key存放數值,value存放出現的位置。從前到后進行遍歷,將target值減去當前的值,看是否存在map中,

若存在map中則取出相應的標號,退出。

public class Solution { public int[] twoSum(int[] nums, int target) { int[] result = new int[2]; HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); for(int i = 0; i < nums.length; i++) { int num = target - nums[i]; if (map.containsKey(num)) { result[0] = map.get(num); result[1] = i; return result; } map.put(nums[i], i); } return result; }}

Runtime: 12 ms


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美一级鲁丝片免费看 | 国产精品99久久久久久久女警 | 久久久www成人免费精品 | 中文欧美日韩 | 一级电影免费看 | 久久久久久久久久久亚洲 | 黄色片免费在线 | 9191久久久久视频 | 91福利影视| 狠狠操精品视频 | 免费男女乱淫真视频 | 久久久在线免费观看 | 日韩做爰视频免费 | 中文欧美日韩 | 亚洲成人黄色片 | 久久99国产伦子精品免费 | 亚洲成人久久精品 | 欧美精品欧美极品欧美激情 | 黄色免费高清网站 | 日韩电影一区二区三区 | 天海翼无删减av三级在线观看 | free性欧美hd另类 | 欧美国产综合视频 | 电影一级毛片 | 免费国产一级淫片 | 亚洲成人福利网站 | 国产成人午夜高潮毛片 | 国产污污视频 | 一级全毛片 | 一区二区国产在线 | 精品一区二区久久久久久按摩 | 黄色av网| 精品国产乱码一区二区 | 国产女厕一区二区三区在线视 | 久久成人视屏 | 日本黄色免费片 | 天天看天天摸天天操 | av色哟哟| 91短视频在线播放 | 色网站免费观看 | 亚洲成人播放 |