網(wǎng)站首頁 編程語言 正文
題目
給定一個整數(shù)數(shù)組?nums
和一個目標(biāo)值?target
,請你在該數(shù)組中找出和為目標(biāo)值的那?兩個?整數(shù),并返回他們的數(shù)組下標(biāo)。
你可以假設(shè)每種輸入只會對應(yīng)一個答案。但是,你不能重復(fù)利用這個數(shù)組中同樣的元素。
示例:
給定 nums = [2, 7, 11, 15], target = 9
因為 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
提示:不能自身相加。
測試用例
[2,7,11,15]
9
預(yù)期結(jié)果
[0,1]
?格式模板
public class Solution { public int[] TwoSum(int[] nums, int target) { /* 代碼 */ } }
筆者的代碼,僅供參考
使用暴力方法,運行時間 700ms-1100ms
public class Solution { public int[] TwoSum(int[] nums, int target) { int [] a = new int[2]; for (int i = 0; i < nums.Length - 1; i++) { for (int j = i + 1; j < nums.Length; j++) { if (nums[i] + nums[j] == target) { a[0] = i; a[1] = j; } } } return a; } }
運行時間 400ms-600ms
由于使用的是哈希表,所以缺點是鍵不能相同。
public class Solution { public int[] TwoSum(int[] nums, int target) { int[] a = new int[2]; System.Collections.Hashtable hashtable = new System.Collections.Hashtable(); for(int i = 0; i < nums.Length; i++) { hashtable.Add(nums[i], i); } for(int i = 0; i < nums.Length; i++) { int complement = target - nums[i]; if (hashtable.ContainsKey(complement) && int.Parse(hashtable[complement].ToString())!=i) { a[0] = i; a[1] = int.Parse(hashtable[complement].ToString()); } } return a; } }
還是哈希表,缺點是哈希表存儲的類型是object,獲取值時需要進行轉(zhuǎn)換。
public int[] TwoSum(int[] nums, int target) { int[] a = new int[2]; System.Collections.Hashtable h = new System.Collections.Hashtable(); for (int i = 0; i < nums.Length; i++) { int c = target - nums[i]; if (h.ContainsKey(c)) { a[0] = int.Parse(h[c].ToString()) <= nums[i] ? int.Parse(h[c].ToString()) : i; a[1] = int.Parse(h[c].ToString()) > nums[i] ? int.Parse(h[c].ToString()) : i; } else if (!h.ContainsKey(nums[i])) { h.Add(nums[i], i); } } return a; }
抄一下別人的
public class Solution { public int[] TwoSum(int[] nums, int target) { int[] res = {0, 0}; int len = nums.Length; Dictionary<int, int> dict = new Dictionary<int, int>(); for (int i = 0; i < len; i++) { int query = target - nums[i]; if (dict.ContainsKey(query)) { int min = (i <= dict[query]) ? i : dict[query]; int max = (i <= dict[query]) ? dict[query] : i; return new int[] { min, max }; } else if (!dict.ContainsKey(nums[i])) { dict.Add(nums[i], i); } } return res; } }
原文鏈接:https://www.cnblogs.com/whuanle/p/10342416.html
相關(guān)推薦
- 2022-06-02 C語言的模板與泛型編程你了解嗎_C 語言
- 2022-07-12 aqs原理初探以及公平鎖和非公平鎖實現(xiàn)
- 2022-11-06 關(guān)于useEffect的第二個參數(shù)解讀_React
- 2023-05-30 python中pip無法正確安裝或路徑出錯的解決方案_python
- 2023-11-23 寶塔數(shù)據(jù)庫過大導(dǎo)入失效解決方案
- 2022-11-04 Android自定義定時鬧鐘開發(fā)_Android
- 2022-12-13 iOS底層實例解析Swift閉包及OC閉包_IOS
- 2022-02-20 Linux下安裝jdk包含卸載OpenJDK介紹_Linux
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支