題目英文
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]. Given nums = [230, 863, 916, 585, 981, 404, 316, 785, 88, 12, 70, 435, 384, 778, 887, 755, 740, 337, 86, 92, 325, 422, 815, 650, 920, 125, 277, 336, 221, 847, 168, 23, 677, 61, 400, 136, 874, 363, 394, 199, 863, 997, 794, 587, 124, 321, 212, 957, 764, 173, 314, 422, 927, 783, 930, 282, 306, 506, 44, 926, 691, 568, 68, 730, 933, 737, 531, 180, 414, 751, 28, 546, 60, 371, 493, 370, 527, 387, 43, 541, 13, 457, 328, 227, 652, 365, 430, 803, 59, 858, 538, 427, 583, 368, 375, 173, 809, 896, 370, 789], target = 542 Because nums[28] + nums[45] = 221 + 321 = 542,return [28, 45].
題目中文
給定一個整數數組 nums 和一個目標值 target,請你在該數組中找出和為目標值的那 兩個整數,並返回他們的數組下標。
你可以假設每種輸入只會對應一個答案。但是,你不能重複利用這個數組中同樣的元素。
示例:
給定 nums = [2, 7, 11, 15], target = 9 因為 nums[0] + nums[1] = 2 + 7 = 9,所以返回 [0, 1] 給定 nums = [230, 863, 916, 585, 981, 404, 316, 785, 88, 12, 70, 435, 384, 778, 887, 755, 740, 337, 86, 92, 325, 422, 815, 650, 920, 125, 277, 336, 221, 847, 168, 23, 677, 61, 400, 136, 874, 363, 394, 199, 863, 997, 794, 587, 124, 321, 212, 957, 764, 173, 314, 422, 927, 783, 930, 282, 306, 506, 44, 926, 691, 568, 68, 730, 933, 737, 531, 180, 414, 751, 28, 546, 60, 371, 493, 370, 527, 387, 43, 541, 13, 457, 328, 227, 652, 365, 430, 803, 59, 858, 538, 427, 583, 368, 375, 173, 809, 896, 370, 789], target = 542 因為 nums[28] + nums[45] = 221 + 321 = 542 所以返回 [28, 45]
算法實現
第一種:暴力匹配算法
public class Solution { public int[] TwoSum(int[] nums, int target) { int[] result = new int[2]; for (int i = 0; i < nums.Length; i++) { int find = target - nums[i]; for (int j = i + 1; j < nums.Length; j++) { if (find == nums[j]) { result[0] = i; result[1] = j; return result; } } } return result; } }
第二種:通過Hash的方式
public class Solution { public int[] TwoSum(int[] nums, int target) { int[] result = new int[2]; Dictionary dic = new Dictionary(); for (int i = 0; i < nums.Length; i++) { int find = target - nums[i]; if (dic.ContainsKey(find)) { result[0] = dic[find]; result[1] = i; break; } if (dic.ContainsKey(nums[i]) == false) dic.Add(nums[i], i); } return result; } }
實驗結果
第一種方式,用時532ms
第二種方式,用時380ms
相關圖文:
- LeetCode實戰:刪除鏈表的倒數第N個節點
- LeetCode實戰:合併兩個有序鏈表
- LeetCode實戰:兩兩交換鏈表中的節點
- LeetCode實戰:旋轉鏈表
- LeetCode實戰:相同的樹
- LeetCode實戰:對稱二叉樹
- LeetCode實戰:二叉樹的最大深度
- LeetCode實戰:搜索二維矩陣
- LeetCode實戰:將有序數組轉換為二叉搜索樹
- 資料分享:數學建模資料分享 -- 圖論部分
- 資料分享:數學建模資料分享 -- 神經網絡部分
- 如何利用 C# 實現 K 最鄰近算法?
- 如何利用 C# 實現 K-D Tree 結構?
- 如何利用 C# + KDTree 實現 K 最鄰近算法?
- 如何利用 C# 對神經網絡模型進行抽象?
- 如何利用 C# 實現神經網絡的感知器模型?
- 如何利用 C# 實現 Delta 學習規則?
- 如何利用 C# 實現 誤差反向傳播 學習規則?
- 如何利用 C# 爬取帶 Token 驗證的網站數據?
- 如何利用 C# 向 Access 數據庫插入大量數據?
- 如何利用 C# + Python 破解貓眼電影的反爬蟲機制?
經過8年多的發展,LSGO軟件技術團隊在「地理信息系統」、「數據統計分析」、「計算機視覺」等領域積累了豐富的研發經驗,也建立了人才培養的完備體系,由於自己準備在「量化交易」領域精進技能,如果大家對這個領域感興趣可以與我聯繫,加入我們的量化學習群一起學習探討。
在這個領域我已做了以下積累:
策略部分:
- 數字貨幣 One 的投資價值分析
- 數字資產量化中的跨市場套利策略
- 數字資產量化中的同市場套利策略
- 數字資產量化中的網格交易法
- 我們能否效仿李笑來的投資策略?
- 賺錢是剛需,如何正確的交易股票?
數據部分:
- 如何利用 C# 爬取 One 的交易數據?
- 如何利用 C# 爬取 One 持有者返利數據?
- 如何利用 C# 爬取BigOne交易所的公告?
- 如何利用 C# 爬取Gate.io交易所的公告?
- 如何利用 C# 爬取「財報說」中的股票數據?
自動化交易部分:
- 封裝BigOne API:身份驗證
- 封裝BigOne API:獲取賬戶資產
- 封裝BigOne API:訂單系統
- 封裝BigOne API:網格交易法
- 封裝BigOne API:代碼的重構
- 進一步完善自動化交易系統 01
- 進一步完善自動化交易系統 02
- 進一步完善自動化交易系統 03
- 進一步完善自動化交易系統 04
- 如何開發「股票數據分析軟件」(上)
- 如何開發「股票數據分析軟件」(中)
- 如何開發「股票數據分析軟件」(下)
- 進一步完善「股票數據分析軟件」 - 01
後臺回覆「搜搜搜」,隨機獲取電子資源!
歡迎關注,請掃描二維碼: