From c49a3dce335cf1047e8e88e2e0841d5dca6db7e6 Mon Sep 17 00:00:00 2001 From: MYP Date: Thu, 22 Oct 2020 19:45:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DataStructureAndAlgorithm/13 字符串.md | 553 ---------- .../13 字符串与整数集合.md | 991 ++++++++++++++++++ 2 files changed, 991 insertions(+), 553 deletions(-) delete mode 100644 DataStructureAndAlgorithm/13 字符串.md create mode 100644 DataStructureAndAlgorithm/13 字符串与整数集合.md diff --git a/DataStructureAndAlgorithm/13 字符串.md b/DataStructureAndAlgorithm/13 字符串.md deleted file mode 100644 index 0894ac3..0000000 --- a/DataStructureAndAlgorithm/13 字符串.md +++ /dev/null @@ -1,553 +0,0 @@ - - -# Task05ַ2죩 - -ǹûеӰӣûϷ磬ǾͻһЩϷ֡δخдһʫңɽˮ֪һƣѳɺʫ;·ãѶĻس١µҹȼţ⸸ȻϹźͶӵʫ䡣޶һ֣֮һ˳ûлؼңҲŷأ͵ˣܲ˸ - -ϸһ֣ʫȻԵ丸򣬼ȳҹصƹ¡ٻؼѶñ·;ʫϺͳ±ʣƱһ¿պ֪ļˮɽңۿݡӶɷ˼Ϲ뿪þã·;ңԶдŲ֪дʲôԺȾҲûʲô¡ֻܺͶҹҹڼһյµ£ϹĹ - -ʫʫһֿԵ򷴸Ķʫ塣ղ׾ɷ˼ӣ˼ɷĹʫDzǸоأ -ӢĵУͬĵطʹ lover Ҳи overʹ friend Ҳи endʹ believe Ҳи lie֣ᷢɣʣȴijϵǴ⼸ʵҲû뵽⡣ - -ǾҪ̸̸Щʻַ⡣ - - -## 1. Ķ - -**1.1 ض** - -- stringַɵУַΪ`S=a0a1...an` -- аַĸΪijȡ -- ΪĴΪմnull stringֱ˫šʾC#Ҳ`string.Empty`ʾ - -һЩҪͣ -- հ״һոɵĴ -- ӴַɵУΪôӴӦİӴĴΪӴһ֡ -- ӴеλãӴеһγʱӴһַеš - -``` -磺 - -A=this is a string; -B=is; - -BAеλΪ2 -``` - -- ȣҶӦλַͬ - - - -**1.2 IJ** - -߼ṹԱƣ֮ͬڴԵַҲǴеԪضַˣڴĻԱкܴġԱעǵԪصIJһԪأɾһԪأиǹעӴӦ⣬ӴλãõָλӴ滻ӴȲ - -ڴĻ£ -- 1ȡij -- 2ȡַָ -- 3ָλòӴ -- 4ָλƳȵӴ -- 5ָλȡӴ -- 6ǰĿ -- 7 -- 8ƥ - -C#Уַ`ToLower`תСд`ToUpper`תд`IndexOf`Ӵλá`LastIndexOf`ҲӴλá`Trim`ȥ߿ոȱȽϷIJʵǰЩչ - - -![ӿ](https://img-blog.csdnimg.cn/20191223194206307.png) - - -## 2. Ĵ洢ʵ - -Ĵ洢ṹԱͬΪ֣ - -- ˳洢char͵顣ǶģʹһԤ󴮳ȣ涨ڴֵһ봮ȵĽ`\0`ʾֵսᡣ -- ʽ洢`SlinkList` ˷Ѵ洢ռ䣩 - - -![˳ͼ](https://img-blog.csdnimg.cn/20191223194433900.png) - -```c -using System; - -namespace LinearStruct -{ - /// - /// ͵ʵ--˳ - /// - public class SeqString : IString - { - /// - /// - /// - protected readonly char[] CStr; //ַ'\0' - - /// - /// ʼSeqStringʵ - /// - public SeqString() - { - CStr = new char[] {'\0'}; - } - - /// - /// ʼSeqStringʵ - /// - /// ʼַ - public SeqString(string s) - { - if (s == null) - throw new ArgumentNullException(); - - int length = s.Length; - CStr = new char[length + 1]; - - for (int i = 0; i < length; i++) - CStr[i] = s[i]; - CStr[length] = '\0'; - } - - /// - /// ʼSeqStringʵ(ֻڲʹ) - /// - /// ij - protected SeqString(int length) - { - if (length < 0) - throw new ArgumentOutOfRangeException(); - - CStr = new char[length + 1]; - CStr[length] = '\0'; - } - - /// - /// Ҷʵеַָ Unicode ַԴﵽָܳȡ - /// - /// ַеַԭʼַκַ - /// Unicode ַ - /// - /// Чڴʵһ IStringҶģôﵽ totalWidth Ŀ paddingChar ַ䡣 - /// totalWidth СڴʵijȣΪʵͬ IString - /// - /// - /// 쳣: - /// System.ArgumentOutOfRangeException:totalWidth С㡣 - /// - public IString PadLeft(int totalWidth, char paddingChar) - { - if (totalWidth < 0) - throw new ArgumentOutOfRangeException(); - if (Length >= totalWidth) - return Clone(); - - SeqString result = new SeqString(totalWidth); - int left = totalWidth - Length; - for (int i = 0; i < left; i++) - result.CStr[i] = paddingChar; - for (int i = 0; i < Length; i++) - result.CStr[i + left] = CStr[i]; - - return result; - } - - /// - /// ȡij - /// - public int Length - { - get - { - int i = 0; - while (CStr[i] != '\0') - i++; - return i; - } - } - - /// - /// ȡַָ - /// - /// Ҫȡõַ㿪ʼ - /// ַָ - public char this[int index] - { - get - { - if (index < 0 || index > Length - 1) - throw new IndexOutOfRangeException(); - return CStr[index]; - } - set - { - if (index < 0 || index > Length - 1) - throw new IndexOutOfRangeException(); - CStr[index] = value; - } - } - - /// - /// ָλòӴ - /// - /// λ - /// Ӵ - /// 봮õ´ - public IString Insert(int startIndex, IString s) - { - if (s == null) - throw new ArgumentNullException(); - - if (startIndex < 0 || startIndex > Length) - throw new ArgumentOutOfRangeException(); - - SeqString str = new SeqString(s.Length + Length); - for (int i = 0; i < startIndex; i++) - str.CStr[i] = CStr[i]; //עstr[i]ֱʹǴ - for (int i = 0, len = s.Length; i < len; i++) - str.CStr[i + startIndex] = s[i]; - for (int i = startIndex; i < Length; i++) - str.CStr[i + s.Length] = CStr[i]; - - return str; - } - - /// - /// ָλƳӴ - /// - /// Ƴλ - /// Ƴij - /// Ƴõ´ - public IString Remove(int startIndex, int count) - { - if (startIndex < 0 || startIndex > Length - 1) - throw new ArgumentOutOfRangeException(); - if (count < 0) - throw new ArgumentOutOfRangeException(); - - int left = Length - startIndex; //Ƴַ - count = (left < count) ? left : count; //ʵƳַ - SeqString str = new SeqString(Length - count); - for (int i = 0; i < startIndex; i++) - str.CStr[i] = CStr[i]; - for (int i = startIndex + count; i < Length; i++) - str.CStr[i - count] = CStr[i]; - return str; - } - - /// - /// ָλȡӴ - /// - /// ȡӴλ - /// Ӵij - /// ȡõӴ - public IString SubString(int startIndex, int count) - { - if (startIndex < 0 || startIndex > Length - 1) - throw new ArgumentOutOfRangeException(); - if (count < 0) - throw new ArgumentOutOfRangeException(); - int left = Length - startIndex; //ȡӴ󳤶 - count = (left < count) ? left : count; //Ӵʵʳ - SeqString str = new SeqString(count); - for (int i = 0; i < count; i++) - str.CStr[i] = CStr[i + startIndex]; - return str; - } - - /// - /// ǰĿ - /// - /// ǰĿ - public IString Clone() - { - SeqString str = new SeqString(Length); - for (int i = 0; i < Length; i++) - str.CStr[i] = CStr[i]; - return str; - } - - /// - /// - /// - /// βҪӵĴ - /// Ӻõ´ - public IString Concat(IString s) - { - if (s == null) - throw new ArgumentNullException(); - return Insert(Length, s); - } - - /// - /// ƥ - /// - /// ҪƥӴ - /// Ӵеλ,ڷ-1. - public int FindParam(IString s) - { - if (s == null || s.Length == 0) - throw new Exception("ƥַΪnull."); - for (int i = 0; i <= Length - s.Length; i++) - { - if (CStr[i] == s[0]) - { - int j; - for (j = 1; j < s.Length; j++) - { - if (CStr[j + i] != s[j]) - break; - } - if (j == s.Length) - return i; - } - } - return -1; - } - - /// - /// - /// - /// һ - /// ڶ - /// Ӻõ´ - public static SeqString operator +(SeqString s1, SeqString s2) - { - if (s1 == null || s2 == null) - throw new ArgumentNullException(); - return s1.Concat(s2) as SeqString; - } - - /// - /// SeqStringַ - /// - /// SeqStringַ - public override string ToString() - { - string str = string.Empty; - for (int i = 0; i < Length; i++) - str += CStr[i]; - return str; - } - - /// - /// ƥ - /// - /// ҪƥӴ - /// Ӵеλ,ڷ-1. - public int IndexOf(IString value) - { - if (value == null || value.Length == 0) - throw new Exception("ƥַΪnull."); - return IndexOf(value, 0); - } - - /// - /// ƥ - /// - /// ҪƥӴ - /// ƥʼλ - /// Ӵеλ,ڷ-1. - public int IndexOf(IString value, int startIndex) - { - if (value == null || value.Length == 0) - throw new Exception("ƥַΪnull."); - if (startIndex < 0 || startIndex > value.Length - 1) - throw new ArgumentOutOfRangeException(); - - for (int i = startIndex; i <= Length - value.Length; i++) - { - if (CStr[i] == value[0]) - { - int j; - for (j = 1; j < value.Length; j++) - { - if (CStr[j + i] != value[j]) - break; - } - if (j == value.Length) - return i; - } - } - return -1; - - } - - /// - /// ʵеָ IString ƥ滻Ϊָ IString - /// - /// Ҫ滻 IString - /// Ҫ滻 oldValue ƥ IString - /// Чڴʵ oldValue ʵ滻Ϊ newValue IString - /// - /// 쳣: - /// System.ArgumentException:oldValue ǿַ ("") - /// - public IString Replace(IString oldValue, IString newValue) - { - if (Length == 0) - throw new ArgumentException("oldValueǿַ"); - - string str = string.Empty; - int i = 0; - while (i < Length) - { - if (CStr[i] == oldValue[0]) - { - int j; - for (j = 1; j < oldValue.Length; j++) - { - if (CStr[i + j] != oldValue[j]) - { - break; - } - } - if (j == oldValue.Length) - { - str += newValue; - i += oldValue.Length; - continue; - } - } - str += CStr[i]; - i++; - } - return new SeqString(str); - } - - /// - /// Ƴǰհַβհַ - /// - /// ӵǰĿʼĩβƳпհַַ - public IString Trim() - { - //Ƴǰհַβհַַ. - int left; - int right; - for (left = 0; left < CStr.Length - 1; left++) - { - if (CStr[left] != ' ') - break; - } - if (left == CStr.Length - 1) - return new SeqString(); - - for (right = CStr.Length - 2; right >= 0; right--) - { - if (CStr[right] != ' ') - break; - } - return SubString(left, right - left + 1); - } - } -} -``` - - - - - -## 3. ϰο -**1. Ŀ** - -Ϊ`python`ʵ - -``` -class Solution: - """ - Ҫõ˼·ǣ - ʲôǻڣ - ʵһ,е abcabcbbУڣΪ abc ĿҪ - ٽ aб abcaʱҪԣҪƶУ - ƶ - ֻҪѶеߵԪƳˣֱĿҪ - һֱάĶУҳгijʱ⣡ - """ - def lengthOfLongestSubstring(self, s: str) -> int: - if not s: - return 0 - left = 0 # ڵ - lookup = set() # ַʼΪգΪҶΪ - n = len(s) # ַܳ - max_len = 0 # ظӴij - cur_len = 0 # ǰڵij - for i in range(n): # Ҳƶ - cur_len += 1 # ƶһΣڳȼһ - # жʼ - while s[i] in lookup: # s[i]ʾƶʱ봰ڵֵ - # Ҳ¼ַڴֵͬ򽫴ұƶǸöҪɵļ - lookup.remove(s[left]) # Ƴֵ - left += 1 # ±һ - cur_len -= 1 # ڳȼһ - # ж - if cur_len > max_len: # ǰڳȴڼ¼󴰿ڳȣ¸󴰿ڳ - max_len = cur_len - lookup.add(s[i]) # ж֮󣬼Ҳƶĸ² - return max_len - - -if __name__ == '__main__': - solution = Solution() - max_length = solution.lengthOfLongestSubstring("abcddsd") - print(max_length) -``` - -**2. Ŀ** - - - -Ϊ`python`ʵ - -```python -class Solution: - def findSubstring(self, s, words): - from collections import Counter - if not s or not words: - return [] - one_word = len(words[0]) - all_len = len(words) * one_word # wordsַɵַ - n = len(s) - words = Counter(words) - res = [] - for i in range(0, n - all_len + 1): # രڣരи - tmp = s[i:i + all_len] # ַ i+all_lenǴҲ - # ж - c_tmp = [] - for j in range(0, all_len, one_word): - c_tmp.append(tmp[j:j + one_word]) - if Counter(c_tmp) == words: - # жͨһ± - res.append(i) - return res - - -if __name__ == '__main__': - solution = Solution() - s = "barfoothefoobarman" - words = ["foo", "bar"] - out = solution.findSubstring(s, words) - print(out) -``` - -**3. 滻Ӵõƽַ** - -Ϊ`python`ʵ - -```python -class Solution(object): - def balancedString(self, s): - """ - python汾 - :type s: str - :rtype: int - """ - cnt = collections.Counter(s) - res = n = len(s) - i, avg = 0, n//4 - for j, c in enumerate(s): - cnt[c] -= 1 - while i < n and all(avg >= cnt[x] for x in 'QWER'): - res = min(res, j - i + 1) - cnt[s[i]] += 1 - i += 1 - return res -``` diff --git a/DataStructureAndAlgorithm/13 字符串与整数集合.md b/DataStructureAndAlgorithm/13 字符串与整数集合.md new file mode 100644 index 0000000..0a23e0a --- /dev/null +++ b/DataStructureAndAlgorithm/13 字符串与整数集合.md @@ -0,0 +1,991 @@ +# 13 ַ + +**֪ʶ㣺** + +![ͼ1 ֪ʶṹ](https://img-blog.csdnimg.cn/20201021164938145.png) + +## 1. ַ + +ǹûеӰӣûϷ磬ǾͻһЩϷ֡δخдһʫ**ңɽˮ֪һƣѳɺʫ;·ãѶĻس١µҹȼţ⸸**ȻϹźͶӵʫ䡣޶һ֣֮һ˳ûлؼңҲŷأ͵ˣܲ˸ + +ϸһ֣ʫȻԵ**丸򣬼ȳҹصƹ¡ٻؼѶñ·;ʫϺͳ±ʣƱһ¿պ֪ļˮɽңۿݡ**Ӷɷ˼Ϲ뿪þã·;ңԶдŲ֪дʲôԺȾҲûʲô¡ֻܺͶҹҹڼһյµ£ϹĹ + +ʫʫһֿԵ򷴸Ķʫ塣ղ׾ɷ˼ӣ˼ɷĹʫDzǸоأ +ӢĵУͬĵطʹ lover Ҳи overʹ friend Ҳи endʹ believe Ҳи lie֣ᷢɣʣȴijϵǴ⼸ʵҲû뵽⡣ + +ǾҪ̸̸Щʻַ⡣ + +### 1.1 Ķ + +**1ض** + +- **string**ַɵУַΪ`S=c1c2...cn` +- **ij**аַĸ +- **մnull string**ΪĴֱ˫`""`ʾC#Ҳ`string.Empty`ʾ + +һЩҪͣ +- **հ״**һոɵĴ +- **Ӵ**ַɵУΪôӴӦİӴĴΪӴһ֡ +- **Ӵеλ**ӴеһγʱӴһַеš + + +磺 + +```c +A = this is a string; +B = is; +// BAеλΪ2 +``` + +- ****ҶӦλַͬ + + + +**2IJ** + +߼ṹԱƣ֮ͬڴԵַҲǴеԪضַˣڴĻԱкܴġ +- ԱעǵԪصIJһԪأɾһԪء +- עӴӦ⣬ӴλãõָλõӴ滻ӴȲ + +ڴĻ£ +- ȡij +- ȡַָ +- ָλòӴ +- ָλƳȵӴ +- ָλȡӴ +- ǰĿ +- +- ƥ +- ַ +- ȥ߿ո + + + + C# Уַ`ToLower()`תСд`ToUpper()`תдȱȽϷIJʵǰЩչ + +![ͼ2 ӿ](https://img-blog.csdnimg.cn/20201020205757872.png) + +```c +namespace LinearStruct +{ + /// + /// ij + /// + public interface IString + { + /// + /// ȡij + /// + int Length { get; } + + /// + /// ȡַָ + /// + /// Ҫȡõַ㿪ʼ + /// ַָ + char this[int index] { get; set; } + + /// + /// ָλòӴ + /// + /// λ + /// Ӵ + /// 봮õ´ + IString Insert(int startIndex, IString s); + + /// + /// ָλƳӴ + /// + /// Ƴλ + /// Ƴij + /// Ƴõ´ + IString Remove(int startIndex, int count); + + /// + /// ָλȡӴ + /// + /// ȡӴλ + /// Ӵij + /// ȡõӴ + IString SubString(int startIndex, int count); + + /// + /// ǰĿ + /// + /// ǰĿ + IString Clone(); + + /// + /// + /// + /// βҪӵĴ + /// Ӻõ´ + IString Concat(IString s); + + /// + /// ƥ + /// + /// ҪƥӴ + /// ƥĿʼλ + /// Ӵеλ,ڷ-1. + int IndexOf(IString value, int startIndex = 0); + + /// + /// ַ + /// + /// ַеַ + /// ַ + /// totalWidthСڴʵijȣΪʵͬIString + IString PadLeft(int totalWidth, char paddingChar); + + /// + /// ȥ˵Ŀո + /// + /// ӵǰĿʼĩβƳпհַַ + IString Trim(); + } +} +``` + +### 1.2 Ĵ洢ʵ + +Ĵ洢ṹԱͬΪ֣ + +- ˳洢char͵顣ǶģʹһԤ󴮳ȣ涨ڴֵһ봮ȵĽ`\0`ʾֵսᡣ +- ʽ洢`SlinkList` ˷Ѵ洢ռ䣩 + + +![ͼ3 ˳ͼ](https://img-blog.csdnimg.cn/20201020210836226.png) + +```c +using System; + +namespace LinearStruct +{ + /// + /// ͵ʵ--˳ + /// + public class SeqString : IString + { + protected readonly char[] CStr; //ַ'\0' + + /// + /// ʼSeqStringʵ + /// + public SeqString() + { + CStr = new char[] { '\0' }; + } + + /// + /// ʼSeqStringʵ + /// + /// ʼַ + public SeqString(string s) + { + if (s == null) + throw new ArgumentNullException(); + + int len = s.Length; + CStr = new char[len + 1]; + for (int i = 0; i < len; i++) + { + CStr[i] = s[i]; + } + CStr[len] = '\0'; + } + + /// + /// ʼSeqStringʵ(ֻڲʹ) + /// + /// ij + protected SeqString(int length) + { + if (length < 0) + throw new ArgumentOutOfRangeException(); + + CStr = new char[length + 1]; + CStr[length] = '\0'; + } + + /// + /// ȡij + /// + public int Length + { + get + { + int i = 0; + while (CStr[i] != '\0') + i++; + return i; + } + } + + /// + /// ȡַָ + /// + /// Ҫȡõַ㿪ʼ + /// ַָ + public char this[int index] + { + get + { + if (index < 0 || index > Length - 1) + throw new IndexOutOfRangeException(); + return CStr[index]; + } + set + { + if (index < 0 || index > Length - 1) + throw new IndexOutOfRangeException(); + CStr[index] = value; + } + } + + /// + /// ָλòӴ + /// + /// λ + /// Ӵ + /// 봮õ´ + public IString Insert(int startIndex, IString s) + { + if (s == null) + throw new ArgumentNullException(); + if (startIndex < 0 || startIndex > Length) + throw new ArgumentOutOfRangeException(); + + int len1 = Length; + int len2 = s.Length; + SeqString str = new SeqString(len1 + len2); + for (int i = 0; i < startIndex; i++) + { + str.CStr[i] = CStr[i]; //עstr[i]ֱʹǴ + } + for (int i = 0; i < len2; i++) + { + str.CStr[i + startIndex] = s[i]; + } + for (int i = startIndex; i < len1; i++) + { + str.CStr[i + len2] = CStr[i]; + } + return str; + } + + /// + /// + /// + /// βҪӵĴ + /// Ӻõ´ + public IString Concat(IString s) + { + if (s == null) + throw new ArgumentNullException(); + + return Insert(Length, s); + } + + /// + /// + /// + /// һ + /// ڶ + /// Ӻõ´ + public static SeqString operator +(SeqString s1, SeqString s2) + { + if (s1 == null || s2 == null) + throw new ArgumentNullException(); + + return s1.Concat(s2) as SeqString; + } + /// + /// ָλƳӴ + /// + /// Ƴλ + /// Ƴij + /// Ƴõ´ + public IString Remove(int startIndex, int count) + { + if (startIndex < 0 || startIndex > Length - 1) + throw new ArgumentOutOfRangeException(); + if (count < 0) + throw new ArgumentOutOfRangeException(); + + int len = Length; + int left = len - startIndex; //Ƴַ + count = (left < count) ? left : count; //ʵƳַ + SeqString str = new SeqString(len - count); + for (int i = 0; i < startIndex; i++) + { + str.CStr[i] = CStr[i]; + } + for (int i = startIndex + count; i < len; i++) + { + str.CStr[i - count] = CStr[i]; + } + return str; + } + + /// + /// ָλȡӴ + /// + /// ȡӴλ + /// Ӵij + /// ȡõӴ + public IString SubString(int startIndex, int count) + { + if (startIndex < 0 || startIndex > Length - 1) + throw new ArgumentOutOfRangeException(); + if (count < 0) + throw new ArgumentOutOfRangeException(); + + int left = Length - startIndex; //ȡӴ󳤶 + count = (left < count) ? left : count; //Ӵʵʳ + SeqString str = new SeqString(count); + for (int i = 0; i < count; i++) + str.CStr[i] = CStr[i + startIndex]; + return str; + } + + /// + /// ǰĿ + /// + /// ǰĿ + public IString Clone() + { + int len = Length; + SeqString str = new SeqString(len); + for (int i = 0; i < len; i++) + str.CStr[i] = CStr[i]; + return str; + } + + /// + /// ַ + /// + /// ַеַ + /// ַ + /// + /// totalWidthСڴʵijȣΪʵͬIString + /// + /// + /// 쳣: + /// System.ArgumentOutOfRangeException:totalWidth С㡣 + /// + public IString PadLeft(int totalWidth, char paddingChar) + { + if (totalWidth < 0) + throw new ArgumentOutOfRangeException(); + + int len = Length; + if (len >= totalWidth) + return Clone(); + + SeqString result = new SeqString(totalWidth); + int left = totalWidth - len; + for (int i = 0; i < left; i++) + { + result.CStr[i] = paddingChar; + } + for (int i = 0; i < len; i++) + { + result.CStr[i + left] = CStr[i]; + } + return result; + } + + /// + /// SeqStringַ + /// + /// SeqStringַ + public override string ToString() + { + string str = string.Empty; + for (int i = 0, len = Length; i < len; i++) + str += CStr[i]; + return str; + } + + /// + /// ƥ + /// + /// ҪƥӴ + /// ƥʼλ + /// Ӵеλ,ڷ-1. + public int IndexOf(IString value, int startIndex = 0) + { + if (value == null || value.Length == 0) + throw new Exception("ƥַΪnull."); + if (startIndex < 0 || startIndex > value.Length - 1) + throw new ArgumentOutOfRangeException(); + + int len1 = Length; + int len2 = value.Length; + for (int i = startIndex; i <= len1 - len2; i++) + { + if (CStr[i] == value[0]) + { + int j; + for (j = 1; j < len2; j++) + { + if (CStr[j + i] != value[j]) + break; + } + if (j == len2) + return i; + } + } + return -1; + } + + /// + /// Ƴǰհַβհַ + /// + /// ӵǰĿʼĩβƳпհַַ + public IString Trim() + { + //Ƴǰհַβհַַ. + int left, right; + int len = Length; + for (left = 0; left < len; left++) + { + if (CStr[left] != ' ') + break; + } + if (left == len) + return new SeqString(); + + for (right = len - 1; right >= 0; right--) + { + if (CStr[right] != ' ') + break; + } + return SubString(left, right - left + 1); + } + } +} +``` + +ӣ + +```c +class Program +{ + static void Main(string[] args) + { + string S = "teacher and student all like tea."; + IString str = new SeqString(S); + Console.WriteLine("ԭַ:{0}", str); + // ԭַ: teacher and student all like tea. + + IString str2 = str; + IString str3 = str.Clone(); + str2[0] = 'm'; + Console.WriteLine("ûClone:{0}", str); + // ûClone: meacher and student all like tea. + Console.WriteLine("Clone:{0}", str3); + // Clone: teacher and student all like tea. + + str = new SeqString(S); + IString str4 = new SeqString("mother "); + Console.WriteLine(str.Insert(0, str4)); + // mother teacher and student all like tea. + Console.WriteLine(str.Insert(3, str4)); + // teamother cher and student all like tea. + + Console.WriteLine(str.Remove(3, 2)); + // teaer and student all like tea. + Console.WriteLine(str.Remove(3, 10)); + // teatudent all like tea. + + Console.WriteLine(str.Concat(str4)); + // teacher and student all like tea.mother + Console.WriteLine((SeqString) str4 + (SeqString) str); + // mother teacher and student all like tea. + + Console.WriteLine(str.SubString(3, 0)); + // + Console.WriteLine(str.SubString(3, 4)); + // cher + Console.WriteLine(str.SubString(3, 10)); + // cher and s + + IString parS1 = new SeqString("ea"); + IString parS2 = new SeqString("hk"); + Console.WriteLine(str.IndexOf(parS1)); + // 1 + Console.WriteLine(str.IndexOf(parS2)); + // -1 + + IString str5 = new SeqString(" a "); + Console.WriteLine(str5.Length); + // 6 + Console.WriteLine(str5.Trim().Length); + // 1 + } +``` + + +### 1.3 C# еַ + +ַΪC#һĬϵͣṩ˴ķԹԱʹá + +C# дַһDzɱַַstringһǿɱַַStringBuilder + +ӣɱַ + +```c +class Program +{ + static void Main(string[] args) + { + string str = "This is a string"; + string substr = str.Substring(0, 3); + Console.WriteLine(substr); + // Thi + string insertstr = str.Insert(3, "InsertString"); + Console.WriteLine(insertstr); + // ThiInsertStrings is a string + string removestr = str.Remove(3, 4); + Console.WriteLine(removestr); + // Thi a string + int length = str.Length; + Console.WriteLine(length); + // 16 + bool flag1 = (str == "This is a String") ? true : false; + Console.WriteLine(flag1); + // False + bool flag2 = (str != "This is a String") ? true : false; + Console.WriteLine(flag2); + // True + string catstr = str + "and other string"; + Console.WriteLine(catstr); + // This is a stringand other string + int index = str.IndexOf("is"); + Console.WriteLine(index); + // 2 + string replacestr = str.Replace("is", "IS"); + Console.WriteLine(replacestr); + // ThIS IS a string + char c = str[3]; + Console.WriteLine(c); + // s + // str[3] = 'c'; + // CS0200 ޷ΪԻstring.this[int]ֵ - ֻ + } +} +``` + +ӣɱַ + +```c +class Program +{ + static void Main(string[] args) + { + StringBuilder strb = new StringBuilder("abcdef"); + strb[0] = 'M'; + Console.WriteLine(strb); + // Mbcdef + } +} +``` + +## 2. + +### 2.1 ϵĶ + +**1ϵĶ** + +ɻͬɵļϣȫΪ 0 MaxRangeMaxRange ΪɸüԪصֵ + +**2ϵIJ** + +- 򼯺Ԫ +- ɾеԪ +- жԪǷڸü +- õеԪ +- ϵIJ +- ϵĽ +- ϵIJ +- 󼯺ϵIJ + +![ͼ4 Ͻӿ](https://img-blog.csdnimg.cn/20201021145404132.png) + +```c +namespace LinearStruct +{ + /// + /// ϵij + /// + /// Ԫص + public interface ISet + { + /// + /// 򼯺Ԫ + /// + /// Ҫ뵽ϵԪ + void Insert(T elt); + /// + /// ɾеԪ + /// + /// ҪɾļԪ + void Remove(T elt); + /// + /// жԪǷڸü + /// + /// ҪжϵļԪ + /// ڸüϷtrue,򷵻false. + bool IsMember(T elt); + /// + /// õеԪ + /// + /// ʾԪصַ. + string GetElements(); + /// + /// ϵIJ + /// + /// 󲢵ļ + /// ϵIJ + ISet Union(ISet b); + /// + /// ϵĽ + /// + /// 󽻵ļ + /// ϵĽ + ISet Intersect(ISet b); + /// + /// ϵIJ + /// + /// ļ + /// ϵIJ + ISet DiffSet(ISet b); + /// + /// 󼯺ϵIJ + /// + /// ϵIJ + ISet Complement(); + } +} +``` + +### 2.2 ϵĴ洢ʵ + +**1ʵ** + +Ϊ`$A_n=\{0,1,2,\cdots,n-1\}$` ӼҲڳΪ`$n$`С + +- ȫ`uint[] A = new uint[n];` Ԫȫֵ1 +- ռΪ`uint[] A = new uint[n];` Ԫȫֵ0 + +ӣ + +`$MaxRange=7$` + +`$I=\{0,1,2,3,4,5,6,7\},A=\{0,5,7\},B=\{2,5,6\}$` + +![ͼ5 洢](https://img-blog.csdnimg.cn/20201021153058228.png) + + +**2λʵ** + +ӣ + +`$MaxRange=7$` + +`$I=\{0,1,2,3,4,5,6,7\},A=\{0,5,7\},B=\{2,5,6\}$` + +![ͼ6 λ洢](https://img-blog.csdnimg.cn/20201021153240372.png) + + +![ͼ7 ͼ](https://img-blog.csdnimg.cn/20201021153453377.png) + +```c +using System; + +namespace LinearStruct +{ + /// + /// ʵּϵij + /// + public class IntSet : ISet //uint 32λ޷ + { + private readonly uint[] _bitSet;//λ + + /// + /// ȡеԪ,ȫΪ0MaxRange + /// + public uint MaxRange { get; } + + /// + /// ʼIntSetʵ + /// + /// еԪ + public IntSet(uint maxRge) + { + MaxRange = maxRge; + _bitSet = new uint[MaxRange / 32 + 1]; + for (int i = 0; i < _bitSet.Length; i++) + { + _bitSet[i] = 0;//ǰΪռ + } + } + + /// + /// ȡԪλе + /// + /// ȡλŵԪ + /// Ԫλе + private uint ArrayIndex(uint elt) + { + if (elt > MaxRange) + throw new ArgumentOutOfRangeException(); + + return elt / 32; + } + + /// + /// ȡԪضӦλõ + /// + /// ȡӦλԪ + /// ԪضӦλõ + private uint BitMask(uint elt) + { + if (elt > MaxRange) + throw new ArgumentOutOfRangeException(); + + return (uint)Math.Pow(2, elt % 32); + } + + /// + /// 򼯺Ԫ + /// + /// Ҫ뵽ϵԪ + public void Insert(uint elt) + { + if (elt > MaxRange) + throw new ArgumentOutOfRangeException(); + _bitSet[ArrayIndex(elt)] |= BitMask(elt); + } + + /// + /// ɾеԪ + /// + /// ҪɾļԪ + public void Remove(uint elt) + { + if (elt > MaxRange) + throw new ArgumentOutOfRangeException(); + + _bitSet[ArrayIndex(elt)] &= ~BitMask(elt); + } + + /// + /// жԪǷڸü + /// + /// ҪжϵļԪ + /// ڸüϷtrue,򷵻false. + public bool IsMember(uint elt) + { + if (elt > MaxRange) + return false; + uint i = _bitSet[ArrayIndex(elt)] & BitMask(elt); + return i != 0; + } + + /// + /// ȡӦַ + /// + /// Ӧַ + public string GetBitString() + { + string temp = string.Empty; + for (int i = 0; i < _bitSet.Length; i++) + { + temp = Convert.ToString(_bitSet[i], 2).PadLeft(32, '0') + temp; + } + return temp.Remove(0, 32 - (int)(MaxRange % 32 + 1)); + } + + /// + /// õеԪ + /// + /// ʾԪصַ. + public string GetElements() + { + string s = GetBitString(); + string temp = string.Empty; + int j = 0; + for (int i = s.Length - 1; i >= 0; i--) + { + if (s[i] == '1') + temp += j + " "; + j++; + } + return temp.Trim(); + } + + /// + /// ϵIJ + /// + /// 󲢵ļ + /// ϵIJ + public IntSet Union(IntSet b) + { + if (b == null) + throw new ArgumentNullException(); + if (b.MaxRange != MaxRange) + throw new Exception("ϷΧͬ."); + + IntSet temp = new IntSet(MaxRange); + + for (int i = 0; i < _bitSet.Length; i++) + { + temp._bitSet[i] = _bitSet[i] | b._bitSet[i]; + } + return temp; + } + + /// + /// ϵĽ + /// + /// 󽻵ļ + /// ϵĽ + public IntSet Intersect(IntSet b) + { + if (b == null) + throw new ArgumentNullException(); + if (MaxRange != b.MaxRange) + throw new Exception("ϷΧͬ."); + + IntSet temp = new IntSet(MaxRange); + for (int i = 0; i < _bitSet.Length; i++) + { + temp._bitSet[i] = _bitSet[i] & b._bitSet[i]; + } + return temp; + } + + /// + /// ϵIJ + /// + /// ļ + /// ϵIJ + public IntSet DiffSet(IntSet b) + { + if (b == null) + throw new ArgumentNullException(); + if (MaxRange != b.MaxRange) + throw new Exception("ϷΧͬ."); + + IntSet temp = new IntSet(MaxRange); + for (int i = 0; i < _bitSet.Length; i++) + { + temp._bitSet[i] = _bitSet[i] & (~b._bitSet[i]); + } + return temp; + } + + /// + /// 󼯺ϵIJ + /// + /// ϵIJ + public IntSet Complement() + { + IntSet temp = new IntSet(MaxRange); + for (int i = 0; i < _bitSet.Length; i++) + { + temp._bitSet[i] = ~_bitSet[i]; + } + return temp; + } + + ISet ISet.Union(ISet b) + { + return Union(b as IntSet); + } + + ISet ISet.Intersect(ISet b) + { + return Intersect(b as IntSet); + } + + ISet ISet.DiffSet(ISet b) + { + return DiffSet(b as IntSet); + } + + ISet ISet.Complement() + { + return Complement(); + } + } +} +``` + +ӣ + +```c +class Program +{ + static void Main(string[] args) + { + IntSet setA = new IntSet(33); + IntSet setB = new IntSet(33); + setA.Insert(5); + setA.Insert(30); + setA.Insert(23); + setB.Insert(5); + setB.Insert(24); + setB.Insert(9); + Console.WriteLine("A:{0};{1}", setA.GetBitString(), setA.GetElements()); + //A: 0001000000100000000000000000100000; 5 23 30 + + Console.WriteLine("B:{0};{1}", setB.GetBitString(), setB.GetElements()); + // B: 0000000001000000000000001000100000; 5 9 24 + + IntSet setC = setA.Union(setB); + Console.WriteLine("AB :{0}:{1}", setC.GetBitString(), setC.GetElements()); + // AB: 0001000001100000000000001000100000:5 9 23 24 30 + + setC = setA.Intersect(setB); + Console.WriteLine("AB :{0}:{1}", setC.GetBitString(), setC.GetElements()); + // AB: 0000000000000000000000000000100000:5 + + setC = setA.DiffSet(setB); + Console.WriteLine("AB :{0}:{1}", setC.GetBitString(), setC.GetElements()); + // AB: 0001000000100000000000000000000000:23 30 + + setC = setA.Complement(); + Console.WriteLine("AIJ:{0}:{1}", setC.GetBitString(), setC.GetElements()); + // AIJ: 1110111111011111111111111111011111:0 1 2 3 4 6 7 8 9 10 11 12 13 14 15 16 + // 17 18 19 20 21 22 24 25 26 27 28 29 31 32 33 + } +} +``` + +### 2.3 C# ϵӦ + +```c +namespace System.Drawing +{ + //ָӦõıϢ + public enum FontStyle + { + Regular = 0, // ͨı + Bold = 1, // Ӵı + Italic = 2, // бı + Underline = 4, // »ߵı + Strikeout = 8, // мֱͨı + } +} +``` + +ӣ +```c +FontStyle fs = FontStyle.Bold | FontStyle.Strikeout; +Font fnt = new Font("", 12f, fs); +this.label1.Font = fnt; +```