網站首頁 編程語言 正文
題目
給定一個整數數組?nums
和一個目標值?target
,請你在該數組中找出和為目標值的那?兩個?整數,并返回他們的數組下標。
你可以假設每種輸入只會對應一個答案。但是,你不能重復利用這個數組中同樣的元素。
示例:
給定 nums = [2, 7, 11, 15], target = 9
因為 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
提示:不能自身相加。
測試用例
[2,7,11,15]
9
預期結果
[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,獲取值時需要進行轉換。
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
相關推薦
- 2022-06-01 python?全角半角互換的實現示例_python
- 2022-04-27 一篇文章了解正則表達式的替換技巧_正則表達式
- 2022-07-09 Python實現功能全面的學生管理系統_python
- 2022-10-13 云服務器Windows?Server2012配置FTP服務器詳細圖文教程_FTP服務器
- 2022-10-27 Python中對字典的幾個處理方法分享_python
- 2022-07-02 Python?matplotlib繪圖時指定圖像大小及放大圖像詳解_python
- 2024-01-13 什么是B+樹?
- 2022-04-09 Webservice 服務請求參數xml 嵌套問題/CDATA嵌套
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支