【LeetCode-數組篇】 1 Two Sum
1 前言
之所以開始刷 LeetCode 上的算法題,一是快面臨秋招,第二點是因為提升自己的編程能力,在博客上爭取每天刷 5 道左右的算法題,堅持兩個月,希望博友們監督。
這個系列打算用 C# 和 Java 編程,為什麽用兩門語言,因為經歷春招,發現招 C# 的公司是在是太太太少了,只能在學 C# 的同時,將 Java 基礎也順帶練一練,C# 和 Java 的語法差不多,不會有點太大難都。
廢話不多說,開始上題吧。
2 題目及解決
2.1 題目
給定一個整數數組和一個目標值,找出數組中和為目標值的兩個數。
你可以假設每個輸入只對應一種答案,且同樣的元素不能被重復利用。
示例:
給定 nums = [2, 7, 11, 15], target = 9 因為 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1]
2.2 C# 解決方法
2.2.1 暴力解決
題目蠻簡單,暴力解決即可。
public class Solution { public int[] TwoSum(int[] nums, int target) { int[] res = 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){ res[0] = i ; res[1] = j ; break; } } } return res; } }
暴力解法,兩次for循環,遍歷所有可能,這也是容易想到的方法,時間復雜度O(n^2),空間復雜度O(1);
2.2.2 使用字典
public class Solution { public int[] TwoSum(int[] nums, int target) { Dictionary<int,int> symbol = new Dictionary<int,int>(); int[] res = new int[2]; //數據存入字典 for(int i=0;i<nums.Length;i++) { symbol.Add(nums[i], i); } for(int j=0;j<nums.Length;j++) { int t = target - nums[j]; if(symbol.ContainsKey(t) && symbol[t]!=j) { res[0] = j; res[1] = symbol[t]; break; } } return res; } }
時間復雜度T(n) = O(n)
2.3 Java 解決方法
2.3.1暴力解決
class Solution { public int[] twoSum(int[] nums, int target) { int[] res = 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){ res[0] = i ; res[1] = j ; break; } } } return res; } }
2.3.2 使用HashMap
class Solution { public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) { map.put(nums[i], i); }
for (int i = 0; i < nums.length; i++) { int complement = target - nums[i]; if (map.containsKey(complement) && map.get(complement) != i) { return new int[] { i, map.get(complement) }; } } throw new IllegalArgumentException("No two sum solution"); } }
利用哈希表,每次存儲target減去當前數的差值(key),當前值的下標(value),當再碰到這個值時,即找到了符合要求的值。時間復雜度O(n),空間復雜度O(n);
3 總結
3.1 C# 和 Java 中的數組表示不同
3.1.1 C# 數組表示
一維數組的聲明與賦值:
class Program { static void Main(string[] args) { //一、一維數組 //1.1聲明和初始化一維數組 int[] array1 = new int[10]; //1.2賦值一維數組 //方式1 可以創建並初始化一個數組,比如:(【】裏也可以不省略數組的大小,但是只要寫了數組的大小,後面的賦值就要滿足數組的大小) int[] array2 = new int[] { 1, 2, 3, 4, 5 }; int[] array3 = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; //方式2 通過使用索引號賦值給一個單獨的數組元素,比如: array1[0] = 1; //方式3 在聲明數組的同時給數組賦值,比如: double[] balance = { 2340.0, 4523.69, 3421.0 }; Console.WriteLine(array1.Length);//顯示數組長度 Console.ReadKey(); } }
二維數組的聲明與賦值:
static void Main(string[] args) {//聲明和初始化以及賦值二維數組 int[,] a = new int[3, 4] { {0, 1, 2, 3} , /* 初始化索引號為 0 的行 */ {4, 5, 6, 7} , /* 初始化索引號為 1 的行 */ {8, 9, 10, 11} /* 初始化索引號為 2 的行 */ }; Console.WriteLine(a.Length); Console.ReadKey(); }
3.1.2 Java中的數組表示
一維數組和 C# 相同
二維數組的聲明與賦值:
static void Main(string[] args) { String str[][] = new String[3][4]; System.out.println( total.length); }
Java 的二維數組和 C# 的二維數組表示差距還蠻大的,要註意使用!!!
區別 1 :Java中的一維數組中的 “[ ]” 可以放在 變量後邊也可以放在前邊,而 C# 的 放在前面。Java中的二維數組 要以 “[ ] [ ]” 放在 變量後邊 ,而 C# 中二維數組 是以 “[ , ]”放在變量前邊的。。。(為了方便記憶不記混,同一 C# 放在變量前 ,Java 放在比變量後)
區別2:C# 表示數組長度的 Length 要首字母要寫 ,Java中的數組長度要小寫 。
區別3:在.Net 模仿java 的過程中 拋棄了 HashMap ,所以以後再去面試.Net的時候當別人問你HashTable 和HashMap 的區別的時候,請告訴他,C#.Net 中 沒有HashMap 。
【LeetCode-數組篇】 1 Two Sum