不定時、不定量、不特定API跳出**【System.InvalidOperationException】 - The connection was not closed. The connection’s current state is connecting.**錯誤,這個錯誤以前公司也遇到過,當時研判是Entity Framework與Unity套件生命週期的問題,當時專案不算太大又一直找不到確切解決方案,所以索性將Repository Layer整個用Dapper改寫讓問題得以解決了。
using (var scope = AutofacConfig.Container.BeginLifetimeScope()) { var Service = scope.Resolve<ISupplierApiProfileService>(); var Profile = Service.GetSupplierApiProfileByToken(token); }
Also called ‘transient’ or ‘factory’ in other containers. Using per-dependency scope, a unique instance will be returned from each request for a service.
// GET api/<controller> publicstringGet() { StringBuilder sb = new StringBuilder(); sb.AppendLine(string.Concat("PerDependencyModel ID :", this._lifeTimeModel.ID));
return sb.ToString(); } }
呼叫/api/test會發現隨著呼叫的次數增加,LifeTimeModel ID 也會一直增加,因為每次Request呼叫開始都會產生新的TestController Instance,隨著Request結束TestController也會被消滅,依賴在它身上的LifeTimeModel也隨之產生與消滅。而每次產生新的LifeTimeModel Instance都會在建構子將static的Count加1,這是數字一直增加的緣故。
Some application types naturally lend themselves to “request” type semantics, for example ASP.NET web forms and MVC applications. In these application types, it’s helpful to have the ability to have a sort of “singleton per request.”
publicclassSolution { publicstringFractionToDecimal(int numerator, int denominator) { } }
#解題
案例一
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[TestMethod] publicvoid FractionToDecimalTest_輸入numerator_1_denominator_2() { //arrange var sut = new Solution(); var numerator = 1; var denominator = 2;
var expected = "0.5"; //act var actual = sut.FractionToDecimal(numerator, denominator);
//assert actual.Should().Be(expected); }
1 2 3 4 5 6
publicstringFractionToDecimal(int numerator, int denominator) { decimal result = Convert.ToDecimal(numerator) / Convert.ToDecimal(denominator);
return result.ToString(); }
案例二
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[TestMethod] publicvoid FractionToDecimalTest_輸入numerator_2_denominator_1() { //arrange var sut = new Solution(); var numerator = 2; var denominator = 1;
var expected = "2"; //act var actual = sut.FractionToDecimal(numerator, denominator);
//assert actual.Should().Be(expected); }
直接通過測試
案例三
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[TestMethod] publicvoid FractionToDecimalTest_輸入numerator_2_denominator_3() { //arrange var sut = new Solution(); var numerator = 2; var denominator = 3;
var expected = "0.(6)"; //act var actual = sut.FractionToDecimal(numerator, denominator);
[TestMethod] publicvoid FractionToDecimalTest_輸入numerator_負2147483648_denominator_負1() { //arrange var sut = new Solution(); var numerator = -2147483648; var denominator = -1;
var expected = "2147483648"; //act var actual = sut.FractionToDecimal(numerator, denominator);
publicstringFractionToDecimal(int numerator, int denominator) { StringBuilder sb = new StringBuilder();
var _numerator = Convert.ToInt64(numerator); var _denominator = Convert.ToInt64(denominator); sb.Append(_numerator / _denominator);
//餘數 var Remainder = (_numerator % _denominator); if (Remainder == 0) { return sb.ToString(); }
sb.Append("."); var Map = new Dictionary<Int64, int>(); Map.Add(Remainder, sb.Length); while (Remainder != 0) { sb.Append((Remainder *10) / denominator); Remainder = (Remainder * 10) % denominator; if (Map.ContainsKey(Remainder)) { var Index = Map[Remainder]; //開始重複 sb.Insert(Index, "("); sb.Append(")"); break; }
Map.Add(Remainder, sb.Length); }
return sb.ToString(); }
負數問題
補上案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[TestMethod] publicvoid FractionToDecimalTest_輸入numerator_負50_denominator_8() { //arrange var sut = new Solution(); var numerator = -50; var denominator = 8; var expected = "-6.25"; //act var actual = sut.FractionToDecimal(numerator, denominator);
//只取正數 var _numerator =Math.Abs(Convert.ToInt64(numerator)); var _denominator = Math.Abs(Convert.ToInt64(denominator)); sb.Append(_numerator / _denominator);
//餘數 var Remainder = (_numerator % _denominator); if (Remainder == 0) { return sb.ToString(); }
sb.Append("."); var Map = new Dictionary<Int64, int>(); Map.Add(Remainder, sb.Length); while (Remainder != 0) { sb.Append((Remainder *10) / _denominator); Remainder = (Remainder * 10) % _denominator; if (Map.ContainsKey(Remainder)) { var Index = Map[Remainder]; //開始重複 sb.Insert(Index, "("); sb.Append(")"); break; }
Map.Add(Remainder, sb.Length); }
return sb.ToString(); }
被除數為0問題
當被除數為0時,回傳結果應為0且無關正負
補上案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[TestMethod] publicvoid FractionToDecimalTest_輸入numerator_0_denominator_負5() { //arrange var sut = new Solution(); var numerator = 0; var denominator = -5;
var expected = "0"; //act var actual = sut.FractionToDecimal(numerator, denominator);
<!-- Nav --> <navid="menu"> <ulclass="links"> <li><ahref="index.html">Home</a></li> <li><ahref="generic.html">Generic</a></li> <li><ahref="elements.html">Elements</a></li> </ul> </nav>
<!-- Banner --> <sectionclass="banner full"> <article> <imgsrc="images/slide01.jpg"alt="" /> <divclass="inner"> <header> <p>A free responsive web site template by <ahref="https://templated.co">TEMPLATED</a></p> <h2>Hielo</h2> </header> </div> </article> <article> <imgsrc="images/slide02.jpg"alt="" /> <divclass="inner"> <header> <p>Lorem ipsum dolor sit amet nullam feugiat</p> <h2>Magna etiam</h2> </header> </div> </article> <article> <imgsrc="images/slide03.jpg"alt="" /> <divclass="inner"> <header> <p>Sed cursus aliuam veroeros lorem ipsum nullam</p> <h2>Tempus dolor</h2> </header> </div> </article> <article> <imgsrc="images/slide04.jpg"alt="" /> <divclass="inner"> <header> <p>Adipiscing lorem ipsum feugiat sed phasellus consequat</p> <h2>Etiam feugiat</h2> </header> </div> </article> <article> <imgsrc="images/slide05.jpg"alt="" /> <divclass="inner"> <header> <p>Ipsum dolor sed magna veroeros lorem ipsum</p> <h2>Lorem adipiscing</h2> </header> </div> </article> </section>
<!-- One --> <sectionid="one"class="wrapper style2"> <divclass="inner"> <divclass="grid-style">
<div> <divclass="box"> <divclass="image fit"> <imgsrc="images/pic02.jpg"alt="" /> </div> <divclass="content"> <headerclass="align-center"> <p>maecenas sapien feugiat ex purus</p> <h2>Lorem ipsum dolor</h2> </header> <p> Cras aliquet urna ut sapien tincidunt, quis malesuada elit facilisis. Vestibulum sit amet tortor velit. Nam elementum nibh a libero pharetra elementum. Maecenas feugiat ex purus, quis volutpat lacus placerat malesuada.</p> <footerclass="align-center"> <ahref="#"class="button alt">Learn More</a> </footer> </div> </div> </div>
<div> <divclass="box"> <divclass="image fit"> <imgsrc="images/pic03.jpg"alt="" /> </div> <divclass="content"> <headerclass="align-center"> <p>mattis elementum sapien pretium tellus</p> <h2>Vestibulum sit amet</h2> </header> <p> Cras aliquet urna ut sapien tincidunt, quis malesuada elit facilisis. Vestibulum sit amet tortor velit. Nam elementum nibh a libero pharetra elementum. Maecenas feugiat ex purus, quis volutpat lacus placerat malesuada.</p> <footerclass="align-center"> <ahref="#"class="button alt">Learn More</a> </footer> </div> </div> </div>
</div> </div> </section>
<!-- Two --> <sectionid="two"class="wrapper style3"> <divclass="inner"> <headerclass="align-center"> <p>Nam vel ante sit amet libero scelerisque facilisis eleifend vitae urna</p> <h2>Morbi maximus justo</h2> </header> </div> </section>
<!-- Three --> <sectionid="three"class="wrapper style2"> <divclass="inner"> <headerclass="align-center"> <pclass="special">Nam vel ante sit amet libero scelerisque facilisis eleifend vitae urna</p> <h2>Morbi maximus justo</h2> </header> <divclass="gallery"> <div> <divclass="image fit"> <ahref="#"><imgsrc="images/pic01.jpg"alt="" /></a> </div> </div> <div> <divclass="image fit"> <ahref="#"><imgsrc="images/pic02.jpg"alt="" /></a> </div> </div> <div> <divclass="image fit"> <ahref="#"><imgsrc="images/pic03.jpg"alt="" /></a> </div> </div> <div> <divclass="image fit"> <ahref="#"><imgsrc="images/pic04.jpg"alt="" /></a> </div> </div> </div> </div> </section>
There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. They all look the same. If a pig drinks that poison it will die within 15 minutes. What is the minimum amount of pigs you need to figure out which bucket contains the poison within one hour.
Answer this question, and write an algorithm for the follow-up general case.
Follow-up:
If there are n buckets and a pig drinking poison will die within m minutes, how many pigs (x) you need to figure out the “poison” bucket within p minutes? There is exact one bucket with poison.
1 2 3 4 5
publicclassSolution { publicintPoorPigs(int buckets, int minutesToDie, int minutesToTest) { } }
[TestMethod] publicvoid PoorPigsTest_5桶水_1小時_毒發時間15分鐘_最少需要1隻豬來測試() { //arrange var sut = new Solution(); var bukets = 5; var minutesToDie = 15; var minutesToTest = 60;
var expected = 1; //act var actual = sut.PoorPigs(bukets, minutesToDie, minutesToTest);
//assert actual.Should().Be(expected);
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14
publicintPoorPigs(int buckets, int minutesToDie, int minutesToTest) { var OnePigCanTestBukets = (minutesToTest / minutesToDie) + 1;
var Pigs = 0; double TotalCanTestBukets = 0; while (TotalCanTestBukets < buckets) { Pigs++; TotalCanTestBukets = Math.Pow(OnePigCanTestBukets, Pigs); }
return Pigs; }
案例二
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
[TestMethod] publicvoid PoorPigsTest_6桶水_1小時_毒發時間15分鐘_最少需要2隻豬來測試() { //arrange var sut = new Solution(); var bukets = 6; var minutesToDie = 15; var minutesToTest = 60;
var expected = 2; //act var actual = sut.PoorPigs(bukets, minutesToDie, minutesToTest);
//assert actual.Should().Be(expected);
}
直接通過單元測試
#解析
LeetCode測試發生錯誤
補上案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
[TestMethod] publicvoid PoorPigsTest_1桶水_1分鐘測試時間_毒發時間1分鐘_最少需要0隻豬來測試() { //arrange var sut = new Solution(); var bukets = 1; var minutesToDie = 1; var minutesToTest = 1;
var expected = 0; //act var actual = sut.PoorPigs(bukets, minutesToDie, minutesToTest);
//assert actual.Should().Be(expected);
}
一桶水根本不需要測試,所以修改一下Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
publicintPoorPigs(int buckets, int minutesToDie, int minutesToTest) { var Pigs = 0; if (buckets <= 1) { return Pigs; }
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc", 3] =”abcabcabc”.
On the other hand, we define that string s1 can be obtained from string s2 if we can remove some characters from s2 such that it becomes s1. For example, “abc” can be obtained from “abdbec” based on our definition, but it can not be obtained from “acbbe”.
You are given two non-empty strings s1 and s2 (each at most 100 characters long) and two integers 0 ≤ n1 ≤ 106 and 1 ≤ n2 ≤ 106. Now consider the strings S1 and S2, where S1=[s1,n1] and S2=[s2,n2]. Find the maximum integer M such that [S2,M] can be obtained from S1.
publicintGetMaxRepetitions(string s1, int n1, string s2, int n2) { var S1Chars = s1.ToCharArray(); var S2Chars = s2.ToCharArray();
int S2Cursor = 0; int Repetitions = 0; var LogFirstLetterMatchInS1Index = new Dictionary<int, Tuple<int, int>>(); for (int i = 0; i < S1Chars.Length * n1 ; i++) { var S1Index = i % s1.Length; if (S1Chars[S1Index] == S2Chars[S2Cursor]) { if (S2Cursor == 0) { if (LogFirstLetterMatchInS1Index.ContainsKey(S1Index)) { //形成Loop了 var PreSameIndexValue = LogFirstLetterMatchInS1Index[S1Index];
//Repetitions var OneLoopS2Count = (Repetitions - PreSameIndexValue.Item2);
var OneLoopDistance = i - PreSameIndexValue.Item1;
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another computer environment.
Design an algorithm to serialize and deserialize a binary search tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary search tree can be serialized to a string and this string can be deserialized to the original tree structure.
The encoded string should be as compact as possible.
Note: Do not use class member/global/static variables to store states. Your serialize and deserialize algorithms should be stateless.
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNode left; * public TreeNode right; * public TreeNode(int x) { val = x; } * } */ publicclassCodec {
// Encodes a tree to a single string. publicstringserialize(TreeNode root) { }
// Decodes your encoded data to tree. public TreeNode deserialize(string data) { } }
// Your Codec object will be instantiated and called as such: // Codec codec = new Codec(); // codec.deserialize(codec.serialize(root));
#解題
serialize
案例一
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[TestMethod()] publicvoidserializeTest() { //arrange var node = new TreeNode(1); var sut = new Codec();
[TestMethod()] publicvoiddeserializeTest() { //arrange var nodeSerializeString = @"1!#!#!"; var sut = new Codec(); var expected = new TreeNode(1); //act var actual = sut.deserialize(nodeSerializeString);
string res = root.val + "!"; res += serialize(root.left); res += serialize(root.right); return res; }
// Decodes your encoded data to tree. public TreeNode deserialize(string data) { var nodeData = data.Split(newstring[] { "!" }, StringSplitOptions.RemoveEmptyEntries);
var queue = new Queue<string>();
foreach (var node in nodeData) { queue.Enqueue(node); }
return RebuildNode(queue); }
private TreeNode RebuildNode(Queue<string> queue) { var Value = queue.Dequeue();
Given two strings s and t which consist of only lowercase letters. String t is generated by random shuffling string s and then add one more letter at a random position. Find the letter that was added in t.
publiccharFindTheDifference(string s, string t) { var originalString = s.ToCharArray();
var addSomeSaltString = t.ToCharArray();
for (int i = 0; i < addSomeSaltString.Length; i++) { var checkThisChar = addSomeSaltString[i]; if (i > originalString.Length -1) { return checkThisChar; }
if (originalString[i] != checkThisChar) { return checkThisChar; } }
return'\0'; }
#解析
LeetCode跑測試錯誤
補上錯誤案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
[TestMethod()] publicvoidFindTheDifferenceTest_LeetCode_Error1() { //arrange var sut = new Solution(); var S = "ymbgaraibkfmvocpizdydugvalagaivdbfsfbepeyccqfepzvtpyxtbadkhmwmoswrcxnargtlswqemafandgkmydtimuzvjwxvlfwlhvkrgcsithaqlcvrihrwqkpjdhgfgreqoxzfvhjzojhghfwbvpfzectwwhexthbsndovxejsntmjihchaotbgcysfdaojkjldprwyrnischrgmtvjcorypvopfmegizfkvudubnejzfqffvgdoxohuinkyygbdzmshvyqyhsozwvlhevfepdvafgkqpkmcsikfyxczcovrmwqxxbnhfzcjjcpgzjjfateajnnvlbwhyppdleahgaypxidkpwmfqwqyofwdqgxhjaxvyrzupfwesmxbjszolgwqvfiozofncbohduqgiswuiyddmwlwubetyaummenkdfptjczxemryuotrrymrfdxtrebpbjtpnuhsbnovhectpjhfhahbqrfbyxggobsweefcwxpqsspyssrmdhuelkkvyjxswjwofngpwfxvknkjviiavorwyfzlnktmfwxkvwkrwdcxjfzikdyswsuxegmhtnxjraqrdchaauazfhtklxsksbhwgjphgbasfnlwqwukprgvihntsyymdrfovaszjywuqygpvjtvlsvvqbvzsmgweiayhlubnbsitvfxawhfmfiatxvqrcwjshvovxknnxnyyfexqycrlyksderlqarqhkxyaqwlwoqcribumrqjtelhwdvaiysgjlvksrfvjlcaiwrirtkkxbwgicyhvakxgdjwnwmubkiazdjkfmotglclqndqjxethoutvjchjbkoasnnfbgrnycucfpeovruguzumgmgddqwjgdvaujhyqsqtoexmnfuluaqbxoofvotvfoiexbnprrxptchmlctzgqtkivsilwgwgvpidpvasurraqfkcmxhdapjrlrnkbklwkrvoaziznlpor";
var T = "qhxepbshlrhoecdaodgpousbzfcqjxulatciapuftffahhlmxbufgjuxstfjvljybfxnenlacmjqoymvamphpxnolwijwcecgwbcjhgdybfffwoygikvoecdggplfohemfypxfsvdrseyhmvkoovxhdvoavsqqbrsqrkqhbtmgwaurgisloqjixfwfvwtszcxwktkwesaxsmhsvlitegrlzkvfqoiiwxbzskzoewbkxtphapavbyvhzvgrrfriddnsrftfowhdanvhjvurhljmpxvpddxmzfgwwpkjrfgqptrmumoemhfpojnxzwlrxkcafvbhlwrapubhveattfifsmiounhqusvhywnxhwrgamgnesxmzliyzisqrwvkiyderyotxhwspqrrkeczjysfujvovsfcfouykcqyjoobfdgnlswfzjmyucaxuaslzwfnetekymrwbvponiaojdqnbmboldvvitamntwnyaeppjaohwkrisrlrgwcjqqgxeqerjrbapfzurcwxhcwzugcgnirkkrxdthtbmdqgvqxilllrsbwjhwqszrjtzyetwubdrlyakzxcveufvhqugyawvkivwonvmrgnchkzdysngqdibhkyboyftxcvvjoggecjsajbuqkjjxfvynrjsnvtfvgpgveycxidhhfauvjovmnbqgoxsafknluyimkczykwdgvqwlvvgdmufxdypwnajkncoynqticfetcdafvtqszuwfmrdggifokwmkgzuxnhncmnsstffqpqbplypapctctfhqpihavligbrutxmmygiyaklqtakdidvnvrjfteazeqmbgklrgrorudayokxptswwkcircwuhcavhdparjfkjypkyxhbgwxbkvpvrtzjaetahmxevmkhdfyidhrdeejapfbafwmdqjqszwnwzgclitdhlnkaiyldwkwwzvhyorgbysyjbxsspnjdewjxbhpsvj";
publicclassSolution { public IList<IList<int>> PermuteUnique(int[] nums) { } }
#解題
案例一
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
[TestMethod] publicvoid PermuteUniqueTest_輸入1_1() { //arrange var sut = new Solution(); int[] input = newint []{ 1, 1 }; var expected = new List<List<int>> { new List<int>{ 1,1} }; //act var actual = sut.PermuteUnique(input);
publicpartialclassSolution { public IList<IList<int>> PermuteUnique(int[] nums) { var Result = new List<IList<int>>(); Result.Add(nums.ToList()); return Result; } }
案例二
1 2 3 4 5 6 7 8 9 10 11 12 13 14
[TestMethod] publicvoid PermuteUniqueTest_輸入空Array() { //arrange var sut = new Solution(); int[] input = newint[0]; var expected = new List<List<int>>();
public IList<IList<int>> PermuteUnique(int[] nums) { var Result = new List<IList<int>>();
if (nums.Length == 0) return Result;
Result.Add(nums.ToList());
return Result; }
案例三
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
[TestMethod] publicvoid PermuteUniqueTest_輸入1_2() { //arrange var sut = new Solution(); int[] input = newint[] { 1, 2 }; var expected = new List<List<int>> { new List<int>{ 1,2}, new List<int>{ 2,1} };
[TestMethod] publicvoid PermuteUniqueTest_輸入1_2_3() { //arrange var sut = new Solution(); int[] input = newint[] { 1, 2 , 3}; var expected = new List<List<int>> { new List<int>{ 1,2,3}, new List<int>{ 1,3,2}, new List<int>{ 2,1,3}, new List<int>{ 2,3,1}, new List<int>{ 3,1,2}, new List<int>{ 3,2,1}, };
publicpartialclassSolution { public IList<IList<int>> PermuteUnique(int[] nums) { var Result = new List<IList<int>>();
if (nums.Length == 0) return Result;
Result.Add(nums.ToList());
for (int i = 0; i < nums.Length; i++) { if (i == nums.Length - 1) continue;
for (int s = i + 1 ; s < nums.Length; s++) { if (nums[i] != nums[s]) { var SplitLeaft = new List<int>(nums); Swap(SplitLeaft, i, s); Recursive(SplitLeaft, i + 1, Result); } } }
return Result; }
privatevoidRecursive(List<int> nums, int start, List<IList<int>> result) { result.Add(nums); for (int i = start; i < nums.Count; i++) { if (i == nums.Count - 1) continue;
if (nums[start] != nums[i + 1]) { var SplitLeaft = new List<int>(nums); Swap(SplitLeaft, start, i + 1); Recursive(SplitLeaft, i + 1, result); } } }
privatevoidSwap(List<int> nums, int index1,int index2) { var Temp = nums[index1]; nums[index1] = nums[index2]; nums[index2] = Temp; } }
publicpartialclassSolution { public IList<IList<int>> PermuteUnique(int[] nums) { var Result = new List<IList<int>>();
if (nums.Length == 0) return Result;
Recursive(new List<int>(nums), 0 , Result);
return Result; }
privatevoidRecursive(List<int> nums, int start, List<IList<int>> result) { result.Add(nums); for (int i = start; i < nums.Count; i++) { if (i == nums.Count - 1) { continue; }
for (int j = i + 1; j < nums.Count; j++) { if (nums[i] != nums[j]) { var SplitLeaft = new List<int>(nums); Swap(SplitLeaft, i, j); Recursive(SplitLeaft, i + 1, result); } } } }
privatevoidSwap(List<int> nums, int index1,int index2) { var Temp = nums[index1]; nums[index1] = nums[index2]; nums[index2] = Temp; } }
案例五
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
[TestMethod] publicvoid PermuteUniqueTest_輸入1_1_2() { //arrange var sut = new Solution(); int[] input = newint[] { 1, 1, 2 }; var expected = new List<List<int>> { new List<int>{ 1,1,2}, new List<int>{ 1,2,1}, new List<int>{ 2,1,1} };
[TestMethod] publicvoid PermuteUniqueTest_輸入2_2_1_1() { //arrange var sut = new Solution(); int[] input = newint[] { 2, 2, 1, 1 }; var expected = new List<List<int>> { new List<int>{ 1,1,2,2}, new List<int>{ 1,2,1,2}, new List<int>{ 1,2,2,1}, new List<int>{ 2,1,1,2}, new List<int>{ 2,1,2,1}, new List<int>{ 2,2,1,1} };
public IList<IList<int>> PermuteUnique(int[] nums) { var Result = new List<IList<int>>();
if (nums.Length == 0) return Result;
Recursive(new List<int>(nums), 0 , Result);
return Result; }
privatevoidRecursive(List<int> nums, int start, List<IList<int>> result) { result.Add(nums); for (int i = start; i < nums.Count; i++) { HashSet<int> used = new HashSet<int>(); if (i == nums.Count - 1) { continue; }
for (int j = i + 1; j < nums.Count; j++) { if (nums[i] != nums[j]) { if (used.Contains(nums[j])) { continue; } else { used.Add(nums[j]); }
var SplitLeaft = new List<int>(nums); Swap(SplitLeaft, i, j); Recursive(SplitLeaft, i + 1, result); } } } }
privatevoidSwap(List<int> nums, int index1,int index2) { var Temp = nums[index1]; nums[index1] = nums[index2]; nums[index2] = Temp; }