日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C# 算法题系列(一) 两数之和、无重复字符的最长子串

發布時間:2023/12/18 C# 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C# 算法题系列(一) 两数之和、无重复字符的最长子串 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目一

原題鏈接?https://leetcode-cn.com/problems/two-sum/

給定一個整數數組?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;} } --------------------- 作者:Bruce-Yeung 來源:CSDN 原文:https://blog.csdn.net/lzuacm/article/details/80551669 版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

?

題目二

原題地址?https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/

給定一個字符串,請你找出其中不含有重復字符的?最長子串?的長度。

示例?1:

輸入: "abcabcbb" 輸出: 3 解釋: 因為無重復字符的最長子串是 "abc",所以其長度為 3

示例 2:

輸入: "bbbbb" 輸出: 1 解釋: 因為無重復字符的最長子串是 "b",所以其長度為 1

示例 3:

輸入: "pwwkew" 輸出: 3 解釋: 因為無重復字符的最長子串是 "wke",所以其長度為 3

要注意字符串為空、變量為null、字符串長度 Length = 1 等情況。

測試實例

輸入 " " "au" "abcabcbb" "bbbbb" "pwwkew" "aab"

預期結果分別是 1,2,3,1,3,2

代碼格式模板

public class Solution {public int LengthOfLongestSubstring(string s) {} }

?

筆者的代碼僅供參考

使用最笨的方式,200ms左右

public class Solution {public int LengthOfLongestSubstring(string s) {if (s == null || s == "")return 0;char[] a = s.ToCharArray(); //字符串轉為字符數組int start = 0; //區間開始位置int stop = 0; //區間結束位置int newMax = 1; //當前區間數int max = 1; //區間最大個數for (stop = 1; stop < a.Length; stop++) //每次向后移動一位 {bool b = false; //是否存在重復for (int i = start; i < stop; i++) //檢查當前元素在區間是否有相同值 {if (a[stop] == a[i]) //如果stop+1位在區間找到相同的字符 {char ls = a[stop];if (newMax > max) max = newMax;start = i + 1; //區間開始位置重置newMax = stop - start + 1;b = true; break;}}if (b == false)newMax += 1;}if (newMax > max) max = newMax;return max;} }

完整測試代碼(控制臺)

using System;namespace ConsoleApp1 {public class Testa{public int LengthOfLongestSubstring(string s){if (s == null || s == "")return 0;char[] a = s.ToCharArray(); //字符串轉為字符數組int start = 0; //區間開始位置int stop = 0; //區間結束位置int newMax = 1; //當前區間數int max = 1; //區間最大個數for (stop = 1; stop < a.Length; stop++) //每次向后移動一位 {bool b = false; //是否存在重復for (int i = start; i < stop; i++) //檢查當前元素在區間是否有相同值 {if (a[stop] == a[i]) //如果stop+1位在區間找到相同的字符 {char ls = a[stop];if (newMax > max) max = newMax;start = i + 1; //區間開始位置重置newMax = stop - start + 1; //重新設置區間數b = true; break;}}if (b == false) ////沒有重新設置區間數時加1newMax += 1;}if (newMax > max) max = newMax;return max;}}class Program{static void Main(string[] args){Testa t1 = new Testa(); //正確結果Console.WriteLine(t1.LengthOfLongestSubstring(" ")); //1Console.WriteLine(t1.LengthOfLongestSubstring("au")); //2Console.WriteLine(t1.LengthOfLongestSubstring("abcabcbb")); //3Console.WriteLine(t1.LengthOfLongestSubstring("bbbbb")); //1Console.WriteLine(t1.LengthOfLongestSubstring("pwwkew")); //3Console.WriteLine(t1.LengthOfLongestSubstring("aab")); //2 Console.ReadKey();}} }

?

使用哈希集合,速度更快,100ms-150ms

public int LengthOfLongestSubstring(string s){int n = s.Length;HashSet<char> set = new HashSet<char>(); //集合int ans = 0, start = 0, stop = 0; //ans為字符串長度,starp區間起點,stop區間終點while (start < n && stop < n){// try to extend the range [i, j]if (!set.Contains(s[stop])){set.Add(s[stop++]);ans = Math.Max(ans, stop - start);//或者ans = ans > (stop - start) ? ans : (stop - start) }else{set.Remove(s[start++]);}}return ans;}

?

完整控制臺測試代碼

using System; using System.Collections.Generic; using System.Linq;namespace ConsoleApp2 {public class Solution{public int LengthOfLongestSubstring(string s){int n = s.Length;HashSet<char> set = new HashSet<char>(); //集合int ans = 0, start = 0, stop = 0; //ans為字符串長度,starp區間起點,stop區間終點while (start < n && stop < n){// try to extend the range [i, j]if (!set.Contains(s[stop])){set.Add(s[stop++]);ans = Math.Max(ans, stop - start);//或者ans = ans > (stop - start) ? ans : (stop - start) }else{set.Remove(s[start++]);}}return ans;}}class Program{static void Main(string[] args){Solution t1 = new Solution(); //正確結果Console.WriteLine(t1.LengthOfLongestSubstring(" ")); //1Console.WriteLine(t1.LengthOfLongestSubstring("au")); //2Console.WriteLine(t1.LengthOfLongestSubstring("abcabcbb")); //3Console.WriteLine(t1.LengthOfLongestSubstring("bbbbb")); //1Console.WriteLine(t1.LengthOfLongestSubstring("pwwkew")); //3Console.WriteLine(t1.LengthOfLongestSubstring("aab")); //2 Console.ReadKey();}} }

?

轉載于:https://www.cnblogs.com/whuanle/p/10342416.html

總結

以上是生活随笔為你收集整理的C# 算法题系列(一) 两数之和、无重复字符的最长子串的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。