├── LeetCode ├── TreeNode.cs ├── Interval.cs ├── TreeLinkNode.cs ├── ListNode.cs ├── 1001-1050 │ ├── HashMap.cs │ ├── 1009-ComplementOfBase10Integer.cs │ └── 1029-TwoCityScheduling.cs ├── 0251-0300 │ ├── 0292-NimGame.cs │ ├── 0258-AddDigits.cs │ ├── 0268-MissingNumber.cs │ ├── 0263-UglyNumber.cs │ ├── 0283-MoveZeroes.cs │ ├── 0280-WiggleSort.cs │ ├── 0252-MeetingRooms.cs │ └── 0274-HIndex.cs ├── 0701-0750 │ └── 0709-ToLowerCase.cs ├── 0301-0350 │ ├── 0326-PowerOfThree.cs │ ├── 0342-PowerOfFour.cs │ └── 0344-ReverseString.cs ├── 0351-0400 │ ├── 0371-SumOfTwoIntegers.cs │ ├── 0389-FindTheDifference.cs │ ├── 0392-IsSubsequence.cs │ └── 0387-FirstUniqueCharacterInAString.cs ├── 1251-1300 │ ├── 1256-EncodeNumber.cs │ └── 1295-FindNumbersWithEvenNumberOfDigits.cs ├── 0401-0450 │ ├── 0441-ArrangingCoins.cs │ └── 0434-NumberOfSegmentsInAString.cs ├── 1201-1250 │ ├── 1227-AirplaneSeatAssignmentProbability.cs │ ├── 1217-PlayWithChips.cs │ └── 1221-SplitAStringInBalancedStrings.cs ├── 0201-0250 │ ├── 0231-PowerOfTwo.cs │ ├── 0237-DeleteNodeInALinkedList.cs │ ├── 0201-BitwiseANDOfNumbersRange.cs │ ├── 0226-InvertBinaryTree.cs │ └── 0217-ContainsDuplicate.cs ├── 1151-1200 │ └── 1185-DayOfTheWeek.cs ├── 0101-0150 │ ├── 0136-SingleNumber.cs │ ├── 104-MaximumDepthOfBinaryTree.cs │ ├── 114-FlattenBinaryTreeToLinkedList.cs │ ├── 112-PathSum.cs │ ├── 0122-BestTimeToBuyAndSellStock2.cs │ ├── 0137-SingleNumberII.cs │ └── 111-MinimumDepthOfBinaryTree.cs ├── 0451-0500 │ ├── 0458-PoorPigs.cs │ ├── 0476-NumberComplement.cs │ ├── 0492-ConstructTheRectangle.cs │ ├── 0453-MinimumMovesToEqualArrayElements.cs │ ├── 0461-HammingDistance.cs │ └── 0495-TeemoAttacking.cs ├── 0501-0550 │ ├── 0521-LongestUncommonSubsequenceI.cs │ ├── 0518-CoinChange2.cs │ └── 0520-DetectCapital.cs ├── 0551-0600 │ ├── 0557-ReverseWordsInAStringIII.cs │ ├── 0575-DistributeCandies.cs │ ├── 0561-ArrayPartitionI.cs │ └── 0598-RangeAdditionII.cs ├── 1451-1500 │ ├── 1486-XOROperationInAnArray.cs │ ├── 1480-RunningSumOf1DArray.cs │ └── 1470-ShuffleTheArray.cs ├── 0151-0200 │ ├── 0191-NumberOf1Bits.cs │ ├── 0172-FactorialTrailingZeroes.cs │ ├── 0171-ExcelSheetColumnNumber.cs │ ├── 0151-ReverseWordsInAString.cs │ ├── 0162-FindPeakElement.cs │ ├── 0169-MajorityElement.cs │ └── 0190-ReverseBits.cs ├── 0051-0100 │ ├── 096-UniqueBinarySearchTree.cs │ ├── 100-SameTree.cs │ ├── 069-Sqrt(x).cs │ └── 070-ClimbingStairs.cs ├── 1351-1400 │ ├── 1374-GenerateAStringWithCharactersThatHaveOddCounts.cs │ ├── 1389-CreateTargetArrayInTheGivenOrder.cs │ └── 1375-BulbSwitcherIII.cs ├── 0801-0850 │ ├── 0836-RectangleOverlap.cs │ └── 0829-ConsecutiveNumbersSum.cs ├── 0001-0050 │ ├── 050-Pow.cs │ └── 027-RemoveElement.cs ├── 1101-1150 │ ├── 1150-CheckIfANumberIsMajorityElementInASortedArray.cs │ └── 1118-NumberOfDaysInAMonth.cs ├── 0751-0800 │ └── 0760-FindAnagramMappings.cs ├── 0851-0900 │ └── 0876-MiddleOfTheLinkedList.cs ├── 1051-1100 │ └── 1085-SumOfDigitsInTheMinimumNumber.cs ├── 0951-1000 │ └── 0976-LargestPerimeterTriangle.cs ├── 1301-1350 │ ├── 1342-NumberOfStepsToReduceANumberToZero.cs │ ├── 1317-ConvertIntegerToTheSumOfTwoNoZeroIntegers.cs │ ├── 1313-DecompressRunLengthEncodedList.cs │ └── 1332-RemovePalindromicSubsequences.cs ├── 0901-0950 │ └── 0941-ValidMountainArray.cs └── 0601-0650 │ └── 0617-MergeTwoBinaryTrees.cs └── LeetCode.Test ├── 0451-0500 ├── 0458-PoorPigs-Test.cs ├── 0461-HammingDistance-Test.cs ├── 0492-ConstructTheRectangle-Test.cs ├── 0485-MaxConsecutiveOnes-Test.cs ├── 0500-KeyboardRow-Test.cs ├── 0470-ImplementRand10UsingRand7-Test.cs ├── 0463-IslandPerimeter-Test.cs ├── 0498-DiagonalTraverse-Test.cs ├── 0476-NumberComplement-Test.cs ├── 0475-Heaters-Test.cs └── 0495-TeemoAttacking-Test.cs ├── 0201-0250 ├── 0202-HappyNumber-Test.cs ├── 0209-MinimumSizeSubarraySum-Test.cs ├── 0239-SlidingWindowMaximum-Test.cs ├── 0206-ReverseLinkedList-Test.cs ├── 0226-InvertBinaryTree-Test.cs ├── 0203-RemoveLinkedListElements-Test.cs ├── 0225-ImplementStackUsingQueues-Test.cs ├── 0232-ImplementQueueUsingStacks-Test.cs ├── 0208-ImplementTrie-Test.cs ├── 0204-CountPrimes-Test.cs ├── 0242-ValidAnagram-Test.cs ├── 0201-BitwiseANDOfNumbersRange-Test.cs ├── 0207-CourseSchedule-Test.cs └── 0240-SearchA2DMatrixII-Test.cs ├── 0251-0300 ├── 0274-HIndex-Test.cs ├── 0276-PaintFence-Test.cs ├── 0275-HIndexII-Test.cs ├── 0278-FirstBadVersion-Test.cs ├── 0260-SingleNumberIII-Test.cs ├── 0280-WiggleSort-Test.cs ├── 0271-EncodeAndDecodeStrings-Test.cs ├── 0256-PaintHouse-Test.cs ├── 0295-FindMedianFromDataStream-Test.cs ├── 0297-SerializeandDeserializeBinaryTree-Test.cs └── 0258-AddDigits-Test.cs ├── 0751-0800 ├── 0788-RotatedDigits-Test.cs ├── 0791-CustomSortString-Test.cs ├── 0751-IPToCIDR-Test.cs ├── 0763-PartitionLabels-Test.cs ├── 0760-FindAnagramMappings-Test.cs ├── 0796-RotateString-Test.cs ├── 0800-SimilarRGBColor-Test.cs ├── 0771-JewelsAndStones-Test.cs ├── 0777-SwapAdjacentInLRString-Test.cs └── 0765-CouplesHoldingHands-Test.cs ├── 0851-0900 ├── 0877-StoneGame-Test.cs ├── 0890-FindAndReplacePattern-Test.cs ├── 0861-ScoreAfterFlippingMatrix-Test.cs ├── 0865-SmallestSubtreeWithAllTheDeepestNodes-Test.cs ├── 0889-ConstructBinaryTreeFromPreorderAndPostorderTraversal-Test.cs ├── 0897-IncreasingOrderSearchTree-Test.cs └── 0895-MaximumFrequencyStack-Test.cs ├── 0501-0550 ├── 0507-PerfectNumber-Test.cs ├── 0503-NextGreaterElementII-Test.cs ├── 0506-RelativeRanks-Test.cs ├── 0543-DiameterOfBinaryTree-Test.cs ├── 0535-EncodeAndDecodeTinyURL-Test.cs ├── 0501-FindModeInBinarySearchTree-Test.cs ├── 0538-ConvertBSTToGreaterTree-Test.cs ├── 0515-FindLargestValueInEachTreeRow-Test.cs ├── 0541-ReverseStringII-Test.cs └── 0523-ContinuousSubarraySum-Test.cs ├── 0301-0350 ├── 0312-BurstBalloons-Test.cs ├── 0327-CountOfRangeSum-Test.cs ├── 0347-TopKFrequentElements-Test.cs ├── 0309-BestTimeToBuyAndSellStockWithCooldown-Test.cs ├── 0303-RangeSumQueryImmutable-Test.cs ├── 0317-ShortestDistanceFromAllBuildings-Test.cs ├── 0342-PowerOfFour-Test.cs ├── 0322-CoinChange-Test.cs ├── 0348-DesignTicTacToe-Test.cs ├── 0338-CountingBits-Test.cs └── 0346-MovingAverageFromDataStream-Test.cs ├── 1101-1150 ├── 1140-StoneGameII-Test.cs ├── 1120-MaximumAverageSubtree-Test.cs ├── 1122-RelativeSortArray-Test.cs ├── 1128-NumberOfEquivalentDominoPairs-Test.cs ├── 1134-ArmstrongNumber-Test.cs ├── 1137-NThTribonacciNumber-Test.cs └── 1108-DefangingAnIPAddress-Test.cs ├── 0551-0600 ├── 0561-ArrayPartitionI-Test.cs ├── 0594-LongestHarmoniousSubsequence-Test.cs ├── 0563-BinaryTreeTilt-Test.cs ├── 0557-ReverseWordsInAStringIII-Test.cs ├── 0581-ShortestUnsortedContinuousSubarray-Test.cs ├── 0551-StudentAttendanceRecordI-Test.cs ├── 0567-PermutationInString-Test.cs ├── 0560-SubarraySumEqualsK-Test.cs ├── 0588-DesignInMemoryFileSystem-Test.cs └── 0575-DistributeCandies-Test.cs ├── 0601-0650 ├── 0645-SetMismatch-Test.cs ├── 0621-TaskScheduler-Test.cs ├── 0636-ExclusiveTimeOfFunctions-Test.cs ├── 0637-AverageOfLevelsInBinaryTree-Test.cs ├── 0617-MergeTwoBinaryTrees-Test.cs ├── 0624-MaximumDistanceInArrays-Test.cs ├── 0647-PalindromicSubstrings-Test.cs └── 0605-CanPlaceFlowers-Test.cs ├── 0651-0700 ├── 0686-RepeatedStringMatch-Test.cs ├── 0654-MaximumBinaryTree-Test.cs ├── 0657-RobotReturnToOrigin-Test.cs ├── 0683-KEmptySlots-Test.cs ├── 0665-NonDecreasingArray-Test.cs ├── 0682-BaseballGame-Test.cs ├── 0696-CountBinarySubstrings-Test.cs └── 0697-DegreeOfAnArray-Test.cs ├── 0401-0450 ├── 0434-NumberOfSegmentsInAString-Test.cs ├── 0421-MaximumXOROfTwoNumbersInAnArray-Test.cs ├── 0401-BinaryWatch-Test.cs ├── 0412-FizzBuzz-Test.cs ├── 0442-FindAllDuplicatesInAnArray-Test.cs ├── 0404-SumOfLeftLeaves-Test.cs ├── 0448-FindAllNumbersDisappearedInAnArray-Test.cs ├── 0450-DeleteNodeInABST-Test.cs ├── 0447-NumberOfBoomerangs-Test.cs ├── 0419-BattleshipsInABoard-Test.cs ├── 0441-ArrangingCoins-Test.cs ├── 0415-AddStrings-Test.cs └── 0403-FrogJump-Test.cs ├── 1001-1050 ├── 1048-LongestStringChain-Test.cs ├── 1047-RemoveAllAdjacentDuplicatesInString-Test.cs ├── 1043-PartitionArrayForMaximumSum-Test.cs ├── 1008-ConstructBinarySearchTreeFromPreorderTraversal-Test.cs ├── 1026-MaximumDifferenceBetweenNodeAndAncestor-Test.cs ├── 1029-TwoCityScheduling-Test.cs ├── 1046-LastStoneWeight-Test.cs └── 1044-LongestDuplicateSubstring-Test.cs ├── 0901-0950 ├── 0922-SortArrayByParityII-Test.cs ├── 0929-UniqueEmailAddresses-Test.cs ├── 0950-RevealCardsInIncreasingOrder-Test.cs ├── 0931-MinimumFallingPathSum-Test.cs ├── 0933-NumberOfRecentCalls-Test.cs └── 0901-OnlineStockSpan-Test.cs ├── 0701-0750 ├── 0713-SubarrayProductLessThanK-Test.cs ├── 0739-DailyTemperatures-Test.cs ├── 0743-NetworkDelayTime-Test.cs ├── 0715-RangeModule-Test.cs ├── 0705-DesignHashset-Test.cs ├── 0706-DesignHashmap-Test.cs ├── 0703-KthLargestElementInAStream-Test.cs ├── 0704-BinarySearch-Test.cs └── 0717-1BitAnd2BitCharacters-Test.cs ├── 0801-0850 ├── 0821-ShortestDistanceToACharacter-Test.cs ├── 0843-GuesstheWord-Test.cs ├── 0812-LargestTriangleArea-Test.cs ├── 0807-MaxIncreaseToKeepCitySkyline-Test.cs └── 0846-HandOfStraights-Test.cs ├── 0151-0200 ├── 0199-BinaryTreeRightSideView-Test.cs ├── 0174-DungeonGame-Test.cs ├── 0172-FactorialTrailingZeroes-Test.cs ├── 0190-ReverseBits-Test.cs ├── 0179-LargestNumber-Test.cs ├── 0155-MinStack-Test.cs ├── 0167-TwoSumII-Test.cs ├── 0162-FindPeakElement-Test.cs └── 0169-MajorityElement-Test.cs ├── 1251-1300 ├── 1287-ElementAppearingMoreThan25InSortedArray-Test.cs ├── 1299-ReplaceElementsWithGreatestElementOnRightSide-Test.cs ├── 1256-EncodeNumber-Test.cs ├── 1271-Hexspeak-Test.cs └── 1286-IteratorForCombination-Test.cs ├── 1151-1200 ├── 1161-MaximumLevelSumOfABinaryTree-Test.cs ├── 1197-MinimumKnightMoves-Test.cs ├── 1175-PrimeArrangements-Test.cs └── 1165-SingleRowKeyboard-Test.cs ├── 1301-1350 ├── 1302-DeepestLeavesSum-Test.cs └── 1315-SumOfNodesWithEvenValuedGrandparent-Test.cs ├── 1201-1250 ├── 1213-IntersectionOfThreeSortedArrays-Test.cs └── 1217-PlayWithChips-Test.cs ├── 1351-1400 └── 1382-BalanceABinarySearchTree-Test.cs ├── 0351-0400 ├── 0370-RangeAddition-Test.cs ├── 0392-IsSubsequence-Test.cs ├── 0393-UTF8Validation-Test.cs ├── 0374-GuessNumberHigherOrLower-Test.cs ├── 0366-FindLeavesOfBinaryTree-Test.cs ├── 0389-FindTheDifference-Test.cs ├── 0375-GuessNumberHigherOrLowerII-Test.cs └── 0387-FirstUniqueCharacterInAString-Test.cs ├── 0101-0150 ├── 0131-PalindromePartitioning-Test.cs ├── 0136-SingleNumber-Test.cs ├── 0137-SingleNumberII-Test.cs └── 0121-BestTimeToBuyAndSellStock-Test.cs ├── 1451-1500 └── 1496-PathCrossing-Test.cs ├── 0951-1000 ├── 0951-FlipEquivalentBinaryTrees-Test.cs ├── 0985-SumOfEvenNumbersAfterQueries-Test.cs └── 0969-PancakeSorting-Test.cs ├── 0051-0100 └── 096-UniqueBinarySearchTree-Test.cs └── 1051-1100 ├── 1079-LetterTilePossibilities-Test.cs └── 1099-TwoSumLessThanK-Test.cs /LeetCode/TreeNode.cs: -------------------------------------------------------------------------------- 1 | namespace LeetCode 2 | { 3 | public class TreeNode 4 | { 5 | public int val; 6 | public TreeNode left; 7 | public TreeNode right; 8 | 9 | public TreeNode(int x) { val = x; } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /LeetCode/Interval.cs: -------------------------------------------------------------------------------- 1 | namespace LeetCode 2 | { 3 | public class Interval 4 | { 5 | public int start; 6 | public int end; 7 | public Interval() { start = 0; end = 0; } 8 | public Interval(int s, int e) { start = s; end = e; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /LeetCode/TreeLinkNode.cs: -------------------------------------------------------------------------------- 1 | namespace LeetCode 2 | { 3 | public class TreeLinkNode 4 | { 5 | public int val; 6 | public TreeLinkNode left; 7 | public TreeLinkNode right; 8 | public TreeLinkNode next; 9 | 10 | public TreeLinkNode(int x) { val = x; } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /LeetCode/ListNode.cs: -------------------------------------------------------------------------------- 1 | namespace LeetCode 2 | { 3 | public class ListNode 4 | { 5 | public int val; 6 | public ListNode next; 7 | public ListNode(int x) { val = x; } 8 | public ListNode(int val = 0, ListNode next = null) 9 | { 10 | this.val = val; 11 | this.next = next; 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /LeetCode/1001-1050/HashMap.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 3 | // Memory Usage: 4 | // Link: 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | internal class HashMap 10 | { 11 | public HashMap() 12 | { 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /LeetCode.Test/0451-0500/0458-PoorPigs-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0458_PoorPigs_Test 7 | { 8 | [TestMethod] 9 | public void PoorPigsTest() 10 | { 11 | var solution = new _0458_PoorPigs(); 12 | Assert.AreEqual(2, solution.PoorPigs(3, 15, 15)); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /LeetCode.Test/0201-0250/0202-HappyNumber-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0202_HappyNumber_Test 7 | { 8 | [TestMethod] 9 | public void IsHappyTest() 10 | { 11 | var solution = new _0202_HappyNumber(); 12 | var result = solution.IsHappy(19); 13 | Assert.IsTrue(result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0251-0300/0274-HIndex-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0274_HIndex_Test 7 | { 8 | [TestMethod] 9 | public void HIndex_1() 10 | { 11 | var solution = new _0274_HIndex(); 12 | var result = solution.HIndex(new int[] { 3,0,6,1,5}); 13 | Assert.AreEqual(3, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0251-0300/0276-PaintFence-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0276_PaintFence_Test 7 | { 8 | [TestMethod] 9 | public void NumWays_1() 10 | { 11 | var solution = new _0276_PaintFence(); 12 | var result = solution.NumWays(3, 2); 13 | Assert.AreEqual(6, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0251-0300/0275-HIndexII-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0275_HIndexII_Test 7 | { 8 | [TestMethod] 9 | public void HIndex_1() 10 | { 11 | var solution = new _0275_HIndexII(); 12 | var result = solution.HIndex(new int[] { 0, 1, 3, 5, 6 }); 13 | Assert.AreEqual(3, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0751-0800/0788-RotatedDigits-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0788_RotatedDigits_Test 7 | { 8 | [TestMethod] 9 | public void RotatedDigits_1() 10 | { 11 | var solution = new _0788_RotatedDigits(); 12 | var result = solution.RotatedDigits(10); 13 | Assert.AreEqual(4, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0851-0900/0877-StoneGame-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0877_StoneGame_Test 7 | { 8 | [TestMethod] 9 | public void StoneGame_1() 10 | { 11 | var solution = new _0877_StoneGame(); 12 | var result = solution.StoneGame(new int[] { 5, 3, 4, 5 }); 13 | Assert.IsTrue(result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0501-0550/0507-PerfectNumber-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0507_PerfectNumber_Test 7 | { 8 | [TestMethod] 9 | public void CheckPerfectNumber_1() 10 | { 11 | var solution = new _0507_PerfectNumber(); 12 | var result = solution.CheckPerfectNumber(28); 13 | Assert.IsTrue(result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0251-0300/0278-FirstBadVersion-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0278_FirstBadVersion_Test 7 | { 8 | [TestMethod] 9 | public void FirstBadVersion_1() 10 | { 11 | var solution = new _0278_FirstBadVersion(); 12 | var result = solution.FirstBadVersion(5); 13 | Assert.AreEqual(4, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0451-0500/0461-HammingDistance-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0461_HammingDistance_Test 7 | { 8 | [TestMethod] 9 | public void HammingDistanceTest() 10 | { 11 | var solution = new _0461_HammingDistance(); 12 | var result = solution.HammingDistance(1, 4); 13 | Assert.AreEqual(2, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0301-0350/0312-BurstBalloons-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0312_BurstBalloons_Test 7 | { 8 | [TestMethod] 9 | public void MaxCoinsTest() 10 | { 11 | var solution = new _0312_BurstBalloons(); 12 | var result = solution.MaxCoins(new int[] { 3, 1, 5, 8 }); 13 | Assert.AreEqual(167, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/1101-1150/1140-StoneGameII-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1140_StoneGameII_Test 7 | { 8 | [TestMethod] 9 | public void StoneGameII_1() 10 | { 11 | var solution = new _1140_StoneGameII(); 12 | var result = solution.StoneGameII(new int[] { 2, 7, 9, 4, 4 }); 13 | Assert.AreEqual(10, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode/0251-0300/0292-NimGame.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 36ms 3 | // Memory Usage: 13.7 MB 4 | // Link: https://leetcode.com/submissions/detail/263265508/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0292_NimGame 10 | { 11 | public bool CanWinNim(int n) 12 | { 13 | return n % 4 != 0; 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0551-0600/0561-ArrayPartitionI-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0561_ArrayPartitionI_Test 7 | { 8 | [TestMethod] 9 | public void ArrayPairSum_1() 10 | { 11 | var solution = new _0561_ArrayPartitionI(); 12 | var result = solution.ArrayPairSum(new int[] { 1, 4, 3, 2 }); 13 | Assert.AreEqual(4, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0751-0800/0791-CustomSortString-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0791_CustomSortString_Test 7 | { 8 | [TestMethod] 9 | public void CustomSortString_1() 10 | { 11 | var solution = new _0791_CustomSortString(); 12 | var result = solution.CustomSortString("cba", "abcd"); 13 | Assert.AreEqual("cbad", result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0301-0350/0327-CountOfRangeSum-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0327_CountOfRangeSum_Test 7 | { 8 | [TestMethod] 9 | public void CountRangeSumTest() 10 | { 11 | var solution = new _0327_CountOfRangeSum(); 12 | var result = solution.CountRangeSum(new int[] { -2, 5, -1 }, -2, 2); 13 | Assert.AreEqual(3, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0601-0650/0645-SetMismatch-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0645_SetMismatch_Test 7 | { 8 | [TestMethod] 9 | public void FindErrorNums_1() 10 | { 11 | var solution = new _0645_SetMismatch(); 12 | var result = solution.FindErrorNums(new int[] { 1, 2, 2, 4 }); 13 | AssertHelper.AssertArray(new int[] { 2, 3 }, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode/0701-0750/0709-ToLowerCase.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 76ms 3 | // Memory Usage: 22.4 MB 4 | // Link: https://leetcode.com/submissions/detail/327368136/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0709_ToLowerCase 10 | { 11 | public string ToLowerCase(string str) 12 | { 13 | return str.ToLower(); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0601-0650/0621-TaskScheduler-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0621_TaskScheduler_Test 7 | { 8 | [TestMethod] 9 | public void LeastIntervalTest_1() 10 | { 11 | var solution = new _0621_TaskScheduler(); 12 | var result = solution.LeastInterval(new char[] { 'A', 'A', 'A', 'B', 'B', 'B' }, 2); 13 | Assert.AreEqual(8, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0651-0700/0686-RepeatedStringMatch-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0686_RepeatedStringMatch_Test 7 | { 8 | [TestMethod] 9 | public void RepeatedStringMatchTest() 10 | { 11 | var solution = new _0686_RepeatedStringMatch(); 12 | var result = solution.RepeatedStringMatch("abcd", "cdabcdab"); 13 | Assert.AreEqual(3, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0201-0250/0209-MinimumSizeSubarraySum-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0209_MinimumSizeSubarraySum_Test 7 | { 8 | [TestMethod] 9 | public void MinSubArrayLen_1() 10 | { 11 | var solution = new _0209_MinimumSizeSubarraySum(); 12 | var result = solution.MinSubArrayLen(7, new int[] { 2, 3, 1, 2, 4, 3 }); 13 | Assert.AreEqual(2, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0251-0300/0260-SingleNumberIII-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0260_SingleNumberIII_Test 7 | { 8 | [TestMethod] 9 | public void SingleNumber_1() 10 | { 11 | var solution = new _0260_SingleNumberIII(); 12 | var result = solution.SingleNumber(new int[] { 1, 2, 1, 3, 2, 5 }); 13 | AssertHelper.AssertArray(new int[] { 3, 5 }, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0401-0450/0434-NumberOfSegmentsInAString-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0434_NumberOfSegmentsInAString_Test 7 | { 8 | [TestMethod] 9 | public void CountSegments_1() 10 | { 11 | var solution = new _0434_NumberOfSegmentsInAString(); 12 | var result = solution.CountSegments("Hello, my name is John"); 13 | Assert.AreEqual(5, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0451-0500/0492-ConstructTheRectangle-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0492_ConstructTheRectangle_Test 7 | { 8 | [TestMethod] 9 | public void ConstructRectangle_1() 10 | { 11 | var solution = new _0492_ConstructTheRectangle(); 12 | var result = solution.ConstructRectangle(4); 13 | AssertHelper.AssertArray(new int[] { 2, 2 }, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0751-0800/0751-IPToCIDR-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0751_IPToCIDR_Test 7 | { 8 | [TestMethod] 9 | public void IpToCIDR_1() 10 | { 11 | var solution = new _0751_IPToCIDR(); 12 | var result = solution.IpToCIDR("255.0.0.7", 10); 13 | AssertHelper.AssertList(new string[] { "255.0.0.7/32", "255.0.0.8/29", "255.0.0.16/32" }, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode/0301-0350/0326-PowerOfThree.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 76ms 3 | // Memory Usage: 16.7 MB 4 | // Link: https://leetcode.com/submissions/detail/352829690/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0326_PowerOfThree 10 | { 11 | public bool IsPowerOfThree(int n) 12 | { 13 | return n > 0 && 1162261467 % n == 0; 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0451-0500/0485-MaxConsecutiveOnes-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0485_MaxConsecutiveOnes_Test 7 | { 8 | [TestMethod] 9 | public void FindMaxConsecutiveOnes_1() 10 | { 11 | var solution = new _0485_MaxConsecutiveOnes(); 12 | var result = solution.FindMaxConsecutiveOnes(new int[] { 1, 1, 0, 1, 1, 1 }); 13 | Assert.AreEqual(3, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0751-0800/0763-PartitionLabels-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0763_PartitionLabels_Test 7 | { 8 | [TestMethod] 9 | public void PartitionLabels_1() 10 | { 11 | var solution = new _0763_PartitionLabels(); 12 | var result = solution.PartitionLabels("ababcbacadefegdehijhklij"); 13 | AssertHelper.AssertList(new int[] { 9, 7, 8 }, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0551-0600/0594-LongestHarmoniousSubsequence-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0594_LongestHarmoniousSubsequence_Test 7 | { 8 | [TestMethod] 9 | public void FindLHS_1() 10 | { 11 | var solution = new _0594_LongestHarmoniousSubsequence(); 12 | var result = solution.FindLHS(new int[] { 1, 3, 2, 2, 5, 2, 3, 7 }); 13 | Assert.AreEqual(5, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/1001-1050/1048-LongestStringChain-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1048_LongestStringChain_Test 7 | { 8 | [TestMethod] 9 | public void LongestStrChain_1() 10 | { 11 | var solution = new _1048_LongestStringChain(); 12 | var result = solution.LongestStrChain(new string[] { "a", "b", "ba", "bca", "bda", "bdca" }); 13 | Assert.AreEqual(4, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0251-0300/0280-WiggleSort-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0280_WiggleSort_Test 7 | { 8 | [TestMethod] 9 | public void WiggleSort_1() 10 | { 11 | var arr = new int[] { 3, 5, 2, 1, 6, 4 }; 12 | 13 | var solution = new _0280_WiggleSort(); 14 | solution.WiggleSort(arr); 15 | AssertHelper.AssertArray(new int[] { 3, 5, 1, 6, 2, 4 }, arr); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode.Test/0451-0500/0500-KeyboardRow-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0500_KeyboardRow_Test 7 | { 8 | [TestMethod] 9 | public void FindWords_1() 10 | { 11 | var solution = new _0500_KeyboardRow(); 12 | var result = solution.FindWords(new string[] { "Hello", "Alaska", "Dad", "Peace" }); 13 | AssertHelper.AssertArray(new string[] { "Alaska", "Dad" }, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode/0351-0400/0371-SumOfTwoIntegers.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 36ms 3 | // Memory Usage: 13.7 MB 4 | // Link: https://leetcode.com/submissions/detail/261549862/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0371_SumOfTwoIntegers 10 | { 11 | public int GetSum(int a, int b) 12 | { 13 | return b == 0 ? a : GetSum(a ^ b, (a & b) << 1); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0301-0350/0347-TopKFrequentElements-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0347_TopKFrequentElements_Test 7 | { 8 | [TestMethod] 9 | public void TopKFrequentTest_1() 10 | { 11 | var solution = new _0347_TopKFrequentElements(); 12 | var result = solution.TopKFrequent(new int[] { 1, 1, 1, 2, 2, 3 }, 2); 13 | AssertHelper.AssertList(new int[] { 1, 2 }, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0551-0600/0563-BinaryTreeTilt-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0563_BinaryTreeTilt_Test 7 | { 8 | [TestMethod] 9 | public void FindTilt_1() 10 | { 11 | var root = TestHelper.GenerateTree(new int?[] { 1, 2, 3 }); 12 | 13 | var solution = new _0563_BinaryTreeTilt(); 14 | var result = solution.FindTilt(root); 15 | Assert.AreEqual(1, result); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode.Test/0501-0550/0503-NextGreaterElementII-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0503_NextGreaterElementII_Test 7 | { 8 | [TestMethod] 9 | public void NextGreaterElements_1() 10 | { 11 | var solution = new _0503_NextGreaterElementII(); 12 | var result = solution.NextGreaterElements(new int[] { 1, 2, 1 }); 13 | AssertHelper.AssertArray(new int[] { 2, -1, 2 }, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0901-0950/0922-SortArrayByParityII-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0922_SortArrayByParityII_Test 7 | { 8 | [TestMethod] 9 | public void SortArrayByParityII_1() 10 | { 11 | var solution = new _0922_SortArrayByParityII(); 12 | var result = solution.SortArrayByParityII(new int[] { 4, 2, 5, 7 }); 13 | AssertHelper.AssertArray(new int[] { 4, 5, 2, 7 }, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/1001-1050/1047-RemoveAllAdjacentDuplicatesInString-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1047_RemoveAllAdjacentDuplicatesInString_Test 7 | { 8 | [TestMethod] 9 | public void RemoveDuplicates_1() 10 | { 11 | var solution = new _1047_RemoveAllAdjacentDuplicatesInString(); 12 | var result = solution.RemoveDuplicates("abbaca"); 13 | Assert.AreEqual("ca", result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0301-0350/0309-BestTimeToBuyAndSellStockWithCooldown-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0309_BestTimeToBuyAndSellStockWithCooldown_Test 7 | { 8 | [TestMethod] 9 | public void MaxProfit_1() 10 | { 11 | var solution = new _0309_BestTimeToBuyAndSellStockWithCooldown(); 12 | var result = solution.MaxProfit(new int[] { 1, 2, 3, 0, 2 }); 13 | Assert.AreEqual(3, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0401-0450/0421-MaximumXOROfTwoNumbersInAnArray-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0421_MaximumXOROfTwoNumbersInAnArray_Test 7 | { 8 | [TestMethod] 9 | public void FindMaximumXORTest() 10 | { 11 | var solution = new _0421_MaximumXOROfTwoNumbersInAnArray(); 12 | var result = solution.FindMaximumXOR(new int[] { 3, 10, 5, 25, 2, 8 }); 13 | Assert.AreEqual(28, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0551-0600/0557-ReverseWordsInAStringIII-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0557_ReverseWordsInAStringIII_Test 7 | { 8 | [TestMethod] 9 | public void ReverseWords_1() 10 | { 11 | var solution = new _0557_ReverseWordsInAStringIII(); 12 | var result = solution.ReverseWords("Let's take LeetCode contest"); 13 | Assert.AreEqual("s'teL ekat edoCteeL tsetnoc", result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0701-0750/0713-SubarrayProductLessThanK-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0713_SubarrayProductLessThanK_Test 7 | { 8 | [TestMethod] 9 | public void NumSubarrayProductLessThanK_1() 10 | { 11 | var solution = new _0713_SubarrayProductLessThanK(); 12 | var result = solution.NumSubarrayProductLessThanK(new int[] { 10, 5, 2, 6 }, 100); 13 | Assert.AreEqual(8, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode/0301-0350/0342-PowerOfFour.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 32ms 3 | // Memory Usage: 14.7 MB 4 | // Link: https://leetcode.com/submissions/detail/352931470/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0342_PowerOfFour 10 | { 11 | public bool IsPowerOfFour(int num) 12 | { 13 | return num > 0 && ((num & (num - 1)) == 0) && ((num & 0xaaaaaaaa) == 0); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode/1251-1300/1256-EncodeNumber.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 96ms 3 | // Memory Usage: 22.7 MB 4 | // Link: https://leetcode.com/submissions/detail/363520520/ 5 | //----------------------------------------------------------------------------- 6 | 7 | using System; 8 | 9 | namespace LeetCode 10 | { 11 | public class _1256_EncodeNumber 12 | { 13 | public string Encode(int num) 14 | { 15 | return Convert.ToString(num + 1, 2).Substring(1); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode.Test/0201-0250/0239-SlidingWindowMaximum-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0239_SlidingWindowMaximum_Test 7 | { 8 | [TestMethod] 9 | public void MyTestMethod() 10 | { 11 | var solution = new _0239_SlidingWindowMaximum(); 12 | var result = solution.MaxSlidingWindow(new int[] { 1, 3, -1, -3, 5, 3, 6, 7 }, 3); 13 | AssertHelper.AssertArray(new int[] { 3, 3, 5, 5, 6, 7 }, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0401-0450/0401-BinaryWatch-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0401_BinaryWatch_Test 7 | { 8 | [TestMethod] 9 | public void ReadBinaryWatch_1() 10 | { 11 | var solution = new _0401_BinaryWatch(); 12 | var result = solution.ReadBinaryWatch(1); 13 | AssertHelper.AssertList(new string[] { "0:01", "0:02", "0:04", "0:08", "0:16", "0:32", "1:00", "2:00", "4:00", "8:00" }, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0401-0450/0412-FizzBuzz-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0412_FizzBuzz_Test 7 | { 8 | [TestMethod] 9 | public void FizzBuzz_1() 10 | { 11 | var solution = new _0412_FizzBuzz(); 12 | var result = solution.FizzBuzz(15); 13 | AssertHelper.AssertList(new string[] { "1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz" }, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0401-0450/0442-FindAllDuplicatesInAnArray-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0442_FindAllDuplicatesInAnArray_Test 7 | { 8 | [TestMethod] 9 | public void FindDuplicates_1() 10 | { 11 | var solution = new _0442_FindAllDuplicatesInAnArray(); 12 | var result = solution.FindDuplicates(new int[] { 4, 3, 2, 7, 8, 2, 3, 1 }); 13 | AssertHelper.AssertList(new int[] { 2, 3 }, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/1001-1050/1043-PartitionArrayForMaximumSum-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1043_PartitionArrayForMaximumSum_Test 7 | { 8 | [TestMethod] 9 | public void MaxSumAfterPartitioning_1() 10 | { 11 | var solution = new _1043_PartitionArrayForMaximumSum(); 12 | var result = solution.MaxSumAfterPartitioning(new int[] { 1, 15, 7, 9, 2, 5, 10 }, 3); 13 | Assert.AreEqual(84, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode/0401-0450/0441-ArrangingCoins.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 40ms 3 | // Memory Usage: 14.6 MB 4 | // Link: https://leetcode.com/submissions/detail/352955327/ 5 | //----------------------------------------------------------------------------- 6 | 7 | using System; 8 | 9 | namespace LeetCode 10 | { 11 | public class _0441_ArrangingCoins 12 | { 13 | public int ArrangeCoins(int n) 14 | { 15 | return (int)(Math.Sqrt(2 * (long)n + 0.25) - 0.5); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode/1201-1250/1227-AirplaneSeatAssignmentProbability.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 44ms 3 | // Memory Usage: 14.9 MB 4 | // Link: https://leetcode.com/submissions/detail/371134754/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _1227_AirplaneSeatAssignmentProbability 10 | { 11 | public double NthPersonGetsNthSeat(int n) 12 | { 13 | return n == 1 ? 1.0 : 0.5; 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0551-0600/0581-ShortestUnsortedContinuousSubarray-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0581_ShortestUnsortedContinuousSubarray_Test 7 | { 8 | [TestMethod] 9 | public void FindUnsortedSubarray_1() 10 | { 11 | var solution = new _0581_ShortestUnsortedContinuousSubarray(); 12 | var result = solution.FindUnsortedSubarray(new int[] { 2, 6, 4, 8, 10, 9, 15 }); 13 | Assert.AreEqual(5, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0701-0750/0739-DailyTemperatures-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0739_DailyTemperatures_Test 7 | { 8 | [TestMethod] 9 | public void DailyTemperatures_1() 10 | { 11 | var solution = new _0739_DailyTemperatures(); 12 | var result = solution.DailyTemperatures(new int[] { 73, 74, 75, 71, 69, 72, 76, 73 }); 13 | AssertHelper.AssertArray(new int[] { 1, 1, 4, 2, 1, 1, 0, 0 }, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode/0201-0250/0231-PowerOfTwo.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 36ms 3 | // Memory Usage: 14.5 MB 4 | // Link: https://leetcode.com/submissions/detail/350927502/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0231_PowerOfTwo 10 | { 11 | public bool IsPowerOfTwo(int n) 12 | { 13 | if (n == 0) return false; 14 | long x = n; 15 | return (x & -x) == x; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode.Test/0501-0550/0506-RelativeRanks-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0506_RelativeRanks_Test 7 | { 8 | [TestMethod] 9 | public void FindRelativeRanks_1() 10 | { 11 | var solution = new _0506_RelativeRanks(); 12 | var result = solution.FindRelativeRanks(new int[] { 5, 4, 3, 2, 1 }); 13 | AssertHelper.AssertArray(new string[] { "Gold Medal", "Silver Medal", "Bronze Medal", "4", "5" }, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0401-0450/0404-SumOfLeftLeaves-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0404_SumOfLeftLeaves_Test 7 | { 8 | [TestMethod] 9 | public void SumOfLeftLeaves_1() 10 | { 11 | var root = TestHelper.GenerateTree(new int?[] { 3, 9, 20, null, null, 15, 7 }); 12 | 13 | var solution = new _0404_SumOfLeftLeaves(); 14 | var result = solution.SumOfLeftLeaves(root); 15 | Assert.AreEqual(24, result); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode.Test/0651-0700/0654-MaximumBinaryTree-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0654_MaximumBinaryTree_Test 7 | { 8 | [TestMethod] 9 | public void ConstructMaximumBinaryTree() 10 | { 11 | var solution = new _0654_MaximumBinaryTree(); 12 | var root = solution.ConstructMaximumBinaryTree(new int[] { 3, 2, 1, 6, 0, 5 }); 13 | AssertHelper.AssertTree(new int?[] { 6, 3, 5, null, 2, 0, null, null, 1 }, root); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0801-0850/0821-ShortestDistanceToACharacter-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0821_ShortestDistanceToACharacter_Test 7 | { 8 | [TestMethod] 9 | public void ShortestToChar_1() 10 | { 11 | var solution = new _0821_ShortestDistanceToACharacter(); 12 | var result = solution.ShortestToChar("loveleetcode", 'e'); 13 | AssertHelper.AssertArray(new int[] { 3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0 }, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode/0201-0250/0237-DeleteNodeInALinkedList.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 96ms 3 | // Memory Usage: 24.2 MB 4 | // Link: https://leetcode.com/submissions/detail/263755695/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0237_DeleteNodeInALinkedList 10 | { 11 | public void DeleteNode(ListNode node) 12 | { 13 | node.val = node.next.val; 14 | node.next = node.next.next; 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /LeetCode.Test/0151-0200/0199-BinaryTreeRightSideView-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0199_BinaryTreeRightSideView_Test 7 | { 8 | [TestMethod] 9 | public void RightSideViewTest() 10 | { 11 | var solution = new _0199_BinaryTreeRightSideView(); 12 | var result = solution.RightSideView(TestHelper.GenerateTree(new int?[] { 1, 2, 3, null, 5, null, 4 })); 13 | AssertHelper.AssertList(new int[] { 1, 3, 4 }, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0601-0650/0636-ExclusiveTimeOfFunctions-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0636_ExclusiveTimeOfFunctions_Test 7 | { 8 | [TestMethod] 9 | public void ExclusiveTime_1() 10 | { 11 | var solution = new _0636_ExclusiveTimeOfFunctions(); 12 | var result = solution.ExclusiveTime(2, new string[] { "0:start:0", "1:start:2", "1:end:5", "0:end:6" }); 13 | AssertHelper.AssertArray(new int[] { 3, 4 }, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0751-0800/0760-FindAnagramMappings-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0760_FindAnagramMappings_Test 7 | { 8 | [TestMethod] 9 | public void AnagramMappings_1() 10 | { 11 | var solution = new _0760_FindAnagramMappings(); 12 | var result = solution.AnagramMappings(new int[] { 12, 28, 46, 32, 50 }, new int[] { 50, 12, 32, 46, 28 }); 13 | AssertHelper.AssertArray(new int[] { 1, 4, 3, 2, 0 }, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode/0251-0300/0258-AddDigits.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 36ms 3 | // Memory Usage: 14.6 MB 4 | // Link: https://leetcode.com/submissions/detail/340981660/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0258_AddDigits 10 | { 11 | public int AddDigits(int num) 12 | { 13 | if (num == 0) return 0; 14 | var result = num % 9; 15 | return result == 0 ? 9 : result; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode.Test/0251-0300/0271-EncodeAndDecodeStrings-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0271_EncodeAndDecodeStrings_Test 7 | { 8 | [TestMethod] 9 | public void EncodeDecodeTest() 10 | { 11 | var input = new string[] { "Hello", "", "World" }; 12 | 13 | var solution = new _0271_EncodeAndDecodeStrings(); 14 | var result = solution.decode(solution.encode(input)); 15 | AssertHelper.AssertList(input, result); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode.Test/0501-0550/0543-DiameterOfBinaryTree-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0543_DiameterOfBinaryTree_Test 7 | { 8 | [TestMethod] 9 | public void DiameterOfBinaryTreeTest() 10 | { 11 | var root = TestHelper.GenerateTree(new int?[] { 1, 2, 3, 4, 5 }); 12 | 13 | var solution = new _0543_DiameterOfBinaryTree(); 14 | var result = solution.DiameterOfBinaryTree(root); 15 | Assert.AreEqual(3, result); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode.Test/1101-1150/1120-MaximumAverageSubtree-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1120_MaximumAverageSubtree_Test 7 | { 8 | 9 | [TestMethod] 10 | public void MaximumAverageSubtree_1() 11 | { 12 | var root = TestHelper.GenerateTree(new int?[] { 5, 6, 1 }); 13 | var solution = new _1120_MaximumAverageSubtree(); 14 | var result = solution.MaximumAverageSubtree(root); 15 | Assert.AreEqual(6.0, result); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode.Test/1251-1300/1287-ElementAppearingMoreThan25InSortedArray-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1287_ElementAppearingMoreThan25InSortedArray_Test 7 | { 8 | [TestMethod] 9 | public void FindSpecialInteger_1() 10 | { 11 | var solution = new _1287_ElementAppearingMoreThan25InSortedArray(); 12 | var result = solution.FindSpecialInteger(new int[] { 1, 2, 2, 6, 6, 6, 6, 7, 10 }); 13 | Assert.AreEqual(6, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode/1151-1200/1185-DayOfTheWeek.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 80ms 3 | // Memory Usage: 22.5 MB 4 | // Link: https://leetcode.com/submissions/detail/333633527/ 5 | //----------------------------------------------------------------------------- 6 | 7 | using System; 8 | 9 | namespace LeetCode 10 | { 11 | public class _1185_DayOfTheWeek 12 | { 13 | public string DayOfTheWeek(int day, int month, int year) 14 | { 15 | return new DateTime(year, month, day).DayOfWeek.ToString(); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode.Test/0201-0250/0206-ReverseLinkedList-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0206_ReverseLinkedList_Test 7 | { 8 | [TestMethod] 9 | public void ReverseListTest() 10 | { 11 | var head = TestHelper.GenerateList(new int[] { 1, 2, 3, 4, 5 }); 12 | 13 | var solution = new _0206_ReverseLinkedList(); 14 | var newHead = solution.ReverseList(head); 15 | AssertHelper.AssertLinkList(new int[] { 5, 4, 3, 2, 1 }, newHead); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode.Test/0201-0250/0226-InvertBinaryTree-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0226_InvertBinaryTree_Test 7 | { 8 | [TestMethod] 9 | public void InvertTreeTest() 10 | { 11 | var root = TestHelper.GenerateTree(new int?[] { 4, 2, 7, 1, 3, 6, 9 }); 12 | var solution = new _0226_InvertBinaryTree(); 13 | var newRoot = solution.InvertTree(root); 14 | AssertHelper.AssertTree(new int?[] { 4, 7, 2, 9, 6, 3, 1 }, newRoot); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /LeetCode.Test/0401-0450/0448-FindAllNumbersDisappearedInAnArray-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0448_FindAllNumbersDisappearedInAnArray_Test 7 | { 8 | [TestMethod] 9 | public void FindDisappearedNumbers_1() 10 | { 11 | var solution = new _0448_FindAllNumbersDisappearedInAnArray(); 12 | var result = solution.FindDisappearedNumbers(new int[] { 4, 3, 2, 7, 8, 2, 3, 1 }); 13 | AssertHelper.AssertList(new int[] { 5, 6 }, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0501-0550/0535-EncodeAndDecodeTinyURL-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0535_EncodeAndDecodeTinyURL_Test 7 | { 8 | [TestMethod] 9 | public void EncodeAndDecodeTinyURLTest() 10 | { 11 | var input = "https://leetcode.com/problems/design-tinyurl"; 12 | 13 | var solution = new _0535_EncodeAndDecodeTinyURL(); 14 | var result = solution.decode(solution.encode(input)); 15 | Assert.AreEqual(result, input); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode.Test/0851-0900/0890-FindAndReplacePattern-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0890_FindAndReplacePattern_Test 7 | { 8 | [TestMethod] 9 | public void FindAndReplacePattern_1() 10 | { 11 | var solution = new _0890_FindAndReplacePattern(); 12 | var result = solution.FindAndReplacePattern(new string[] { "abc", "deq", "mee", "aqq", "dkd", "ccc" }, "abb"); 13 | AssertHelper.AssertList(new string[] { "mee", "aqq" }, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode/0101-0150/0136-SingleNumber.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 96ms 3 | // Memory Usage: 25.2 MB 4 | // Link: https://leetcode.com/submissions/detail/261543230/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0136_SingleNumber 10 | { 11 | public int SingleNumber(int[] nums) 12 | { 13 | var a = 0; 14 | foreach (var num in nums) 15 | a ^= num; 16 | 17 | return a; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode/0201-0250/0201-BitwiseANDOfNumbersRange.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 48ms 3 | // Memory Usage: 15.6 MB 4 | // Link: https://leetcode.com/submissions/detail/329230476/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0201_BitwiseANDOfNumbersRange 10 | { 11 | public int RangeBitwiseAnd(int m, int n) 12 | { 13 | while (m < n) 14 | n = n & (n - 1); 15 | 16 | return m & n; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode.Test/0501-0550/0501-FindModeInBinarySearchTree-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0501_FindModeInBinarySearchTree_Test 7 | { 8 | [TestMethod] 9 | public void FindMode_1() 10 | { 11 | var root = TestHelper.GenerateTree(new int?[] { 1, null, 2, 2 }); 12 | 13 | var solution = new _0501_FindModeInBinarySearchTree(); 14 | var result = solution.FindMode(root); 15 | AssertHelper.AssertArray(new int[] { 2 }, result); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode.Test/0501-0550/0538-ConvertBSTToGreaterTree-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0538_ConvertBSTToGreaterTree_Test 7 | { 8 | [TestMethod] 9 | public void ConvertBST_1() 10 | { 11 | var root = TestHelper.GenerateTree(new int?[] { 5, 2, 13 }); 12 | 13 | var solution = new _0538_ConvertBSTToGreaterTree(); 14 | var result = solution.ConvertBST(root); 15 | AssertHelper.AssertTree(new int?[] { 18, 20, 13 }, result); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode.Test/0901-0950/0929-UniqueEmailAddresses-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0929_UniqueEmailAddresses_Test 7 | { 8 | [TestMethod] 9 | public void NumUniqueEmailsTest() 10 | { 11 | var solution = new _0929_UniqueEmailAddresses(); 12 | var result = solution.NumUniqueEmails(new string[] { "test.email+alex@leetcode.com", "test.e.mail+bob.cathy@leetcode.com", "testemail+david@lee.tcode.com" }); 13 | Assert.AreEqual(2, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0901-0950/0950-RevealCardsInIncreasingOrder-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0950_RevealCardsInIncreasingOrder_Test 7 | { 8 | [TestMethod] 9 | public void DeckRevealedIncreasing_1() 10 | { 11 | var solution = new _0950_RevealCardsInIncreasingOrder(); 12 | var result = solution.DeckRevealedIncreasing(new int[] { 17, 13, 11, 2, 3, 5, 7 }); 13 | AssertHelper.AssertArray(new int[] { 2, 13, 3, 11, 5, 17, 7 }, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/1151-1200/1161-MaximumLevelSumOfABinaryTree-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1161_MaximumLevelSumOfABinaryTree_Test 7 | { 8 | [TestMethod] 9 | public void MaxLevelSum_1() 10 | { 11 | var root = TestHelper.GenerateTree(new int?[] { 1, 7, 0, 7, -8, null, null }); 12 | 13 | var solution = new _1161_MaximumLevelSumOfABinaryTree(); 14 | var result = solution.MaxLevelSum(root); 15 | Assert.AreEqual(2, result); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode/0451-0500/0458-PoorPigs.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 36ms 3 | // Memory Usage: 4 | // Link: 5 | //----------------------------------------------------------------------------- 6 | 7 | using System; 8 | 9 | namespace LeetCode 10 | { 11 | public class _0458_PoorPigs 12 | { 13 | public int PoorPigs(int buckets, int minutesToDie, int minutesToTest) 14 | { 15 | int states = minutesToTest / minutesToDie + 1; 16 | return (int)Math.Ceiling(Math.Log(buckets) / Math.Log(states)); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode.Test/0251-0300/0256-PaintHouse-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0256_PaintHouse_Test 7 | { 8 | [TestMethod] 9 | public void MinCost_1() 10 | { 11 | var solution = new _0256_PaintHouse(); 12 | var result = solution.MinCost(new int[][] { 13 | new int[] { 17, 2, 17 }, 14 | new int[] { 16, 16, 5 }, 15 | new int[] { 14, 3, 19 }, 16 | }); 17 | Assert.AreEqual(10, result); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode.Test/1101-1150/1122-RelativeSortArray-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1122_RelativeSortArray_Test 7 | { 8 | [TestMethod] 9 | public void RelativeSortArray_1() 10 | { 11 | var solution = new _1122_RelativeSortArray(); 12 | var result = solution.RelativeSortArray(new int[] { 2, 3, 1, 3, 2, 4, 6, 7, 9, 2, 19 }, new int[] { 2, 1, 4, 3, 9, 6 }); 13 | AssertHelper.AssertArray(new int[] { 2, 2, 2, 1, 4, 3, 3, 9, 6, 7, 19 }, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/1301-1350/1302-DeepestLeavesSum-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1302_DeepestLeavesSum_Test 7 | { 8 | [TestMethod] 9 | public void DeepestLeavesSum_1() 10 | { 11 | var root = TestHelper.GenerateTree(new int?[] { 1, 2, 3, 4, 5, null, 6, 7, null, null, null, null, 8 }); 12 | 13 | var solution = new _1302_DeepestLeavesSum(); 14 | var result = solution.DeepestLeavesSum(root); 15 | Assert.AreEqual(15, result); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode.Test/0301-0350/0303-RangeSumQueryImmutable-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0303_RangeSumQueryImmutable_Test 7 | { 8 | [TestMethod] 9 | public void RangeSumQueryImmutable_1() 10 | { 11 | var solution = new _0303_RangeSumQueryImmutable(new int[] { -2, 0, 3, -5, 2, -1 }); 12 | Assert.AreEqual(1, solution.SumRange(0, 2)); 13 | Assert.AreEqual(-1, solution.SumRange(2, 5)); 14 | Assert.AreEqual(-3, solution.SumRange(0, 5)); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /LeetCode.Test/0401-0450/0450-DeleteNodeInABST-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0450_DeleteNodeInABST_Test 7 | { 8 | [TestMethod] 9 | public void DeleteNode_1() 10 | { 11 | var root = TestHelper.GenerateTree(new int?[] { 5, 3, 6, 2, 4, null, 7 }); 12 | 13 | var solution = new _0450_DeleteNodeInABST(); 14 | var result = solution.DeleteNode(root, 3); 15 | AssertHelper.AssertTree(new int?[] { 5, 4, 6, 2, null, null, 7 }, result); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode/0401-0450/0434-NumberOfSegmentsInAString.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 68ms 3 | // Memory Usage: 21.6 MB 4 | // Link: https://leetcode.com/submissions/detail/358362139/ 5 | //----------------------------------------------------------------------------- 6 | 7 | using System; 8 | 9 | namespace LeetCode 10 | { 11 | public class _0434_NumberOfSegmentsInAString 12 | { 13 | public int CountSegments(string s) 14 | { 15 | return s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Length; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode/1251-1300/1295-FindNumbersWithEvenNumberOfDigits.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 92ms 3 | // Memory Usage: 25.8 MB 4 | // Link: https://leetcode.com/submissions/detail/326443837/ 5 | //----------------------------------------------------------------------------- 6 | 7 | using System.Linq; 8 | 9 | namespace LeetCode 10 | { 11 | public class _1295_FindNumbersWithEvenNumberOfDigits 12 | { 13 | public int FindNumbers(int[] nums) 14 | { 15 | return nums.Count(num => num.ToString().Length % 2 == 0); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode.Test/0701-0750/0743-NetworkDelayTime-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0743_NetworkDelayTime_Test 7 | { 8 | [TestMethod] 9 | public void NetworkDelayTimeTest() 10 | { 11 | var solution = new _0743_NetworkDelayTime(); 12 | var result = solution.NetworkDelayTime(new int[][] 13 | { 14 | new int[] { 2, 1, 1 }, 15 | new int[] { 2, 3, 1 }, 16 | new int[] { 3, 4, 1 } 17 | }, 4, 2); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode/0451-0500/0476-NumberComplement.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 32ms 3 | // Memory Usage: 14.5 MB 4 | // Link: https://leetcode.com/submissions/detail/334546822/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0476_NumberComplement 10 | { 11 | public int FindComplement(int num) 12 | { 13 | int mask = 1; 14 | while (mask < num) 15 | mask = (mask << 1) | 1; 16 | return num ^ mask; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode/0501-0550/0521-LongestUncommonSubsequenceI.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 68ms 3 | // Memory Usage: 22.1 MB 4 | // Link: https://leetcode.com/submissions/detail/338636813/ 5 | //----------------------------------------------------------------------------- 6 | 7 | using System; 8 | 9 | namespace LeetCode 10 | { 11 | public class _0521_LongestUncommonSubsequenceI 12 | { 13 | public int FindLUSlength(string a, string b) 14 | { 15 | if (a == b) return -1; 16 | return Math.Max(a.Length, b.Length); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode.Test/0451-0500/0470-ImplementRand10UsingRand7-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0470_ImplementRand10UsingRand7_Test 7 | { 8 | [TestMethod] 9 | public void Rand10_1() 10 | { 11 | var solution = new _0470_ImplementRand10UsingRand7(); 12 | 13 | for (int i = 0; i < 1000; i++) 14 | { 15 | var result = solution.Rand10(); 16 | Assert.IsTrue(result > 0); 17 | Assert.IsTrue(result <= 10); 18 | } 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /LeetCode.Test/1251-1300/1299-ReplaceElementsWithGreatestElementOnRightSide-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1299_ReplaceElementsWithGreatestElementOnRightSide_Test 7 | { 8 | [TestMethod] 9 | public void ReplaceElements_1() 10 | { 11 | var solution = new _1299_ReplaceElementsWithGreatestElementOnRightSide(); 12 | var result = solution.ReplaceElements(new int[] { 17, 18, 5, 4, 6, 1 }); 13 | AssertHelper.AssertArray(new int[] { 18, 6, 6, 6, 1, -1 }, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0151-0200/0174-DungeonGame-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0174_DungeonGame_Test 7 | { 8 | [TestMethod] 9 | public void CalculateMinimumHP_1() 10 | { 11 | var solution = new _0174_DungeonGame(); 12 | var result = solution.CalculateMinimumHP(new int[][] { 13 | new int[] { -2, -3, 3 }, 14 | new int[] { -5, -10, 1 }, 15 | new int[] { 10, 30, -5 }, 16 | }); 17 | Assert.AreEqual(7, result); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode.Test/0201-0250/0203-RemoveLinkedListElements-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0203_RemoveLinkedListElements_Test 7 | { 8 | [TestMethod] 9 | public void RemoveElements_1() 10 | { 11 | var head = TestHelper.GenerateList(new int[] { 1, 2, 6, 3, 4, 5, 6 }); 12 | 13 | var solution = new _0203_RemoveLinkedListElements(); 14 | var result = solution.RemoveElements(head, 6); 15 | AssertHelper.AssertLinkList(new int[] { 1, 2, 3, 4, 5 }, result); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode.Test/0201-0250/0225-ImplementStackUsingQueues-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0225_ImplementStackUsingQueues_Test 7 | { 8 | [TestMethod] 9 | public void ImplementStackUsingQueues_1() 10 | { 11 | var solution = new _0225_ImplementStackUsingQueues(); 12 | solution.Push(1); 13 | solution.Push(2); 14 | Assert.AreEqual(2, solution.Top()); 15 | Assert.AreEqual(2, solution.Pop()); 16 | Assert.IsFalse(solution.Empty()); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode.Test/0201-0250/0232-ImplementQueueUsingStacks-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0232_ImplementQueueUsingStacks_Test 7 | { 8 | [TestMethod] 9 | public void ImplementQueueUsingStacks_1() 10 | { 11 | var solution = new _0232_ImplementQueueUsingStacks(); 12 | solution.Push(1); 13 | solution.Push(2); 14 | Assert.AreEqual(1, solution.Peek()); 15 | Assert.AreEqual(1, solution.Pop()); 16 | Assert.IsFalse(solution.Empty()); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode.Test/0701-0750/0715-RangeModule-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0715_RangeModule_Test 7 | { 8 | [TestMethod] 9 | public void RangeModuleTest_1() 10 | { 11 | var solution = new _0715_RangeModule(); 12 | 13 | solution.AddRange(10, 20); 14 | solution.RemoveRange(14, 16); 15 | Assert.IsTrue(solution.QueryRange(10, 14)); 16 | Assert.IsFalse(solution.QueryRange(13, 15)); 17 | Assert.IsTrue(solution.QueryRange(16, 17)); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode.Test/1201-1250/1213-IntersectionOfThreeSortedArrays-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1213_IntersectionOfThreeSortedArrays_Test 7 | { 8 | [TestMethod] 9 | public void ArraysIntersection_1() 10 | { 11 | var solution = new _1213_IntersectionOfThreeSortedArrays(); 12 | var result = solution.ArraysIntersection(new int[] { 1, 2, 3, 4, 5 }, new int[] { 1, 2, 5, 7, 9 }, new int[] { 1, 3, 4, 5, 8 }); 13 | AssertHelper.AssertList(new int[] { 1, 5 }, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/0401-0450/0447-NumberOfBoomerangs-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0447_NumberOfBoomerangs_Test 7 | { 8 | [TestMethod] 9 | public void NumberOfBoomerangs_1() 10 | { 11 | var solution = new _0447_NumberOfBoomerangs(); 12 | var result = solution.NumberOfBoomerangs(new int[][] { 13 | new int[] { 0, 0 }, 14 | new int[] { 1, 0 }, 15 | new int[] { 2, 0 }, 16 | }); 17 | Assert.AreEqual(2, result); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode.Test/0501-0550/0515-FindLargestValueInEachTreeRow-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0515_FindLargestValueInEachTreeRow_Test 7 | { 8 | [TestMethod] 9 | public void LargestValues_1() 10 | { 11 | var root = TestHelper.GenerateTree(new int?[] { 1, 3, 2, 5, 3, null, 9 }); 12 | 13 | var solution = new _0515_FindLargestValueInEachTreeRow(); 14 | var result = solution.LargestValues(root); 15 | AssertHelper.AssertList(new int[] { 1, 3, 9 }, result); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode.Test/0801-0850/0843-GuesstheWord-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0843_GuesstheWord_Test 7 | { 8 | [TestMethod] 9 | public void FindSecretWordTest() 10 | { 11 | var wordlist = new string[] { "acckzz", "ccbazz", "eiowzz", "abcczz" }; 12 | var master = new Master("acckzz", wordlist); 13 | 14 | var solution = new _0843_GuesstheWord(); 15 | solution.FindSecretWord(wordlist, master); 16 | 17 | Assert.IsTrue(master.GuessCount <= 10); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode.Test/1001-1050/1008-ConstructBinarySearchTreeFromPreorderTraversal-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1008_ConstructBinarySearchTreeFromPreorderTraversal_Test 7 | { 8 | [TestMethod] 9 | public void BstFromPreorder_1() 10 | { 11 | var solution = new _1008_ConstructBinarySearchTreeFromPreorderTraversal(); 12 | var result = solution.BstFromPreorder(new int[] { 8, 5, 1, 7, 10, 12 }); 13 | AssertHelper.AssertTree(new int?[] { 8, 5, 10, 1, 7, null, 12 }, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode.Test/1351-1400/1382-BalanceABinarySearchTree-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1382_BalanceABinarySearchTree_Test 7 | { 8 | [TestMethod] 9 | public void BalanceBST_1() 10 | { 11 | var root = TestHelper.GenerateTree(new int?[] { 1, null, 2, null, 3, null, 4, null, null }); 12 | 13 | var solution = new _1382_BalanceABinarySearchTree(); 14 | var result = solution.BalanceBST(root); 15 | AssertHelper.AssertTree(new int?[] { 3, 2, 4, 1 }, result); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode/0251-0300/0268-MissingNumber.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 100ms 3 | // Memory Usage: 29.9 MB 4 | // Link: https://leetcode.com/submissions/detail/380050584/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0268_MissingNumber 10 | { 11 | public int MissingNumber(int[] nums) 12 | { 13 | int result = 0; 14 | for (int i = 0; i < nums.Length; ++i) 15 | result += (i + 1) - nums[i]; 16 | return result; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode/0551-0600/0557-ReverseWordsInAStringIII.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 92ms 3 | // Memory Usage: 34.8 MB 4 | // Link: https://leetcode.com/submissions/detail/330119313/ 5 | //----------------------------------------------------------------------------- 6 | 7 | using System.Linq; 8 | 9 | namespace LeetCode 10 | { 11 | public class _0557_ReverseWordsInAStringIII 12 | { 13 | public string ReverseWords(string s) 14 | { 15 | return string.Join(" ", s.Split(' ').Select(w => new string(w.ToCharArray().Reverse().ToArray()))); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode.Test/0201-0250/0208-ImplementTrie-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0208_ImplementTrie_Test 7 | { 8 | [TestMethod] 9 | public void TrieTest() 10 | { 11 | var trie = new _0208_ImplementTrie(); 12 | 13 | trie.Insert("apple"); 14 | Assert.IsTrue(trie.Search("apple")); 15 | Assert.IsFalse(trie.Search("app")); 16 | Assert.IsTrue(trie.StartsWith("app")); 17 | trie.Insert("app"); 18 | Assert.IsTrue(trie.Search("app")); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /LeetCode/0101-0150/104-MaximumDepthOfBinaryTree.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 88ms 3 | // Memory Usage: 25.3 MB 4 | // Link: https://leetcode.com/submissions/detail/333021377/ 5 | //----------------------------------------------------------------------------- 6 | 7 | using System; 8 | 9 | namespace LeetCode 10 | { 11 | public class _104_MaximumDepthOfBinaryTree 12 | { 13 | public int MaxDepth(TreeNode root) 14 | { 15 | if (root == null) return 0; 16 | return Math.Max(1 + MaxDepth(root.left), 1 + MaxDepth(root.right)); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode/1001-1050/1009-ComplementOfBase10Integer.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 32ms 3 | // Memory Usage: 14.5 MB 4 | // Link: https://leetcode.com/submissions/detail/334548152/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _1009_ComplementOfBase10Integer 10 | { 11 | public int BitwiseComplement(int N) 12 | { 13 | int mask = 1; 14 | while (mask < N) 15 | mask = (mask << 1) | 1; 16 | return N ^ mask; 17 | 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode.Test/0601-0650/0637-AverageOfLevelsInBinaryTree-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0637_AverageOfLevelsInBinaryTree_Test 7 | { 8 | [TestMethod] 9 | public void AverageOfLevels_1() 10 | { 11 | var root = TestHelper.GenerateTree(new int?[] { 3, 9, 20, null, null, 15, 7 }); 12 | 13 | var solution = new _0637_AverageOfLevelsInBinaryTree(); 14 | var result = solution.AverageOfLevels(root); 15 | AssertHelper.AssertList(new double[] { 3, 14.5, 11 }, result); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode.Test/0251-0300/0295-FindMedianFromDataStream-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0295_FindMedianFromDataStream_Test 7 | { 8 | [TestMethod] 9 | public void MedianFinderTest() 10 | { 11 | var medianFinder = new _0295_FindMedianFromDataStream(); 12 | 13 | medianFinder.AddNum(1); 14 | medianFinder.AddNum(2); 15 | Assert.AreEqual(1.5, medianFinder.FindMedian()); 16 | medianFinder.AddNum(3); 17 | Assert.AreEqual(2, medianFinder.FindMedian()); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode.Test/0901-0950/0931-MinimumFallingPathSum-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0931_MinimumFallingPathSum_Test 7 | { 8 | [TestMethod] 9 | public void MinFallingPathSum_1() 10 | { 11 | var solution = new _0931_MinimumFallingPathSum(); 12 | var result = solution.MinFallingPathSum(new int[][] { 13 | new int[] { 1, 2, 3 }, 14 | new int[] { 4, 5, 6 }, 15 | new int[] { 7, 8, 9 }, 16 | }); 17 | Assert.AreEqual(12, result); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode/1451-1500/1486-XOROperationInAnArray.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 36ms 3 | // Memory Usage: 14.4 MB 4 | // Link: https://leetcode.com/submissions/detail/358271770/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _1486_XOROperationInAnArray 10 | { 11 | public int XorOperation(int n, int start) 12 | { 13 | var result = 0; 14 | for (int i = 0; i < n; i++) 15 | result ^= start + i * 2; 16 | 17 | return result; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode.Test/0851-0900/0861-ScoreAfterFlippingMatrix-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0861_ScoreAfterFlippingMatrix_Test 7 | { 8 | [TestMethod] 9 | public void MatrixScore_1() 10 | { 11 | var solution = new _0861_ScoreAfterFlippingMatrix(); 12 | var result = solution.MatrixScore(new int[][] { 13 | new int[] { 0, 0, 1, 1 }, 14 | new int[] { 1, 0, 1, 0 }, 15 | new int[] { 1, 1, 0, 0 } 16 | }); 17 | Assert.AreEqual(39, result); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode/0151-0200/0191-NumberOf1Bits.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 36ms 3 | // Memory Usage: 14.7 MB 4 | // Link: https://leetcode.com/submissions/detail/335030341/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0191_NumberOf1Bits 10 | { 11 | public int HammingWeight(uint n) 12 | { 13 | var num = 0; 14 | while (n > 0) 15 | { 16 | n &= n - 1; 17 | num++; 18 | } 19 | 20 | return num; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LeetCode/0451-0500/0492-ConstructTheRectangle.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 200ms 3 | // Memory Usage: 25.3 MB 4 | // Link: https://leetcode.com/submissions/detail/345098085/ 5 | //----------------------------------------------------------------------------- 6 | 7 | using System; 8 | 9 | namespace LeetCode 10 | { 11 | public class _0492_ConstructTheRectangle 12 | { 13 | public int[] ConstructRectangle(int area) 14 | { 15 | var w = (int)Math.Sqrt(area); 16 | while (area % w != 0) w--; 17 | return new int[] { area / w, w }; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode/0051-0100/096-UniqueBinarySearchTree.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 36ms 3 | // Memory Usage: 14.6 MB 4 | // Link: https://leetcode.com/submissions/detail/357929135/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _096_UniqueBinarySearchTree 10 | { 11 | public int NumTrees(int n) 12 | { 13 | long result = 1; 14 | for (int i = 0; i < n; i++) 15 | result = result * 2 * (2 * i + 1) / (i + 2); 16 | 17 | return (int)result; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode.Test/0351-0400/0370-RangeAddition-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0370_RangeAddition_Test 7 | { 8 | [TestMethod] 9 | public void GetModifiedArray_1() 10 | { 11 | var solution = new _0370_RangeAddition(); 12 | var result = solution.GetModifiedArray(5, new int[][] { 13 | new int[] { 1, 3, 2 }, 14 | new int[] { 2, 4, 3 }, 15 | new int[] { 0, 2, -2 }, 16 | }); 17 | AssertHelper.AssertArray(new int[] { -2, 0, 3, 5, 3 }, result); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode.Test/1001-1050/1026-MaximumDifferenceBetweenNodeAndAncestor-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1026_MaximumDifferenceBetweenNodeAndAncestor_Test 7 | { 8 | [TestMethod] 9 | public void MaxAncestorDiff_1() 10 | { 11 | var root = TestHelper.GenerateTree(new int?[] { 8, 3, 10, 1, 6, null, 14, null, null, 4, 7, 13 }); 12 | 13 | var solution = new _1026_MaximumDifferenceBetweenNodeAndAncestor(); 14 | var result = solution.MaxAncestorDiff(root); 15 | Assert.AreEqual(7, result); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode.Test/1301-1350/1315-SumOfNodesWithEvenValuedGrandparent-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1315_SumOfNodesWithEvenValuedGrandparent_Test 7 | { 8 | [TestMethod] 9 | public void SumEvenGrandparent_1() 10 | { 11 | var root = TestHelper.GenerateTree(new int?[] { 6, 7, 8, 2, 7, 1, 3, 9, null, 1, 4, null, null, null, 5 }); 12 | 13 | var solution = new _1315_SumOfNodesWithEvenValuedGrandparent(); 14 | var result = solution.SumEvenGrandparent(root); 15 | Assert.AreEqual(18, result); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode/0551-0600/0575-DistributeCandies.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 264ms 3 | // Memory Usage: 45.1 MB 4 | // Link: https://leetcode.com/submissions/detail/335623583/ 5 | //----------------------------------------------------------------------------- 6 | 7 | using System; 8 | using System.Collections.Generic; 9 | 10 | namespace LeetCode 11 | { 12 | public class _0575_DistributeCandies 13 | { 14 | public int DistributeCandies(int[] candies) 15 | { 16 | var set = new HashSet(candies); 17 | return Math.Min(set.Count, candies.Length / 2); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode/1351-1400/1374-GenerateAStringWithCharactersThatHaveOddCounts.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 72ms 3 | // Memory Usage: 22.7 MB 4 | // Link: https://leetcode.com/submissions/detail/327826280/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _1374_GenerateAStringWithCharactersThatHaveOddCounts 10 | { 11 | public string GenerateTheString(int n) 12 | { 13 | return n % 2 == 1 ? 14 | new string('a', n) : 15 | new string('a', n - 1) + 'b'; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode/0801-0850/0836-RectangleOverlap.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 108ms 3 | // Memory Usage: 23.9 MB 4 | // Link: https://leetcode.com/submissions/detail/351821481/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0836_RectangleOverlap 10 | { 11 | public bool IsRectangleOverlap(int[] rec1, int[] rec2) 12 | { 13 | return rec1[2] > rec2[0] && 14 | rec1[3] > rec2[1] && 15 | rec1[0] < rec2[2] && 16 | rec1[1] < rec2[3]; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode.Test/0251-0300/0297-SerializeandDeserializeBinaryTree-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0297_SerializeandDeserializeBinaryTree_Test 7 | { 8 | [TestMethod] 9 | public void SerializeandDeserializeBinaryTreeTest() 10 | { 11 | var root = TestHelper.GenerateTree(new int?[] { 1, 2, 3, null, null, 4, 5 }); 12 | var solution = new _0297_SerializeandDeserializeBinaryTree(); 13 | var newRoot = solution.Deserialize(solution.Serialize(root)); 14 | 15 | AssertHelper.AssertTree(root, newRoot); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode.Test/0401-0450/0419-BattleshipsInABoard-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0419_BattleshipsInABoard_Test 7 | { 8 | [TestMethod] 9 | public void CountBattleships_1() 10 | { 11 | var solution = new _0419_BattleshipsInABoard(); 12 | var result = solution.CountBattleships(new char[][] { 13 | new char[] { 'X', '.', '.', 'X' }, 14 | new char[] { '.', '.', '.', 'X' }, 15 | new char[] { '.', '.', '.', 'X' }, 16 | }); 17 | Assert.AreEqual(2, result); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode.Test/1001-1050/1029-TwoCityScheduling-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1029_TwoCityScheduling_Test 7 | { 8 | [TestMethod] 9 | public void TwoCitySchedCost_1() 10 | { 11 | var solution = new _1029_TwoCityScheduling(); 12 | var result = solution.TwoCitySchedCost(new int[][] { 13 | new int[] { 10, 20 }, 14 | new int[] { 30, 200 }, 15 | new int[] { 400, 50 }, 16 | new int[] { 30, 20 }, 17 | }); 18 | Assert.AreEqual(110, result); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /LeetCode/0151-0200/0172-FactorialTrailingZeroes.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 40ms 3 | // Memory Usage: 14.6 MB 4 | // Link: https://leetcode.com/submissions/detail/358360122/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0172_FactorialTrailingZeroes 10 | { 11 | public int TrailingZeroes(int n) 12 | { 13 | var count = 0; 14 | while (n > 0) 15 | { 16 | n /= 5; 17 | count += n; 18 | } 19 | 20 | return count; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LeetCode.Test/0451-0500/0463-IslandPerimeter-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0463_IslandPerimeter_Test 7 | { 8 | [TestMethod] 9 | public void IslandPerimeter_1() 10 | { 11 | var solution = new _0463_IslandPerimeter(); 12 | var result = solution.IslandPerimeter(new int[][] { 13 | new int[] { 0, 1, 0, 0 }, 14 | new int[] { 1, 1, 1, 0 }, 15 | new int[] { 0, 1, 0, 0 }, 16 | new int[] { 1, 1, 0, 0 }, 17 | }); 18 | Assert.AreEqual(16, result); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /LeetCode.Test/0251-0300/0258-AddDigits-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0258_AddDigits_Test 7 | { 8 | [TestMethod] 9 | public void AddDigits_1() 10 | { 11 | var solution = new _0258_AddDigits(); 12 | var result = solution.AddDigits(38); 13 | Assert.AreEqual(2, result); 14 | } 15 | 16 | [TestMethod] 17 | public void AddDigits_2() 18 | { 19 | var solution = new _0258_AddDigits(); 20 | var result = solution.AddDigits(0); 21 | Assert.AreEqual(0, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0851-0900/0865-SmallestSubtreeWithAllTheDeepestNodes-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0865_SmallestSubtreeWithAllTheDeepestNodes_Test 7 | { 8 | [TestMethod] 9 | public void SubtreeWithAllDeepest_1() 10 | { 11 | var root = TestHelper.GenerateTree(new int?[] { 3, 5, 1, 6, 2, 0, 8, null, null, 7, 4 }); 12 | 13 | var solution = new _0865_SmallestSubtreeWithAllTheDeepestNodes(); 14 | var result = solution.SubtreeWithAllDeepest(root); 15 | AssertHelper.AssertTree(new int?[] { 2, 7, 4 }, result); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode.Test/0851-0900/0889-ConstructBinaryTreeFromPreorderAndPostorderTraversal-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0889_ConstructBinaryTreeFromPreorderAndPostorderTraversal_Test 7 | { 8 | [TestMethod] 9 | public void ConstructFromPrePost_1() 10 | { 11 | var solution = new _0889_ConstructBinaryTreeFromPreorderAndPostorderTraversal(); 12 | var result = solution.ConstructFromPrePost(new int[] { 1, 2, 4, 5, 3, 6, 7 }, new int[] { 4, 5, 2, 6, 7, 3, 1 }); 13 | AssertHelper.AssertTree(new int?[] { 1, 2, 3, 4, 5, 6, 7 }, result); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /LeetCode/0551-0600/0561-ArrayPartitionI.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 156ms 3 | // Memory Usage: 34.9 MB 4 | // Link: https://leetcode.com/submissions/detail/328332615/ 5 | //----------------------------------------------------------------------------- 6 | 7 | using System; 8 | 9 | namespace LeetCode 10 | { 11 | public class _0561_ArrayPartitionI 12 | { 13 | public int ArrayPairSum(int[] nums) 14 | { 15 | Array.Sort(nums); 16 | 17 | int sum = 0; 18 | for (var i = 0; i < nums.Length; i += 2) 19 | sum += nums[i]; 20 | 21 | return sum; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0301-0350/0317-ShortestDistanceFromAllBuildings-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0317_ShortestDistanceFromAllBuildings_Test 7 | { 8 | [TestMethod] 9 | public void ShortestDistance_1() 10 | { 11 | var solution = new _0317_ShortestDistanceFromAllBuildings(); 12 | var result = solution.ShortestDistance(new int[][] { 13 | new int[] { 1, 0, 2, 0, 1 }, 14 | new int[] { 0, 0, 0, 0, 0 }, 15 | new int[] { 0, 0, 1, 0, 0 }, 16 | }); 17 | Assert.AreEqual(7, result); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode.Test/0451-0500/0498-DiagonalTraverse-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0498_DiagonalTraverse_Test 7 | { 8 | [TestMethod] 9 | public void FindDiagonalOrder_1() 10 | { 11 | var solution = new _0498_DiagonalTraverse(); 12 | var result = solution.FindDiagonalOrder(new int[][] 13 | { 14 | new int[] { 1, 2, 3 }, 15 | new int[] { 4, 5, 6 }, 16 | new int[] { 7, 8, 9 }, 17 | }); 18 | AssertHelper.AssertArray(new int[] { 1, 2, 4, 7, 5, 3, 6, 8, 9 }, result); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /LeetCode.Test/0101-0150/0131-PalindromePartitioning-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using System.Collections.Generic; 3 | 4 | namespace LeetCode.Test 5 | { 6 | [TestClass] 7 | public class _0131_PalindromePartitioning_Test 8 | { 9 | [TestMethod] 10 | public void PartitionTest() 11 | { 12 | var solution = new _0131_PalindromePartitioning(); 13 | var result = solution.Partition("aab"); 14 | AssertHelper.AssertList(new List>() 15 | { 16 | new List() { "a", "a", "b" }, 17 | new List() { "aa", "b" }, 18 | }, result); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /LeetCode.Test/0301-0350/0342-PowerOfFour-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0342_PowerOfFour_Test 7 | { 8 | [TestMethod] 9 | public void IsPowerOfFour_1() 10 | { 11 | var solution = new _0342_PowerOfFour(); 12 | var result = solution.IsPowerOfFour(16); 13 | Assert.IsTrue(result); 14 | } 15 | 16 | [TestMethod] 17 | public void IsPowerOfFour_2() 18 | { 19 | var solution = new _0342_PowerOfFour(); 20 | var result = solution.IsPowerOfFour(5); 21 | Assert.IsFalse(result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0701-0750/0705-DesignHashset-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0705_DesignHashset_Test 7 | { 8 | [TestMethod] 9 | public void MyHashSet_1() 10 | { 11 | var hashSet = new _0705_DesignHashset(); 12 | 13 | hashSet.Add(1); 14 | hashSet.Add(2); 15 | Assert.IsTrue(hashSet.Contains(1)); 16 | Assert.IsFalse(hashSet.Contains(3)); 17 | hashSet.Add(2); 18 | Assert.IsTrue(hashSet.Contains(2)); 19 | hashSet.Remove(2); 20 | Assert.IsFalse(hashSet.Contains(2)); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LeetCode.Test/0701-0750/0706-DesignHashmap-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0706_DesignHashmap_Test 7 | { 8 | [TestMethod] 9 | public void MyHashMap_1() 10 | { 11 | var hashMap = new _0706_DesignHashmap(); 12 | hashMap.Put(1, 1); 13 | hashMap.Put(2, 2); 14 | Assert.AreEqual(1, hashMap.Get(1)); 15 | Assert.AreEqual(-1, hashMap.Get(3)); 16 | hashMap.Put(2, 1); 17 | Assert.AreEqual(1, hashMap.Get(2)); 18 | hashMap.Remove(2); 19 | Assert.AreEqual(-1, hashMap.Get(2)); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LeetCode.Test/1251-1300/1256-EncodeNumber-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1256_EncodeNumber_Test 7 | { 8 | [TestMethod] 9 | public void Encode_1() 10 | { 11 | var solution = new _1256_EncodeNumber(); 12 | var result = solution.Encode(23); 13 | Assert.AreEqual("1000", result); 14 | } 15 | 16 | [TestMethod] 17 | public void Encode_2() 18 | { 19 | var solution = new _1256_EncodeNumber(); 20 | var result = solution.Encode(107); 21 | Assert.AreEqual("101100", result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/1251-1300/1271-Hexspeak-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1271_Hexspeak_Test 7 | { 8 | [TestMethod] 9 | public void ToHexspeak_1() 10 | { 11 | var solution = new _1271_Hexspeak(); 12 | var result = solution.ToHexspeak("257"); 13 | Assert.AreEqual("IOI", result); 14 | } 15 | 16 | [TestMethod] 17 | public void ToHexspeak_2() 18 | { 19 | var solution = new _1271_Hexspeak(); 20 | var result = solution.ToHexspeak("3"); 21 | Assert.AreEqual("ERROR", result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode/0151-0200/0171-ExcelSheetColumnNumber.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 72ms 3 | // Memory Usage: 24.8 MB 4 | // Link: https://leetcode.com/submissions/detail/342807812/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0171_ExcelSheetColumnNumber 10 | { 11 | public int TitleToNumber(string s) 12 | { 13 | var value = 0; 14 | foreach (var ch in s) 15 | { 16 | value *= 26; 17 | value += ch - 'A' + 1; 18 | } 19 | 20 | return value; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LeetCode/0201-0250/0226-InvertBinaryTree.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 92ms 3 | // Memory Usage: 22.9 MB 4 | // Link: https://leetcode.com/submissions/detail/262168882/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0226_InvertBinaryTree 10 | { 11 | public TreeNode InvertTree(TreeNode root) 12 | { 13 | if (root == null) return null; 14 | 15 | var left = root.left; 16 | root.left = InvertTree(root.right); 17 | root.right = InvertTree(left); 18 | 19 | return root; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LeetCode.Test/0201-0250/0204-CountPrimes-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0204_CountPrimes_Test 7 | { 8 | [TestMethod] 9 | public void CountPrimes_1() 10 | { 11 | var solution = new _0204_CountPrimes(); 12 | var result = solution.CountPrimes(10); 13 | Assert.AreEqual(4, result); 14 | } 15 | 16 | [TestMethod] 17 | public void CountPrimes_2() 18 | { 19 | var solution = new _0204_CountPrimes(); 20 | var result = solution.CountPrimes(20); 21 | Assert.AreEqual(8, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0601-0650/0617-MergeTwoBinaryTrees-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0617_MergeTwoBinaryTrees_Test 7 | { 8 | [TestMethod] 9 | public void MergeTreesTest() 10 | { 11 | var root1 = TestHelper.GenerateTree(new int?[] { 1, 3, 2, 5 }); 12 | var root2 = TestHelper.GenerateTree(new int?[] { 2, 1, 3, null, 4, null, 7 }); 13 | 14 | var solution = new _0617_MergeTwoBinaryTrees(); 15 | var result = solution.MergeTrees(root1, root2); 16 | AssertHelper.AssertTree(new int?[] { 3, 4, 5, 5, 4, null, 7 }, result); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode.Test/0601-0650/0624-MaximumDistanceInArrays-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using System.Collections.Generic; 3 | 4 | namespace LeetCode.Test 5 | { 6 | [TestClass] 7 | public class _0624_MaximumDistanceInArrays_Test 8 | { 9 | [TestMethod] 10 | public void MaxDistance_1() 11 | { 12 | var solution = new _0624_MaximumDistanceInArrays(); 13 | var result = solution.MaxDistance(new List>() 14 | { 15 | new int[] { 1, 2, 3 }, 16 | new int[] { 4, 5 }, 17 | new int[] { 1, 2, 3 }, 18 | }); 19 | Assert.AreEqual(4, result); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LeetCode.Test/1101-1150/1128-NumberOfEquivalentDominoPairs-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1128_NumberOfEquivalentDominoPairs_Test 7 | { 8 | [TestMethod] 9 | public void NumEquivDominoPairs_1() 10 | { 11 | var solution = new _1128_NumberOfEquivalentDominoPairs(); 12 | var result = solution.NumEquivDominoPairs(new int[][] { 13 | new int[] { 1, 2 }, 14 | new int[] { 2, 1 }, 15 | new int[] { 3, 4 }, 16 | new int[] { 5, 6 }, 17 | }); 18 | Assert.AreEqual(1, result); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /LeetCode/0001-0050/050-Pow.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 48ms 3 | // Memory Usage: 15 MB 4 | // Link: https://leetcode.com/submissions/detail/367515347/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _050_Pow 10 | { 11 | public double MyPow(double x, int n) 12 | { 13 | if (x == 0 || x == 1) return x; 14 | if (n == 0) return 1; 15 | 16 | var temp = MyPow(x, n / 2); 17 | 18 | if (n % 2 == 0) return temp * temp; 19 | return n > 0 ? x * temp * temp : (temp * temp) / x; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LeetCode/0101-0150/114-FlattenBinaryTreeToLinkedList.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 112ms 3 | // Memory Usage: 4 | // Link: 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _114_FlattenBinaryTreeToLinkedList 10 | { 11 | TreeNode prev = null; 12 | 13 | public void Flatten(TreeNode root) 14 | { 15 | if (root == null) return; 16 | 17 | Flatten(root.right); 18 | Flatten(root.left); 19 | 20 | root.right = prev; 21 | root.left = null; 22 | prev = root; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /LeetCode/0351-0400/0389-FindTheDifference.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 84ms 3 | // Memory Usage: 24.7 MB 4 | // Link: https://leetcode.com/submissions/detail/342531497/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0389_FindTheDifference 10 | { 11 | public char FindTheDifference(string s, string t) 12 | { 13 | char result = (char)0; 14 | foreach (var ch in s) 15 | result ^= ch; 16 | foreach (var ch in t) 17 | result ^= ch; 18 | 19 | return result; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LeetCode/0551-0600/0598-RangeAdditionII.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 100ms 3 | // Memory Usage: 26.3 MB 4 | // Link: https://leetcode.com/submissions/detail/345101395/ 5 | //----------------------------------------------------------------------------- 6 | 7 | using System; 8 | 9 | namespace LeetCode 10 | { 11 | public class _0598_RangeAdditionII 12 | { 13 | public int MaxCount(int m, int n, int[][] ops) 14 | { 15 | foreach (var op in ops) 16 | { 17 | m = Math.Min(m, op[0]); 18 | n = Math.Min(n, op[1]); 19 | } 20 | 21 | return m * n; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0851-0900/0897-IncreasingOrderSearchTree-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0897_IncreasingOrderSearchTree_Test 7 | { 8 | [TestMethod] 9 | public void IncreasingBST_1() 10 | { 11 | var root = TestHelper.GenerateTree(new int?[] { 5, 3, 6, 2, 4, null, 8, 1, null, null, null, 7, 9 }); 12 | 13 | var solution = new _0897_IncreasingOrderSearchTree(); 14 | var result = solution.IncreasingBST(root); 15 | AssertHelper.AssertTree(new int?[] { 1, null, 2, null, 3, null, 4, null, 5, null, 6, null, 7, null, 8, null, 9 }, result); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode.Test/1101-1150/1134-ArmstrongNumber-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1134_ArmstrongNumber_Test 7 | { 8 | [TestMethod] 9 | public void IsArmstrong_1() 10 | { 11 | var solution = new _1134_ArmstrongNumber(); 12 | var result = solution.IsArmstrong(153); 13 | Assert.IsTrue(result); 14 | } 15 | 16 | [TestMethod] 17 | public void IsArmstrong_2() 18 | { 19 | var solution = new _1134_ArmstrongNumber(); 20 | var result = solution.IsArmstrong(123); 21 | Assert.IsFalse(result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode/0101-0150/112-PathSum.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 96ms 3 | // Memory Usage: 26 MB 4 | // Link: https://leetcode.com/submissions/detail/352942498/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _112_PathSum 10 | { 11 | public bool HasPathSum(TreeNode root, int sum) 12 | { 13 | if (root == null) return false; 14 | 15 | sum = sum - root.val; 16 | if (root.left == null && root.right == null) return sum == 0; 17 | return HasPathSum(root.left, sum) || HasPathSum(root.right, sum); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode/0151-0200/0151-ReverseWordsInAString.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 88ms 3 | // Memory Usage: 24.9 MB 4 | // Link: https://leetcode.com/submissions/detail/367112653/ 5 | //----------------------------------------------------------------------------- 6 | 7 | using System; 8 | using System.Linq; 9 | 10 | namespace LeetCode 11 | { 12 | public class _0151_ReverseWordsInAString 13 | { 14 | public string ReverseWords(string s) { 15 | if (string.IsNullOrWhiteSpace(s)) return string.Empty; 16 | 17 | return string.Join(" ", s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Reverse()); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode/0351-0400/0392-IsSubsequence.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 76ms 3 | // Memory Usage: 21.7 MB 4 | // Link: https://leetcode.com/submissions/detail/351443718/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0392_IsSubsequence 10 | { 11 | public bool IsSubsequence(string s, string t) 12 | { 13 | int i = 0, j = 0; 14 | while (i < s.Length && j < t.Length) 15 | { 16 | if (s[i] == t[j]) i++; 17 | j++; 18 | } 19 | 20 | return i == s.Length; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LeetCode/1451-1500/1480-RunningSumOf1DArray.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 240ms 3 | // Memory Usage: 30.8 MB 4 | // Link: https://leetcode.com/submissions/detail/358263097/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _1480_RunningSumOf1DArray 10 | { 11 | public int[] RunningSum(int[] nums) 12 | { 13 | var sum = nums[0]; 14 | for (int i = 1; i < nums.Length; i++) 15 | { 16 | sum += nums[i]; 17 | nums[i] = sum; 18 | } 19 | 20 | return nums; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LeetCode.Test/0401-0450/0441-ArrangingCoins-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0441_ArrangingCoins_Test 7 | { 8 | [TestMethod] 9 | public void ArrangeCoins_1() 10 | { 11 | var solution = new _0441_ArrangingCoins(); 12 | var result = solution.ArrangeCoins(5); 13 | Assert.AreEqual(2, result); 14 | } 15 | 16 | [TestMethod] 17 | public void ArrangeCoins_2() 18 | { 19 | var solution = new _0441_ArrangingCoins(); 20 | var result = solution.ArrangeCoins(8); 21 | Assert.AreEqual(3, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0801-0850/0812-LargestTriangleArea-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0812_LargestTriangleArea_Test 7 | { 8 | [TestMethod] 9 | public void LargestTriangleArea_1() 10 | { 11 | var solution = new _0812_LargestTriangleArea(); 12 | var result = solution.LargestTriangleArea(new int[][] { 13 | new int[] { 0, 0 }, 14 | new int[] { 0, 1 }, 15 | new int[] { 1, 0 }, 16 | new int[] { 0, 2 }, 17 | new int[] { 2, 0 }, 18 | }); 19 | Assert.AreEqual(2, result); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LeetCode/0051-0100/100-SameTree.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 84ms 3 | // Memory Usage: 24.3 MB 4 | // Link: https://leetcode.com/submissions/detail/343624904/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _100_SameTree 10 | { 11 | public bool IsSameTree(TreeNode p, TreeNode q) 12 | { 13 | if (q == null && p == null) return true; 14 | if (p == null || q == null) return false; 15 | if (p.val != q.val) return false; 16 | 17 | return IsSameTree(p.left, q.left) && IsSameTree(p.right, q.right); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode/0101-0150/0122-BestTimeToBuyAndSellStock2.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 92ms 3 | // Memory Usage: 25.1 MB 4 | // Link: https://leetcode.com/submissions/detail/340984824/ 5 | //----------------------------------------------------------------------------- 6 | 7 | using System; 8 | 9 | namespace LeetCode 10 | { 11 | public class _0122_BestTimeToBuyAndSellStock2 12 | { 13 | public int MaxProfit(int[] prices) 14 | { 15 | var maxProfit = 0; 16 | for (int i = 1; i < prices.Length; i++) 17 | maxProfit += Math.Max(0, prices[i] - prices[i - 1]); 18 | 19 | return maxProfit; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LeetCode/0451-0500/0453-MinimumMovesToEqualArrayElements.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 128ms 3 | // Memory Usage: 34.6 MB 4 | // Link: https://leetcode.com/submissions/detail/344799358/ 5 | //----------------------------------------------------------------------------- 6 | 7 | using System.Linq; 8 | 9 | namespace LeetCode 10 | { 11 | public class _0453_MinimumMovesToEqualArrayElements 12 | { 13 | public int MinMoves(int[] nums) 14 | { 15 | var min = nums.Min(); 16 | var count = 0; 17 | foreach (var num in nums) 18 | count += num - min; 19 | 20 | return count; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LeetCode.Test/0201-0250/0242-ValidAnagram-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0242_ValidAnagram_Test 7 | { 8 | [TestMethod] 9 | public void IsAnagram_1() 10 | { 11 | var solution = new _0242_ValidAnagram(); 12 | var result = solution.IsAnagram("anagram", "nagaram"); 13 | Assert.IsTrue(result); 14 | } 15 | 16 | [TestMethod] 17 | public void IsAnagram_2() 18 | { 19 | var solution = new _0242_ValidAnagram(); 20 | var result = solution.IsAnagram("rat", "car"); 21 | Assert.IsFalse(result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0401-0450/0415-AddStrings-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0415_AddStrings_Test 7 | { 8 | [TestMethod] 9 | public void AddStrings_1() 10 | { 11 | var solution = new _0415_AddStrings(); 12 | var result = solution.AddStrings("123", "987"); 13 | Assert.AreEqual("1110", result); 14 | } 15 | 16 | [TestMethod] 17 | public void AddStrings_2() 18 | { 19 | var solution = new _0415_AddStrings(); 20 | var result = solution.AddStrings("111", "1"); 21 | Assert.AreEqual("112", result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/1451-1500/1496-PathCrossing-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1496_PathCrossing_Test 7 | { 8 | [TestMethod] 9 | public void IsPathCrossing_1() 10 | { 11 | var solution = new _1496_PathCrossing(); 12 | var result = solution.IsPathCrossing("NES"); 13 | Assert.IsFalse(result); 14 | } 15 | 16 | [TestMethod] 17 | public void IsPathCrossing_2() 18 | { 19 | var solution = new _1496_PathCrossing(); 20 | var result = solution.IsPathCrossing("NESWW"); 21 | Assert.IsTrue(result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode/0451-0500/0461-HammingDistance.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 36ms 3 | // Memory Usage: 13.7 MB 4 | // Link: https://leetcode.com/submissions/detail/261547622/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0461_HammingDistance 10 | { 11 | public int HammingDistance(int x, int y) 12 | { 13 | var xor = x ^ y; 14 | var count = 0; 15 | while (xor > 0) 16 | { 17 | if ((xor & 1) == 1) count++; 18 | xor >>= 1; 19 | } 20 | return count; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LeetCode/1101-1150/1150-CheckIfANumberIsMajorityElementInASortedArray.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 84ms 3 | // Memory Usage: 24.4 MB 4 | // Link: https://leetcode.com/submissions/detail/336978508/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _1150_CheckIfANumberIsMajorityElementInASortedArray 10 | { 11 | public bool IsMajorityElement(int[] nums, int target) 12 | { 13 | var count = 0; 14 | foreach (var num in nums) 15 | if (num == target) count++; 16 | 17 | return count > nums.Length / 2; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode.Test/0951-1000/0951-FlipEquivalentBinaryTrees-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0951_FlipEquivalentBinaryTrees_Test 7 | { 8 | [TestMethod] 9 | public void FlipEquiv_1() 10 | { 11 | var root1 = TestHelper.GenerateTree(new int?[] { 1, 2, 3, 4, 5, 6, null, null, null, 7, 8 }); 12 | var root2 = TestHelper.GenerateTree(new int?[] { 1, 3, 2, null, 6, 4, 5, null, null, null, null, 8, 7 }); 13 | 14 | var solution = new _0951_FlipEquivalentBinaryTrees(); 15 | var result = solution.FlipEquiv(root1, root2); 16 | Assert.IsTrue(result); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LeetCode.Test/0651-0700/0657-RobotReturnToOrigin-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0657_RobotReturnToOrigin_Test 7 | { 8 | [TestMethod] 9 | public void JudgeCircle_1() 10 | { 11 | var solution = new _0657_RobotReturnToOrigin(); 12 | var result = solution.JudgeCircle("UD"); 13 | Assert.IsTrue(result); 14 | } 15 | 16 | [TestMethod] 17 | public void JudgeCircle_2() 18 | { 19 | var solution = new _0657_RobotReturnToOrigin(); 20 | var result = solution.JudgeCircle("LL"); 21 | Assert.IsFalse(result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0751-0800/0796-RotateString-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0796_RotateString_Test 7 | { 8 | [TestMethod] 9 | public void RotateString_1() 10 | { 11 | var solution = new _0796_RotateString(); 12 | var result = solution.RotateString("abcde", "cdeab"); 13 | Assert.IsTrue(result); 14 | } 15 | 16 | [TestMethod] 17 | public void RotateString_2() 18 | { 19 | var solution = new _0796_RotateString(); 20 | var result = solution.RotateString("abcde", "abced"); 21 | Assert.IsFalse(result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0301-0350/0322-CoinChange-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0322_CoinChange_Test 7 | { 8 | [TestMethod] 9 | public void CoinChange_1() 10 | { 11 | var solution = new _0322_CoinChange(); 12 | var result = solution.CoinChange(new int[] { 1, 2, 5 }, 11); 13 | Assert.AreEqual(3, result); 14 | } 15 | 16 | [TestMethod] 17 | public void CoinChange_2() 18 | { 19 | var solution = new _0322_CoinChange(); 20 | var result = solution.CoinChange(new int[] { 2 }, 3); 21 | Assert.AreEqual(-1, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0351-0400/0392-IsSubsequence-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0392_IsSubsequence_Test 7 | { 8 | [TestMethod] 9 | public void IsSubsequence_1() 10 | { 11 | var solution = new _0392_IsSubsequence(); 12 | var result = solution.IsSubsequence("abc", "ahbgdc"); 13 | Assert.IsTrue(result); 14 | } 15 | 16 | [TestMethod] 17 | public void IsSubsequence_2() 18 | { 19 | var solution = new _0392_IsSubsequence(); 20 | var result = solution.IsSubsequence("axc", "ahbgdc"); 21 | Assert.IsFalse(result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0451-0500/0476-NumberComplement-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0476_NumberComplement_Test 7 | { 8 | [TestMethod] 9 | public void FindComplement_1() 10 | { 11 | var solution = new _0476_NumberComplement(); 12 | var result = solution.FindComplement(5); 13 | Assert.AreEqual(2, result); 14 | } 15 | 16 | [TestMethod] 17 | public void FindComplement_2() 18 | { 19 | var solution = new _0476_NumberComplement(); 20 | var result = solution.FindComplement(1); 21 | Assert.AreEqual(0, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/1101-1150/1137-NThTribonacciNumber-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1137_NThTribonacciNumber_Test 7 | { 8 | [TestMethod] 9 | public void Tribonacci_1() 10 | { 11 | var solution = new _1137_NThTribonacciNumber(); 12 | var result = solution.Tribonacci(4); 13 | Assert.AreEqual(4, result); 14 | } 15 | 16 | [TestMethod] 17 | public void Tribonacci_2() 18 | { 19 | var solution = new _1137_NThTribonacciNumber(); 20 | var result = solution.Tribonacci(25); 21 | Assert.AreEqual(1389537, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode/0151-0200/0162-FindPeakElement.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 92ms 3 | // Memory Usage: 4 | // Link: 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0162_FindPeakElement 10 | { 11 | public int FindPeakElement(int[] nums) 12 | { 13 | int left = 0, right = nums.Length - 1; 14 | while (left < right) 15 | { 16 | var mid = left + (right - left) / 2; 17 | if (nums[mid] > nums[mid + 1]) right = mid; 18 | else left = mid + 1; 19 | } 20 | return left; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LeetCode/0501-0550/0518-CoinChange2.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 92ms 3 | // Memory Usage: 23.7 MB 4 | // Link: https://leetcode.com/submissions/detail/350512612/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0518_CoinChange2 10 | { 11 | public int Change(int amount, int[] coins) 12 | { 13 | var dp = new int[amount + 1]; 14 | dp[0] = 1; 15 | 16 | foreach (var coin in coins) 17 | for (int x = coin; x <= amount; x++) 18 | dp[x] += dp[x - coin]; 19 | 20 | return dp[amount]; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LeetCode.Test/0401-0450/0403-FrogJump-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0403_FrogJump_Test 7 | { 8 | [TestMethod] 9 | public void CanCross_1() 10 | { 11 | var solution = new _0403_FrogJump(); 12 | var result = solution.CanCross(new int[] { 0, 1, 3, 5, 6, 8, 12, 17 }); 13 | Assert.IsTrue(result); 14 | } 15 | 16 | [TestMethod] 17 | public void CanCross_2() 18 | { 19 | var solution = new _0403_FrogJump(); 20 | var result = solution.CanCross(new int[] { 0, 1, 2, 3, 4, 8, 9, 11 }); 21 | Assert.IsFalse(result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0501-0550/0541-ReverseStringII-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0541_ReverseStringII_Test 7 | { 8 | [TestMethod] 9 | public void ReverseStr_1() 10 | { 11 | var solution = new _0541_ReverseStringII(); 12 | var result = solution.ReverseStr("abcdefg", 2); 13 | Assert.AreEqual("bacdfeg", result); 14 | } 15 | 16 | [TestMethod] 17 | public void ReverseStr_2() 18 | { 19 | var solution = new _0541_ReverseStringII(); 20 | var result = solution.ReverseStr("a", 2); 21 | Assert.AreEqual("a", result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0801-0850/0807-MaxIncreaseToKeepCitySkyline-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0807_MaxIncreaseToKeepCitySkyline_Test 7 | { 8 | [TestMethod] 9 | public void MaxIncreaseKeepingSkyline_1() 10 | { 11 | var solution = new _0807_MaxIncreaseToKeepCitySkyline(); 12 | var result = solution.MaxIncreaseKeepingSkyline(new int[][] { 13 | new int[] { 3, 0, 8, 4 }, 14 | new int[] { 2, 4, 5, 7 }, 15 | new int[] { 9, 2, 6, 3 }, 16 | new int[] { 0, 3, 1, 0 }, 17 | }); 18 | Assert.AreEqual(35, result); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /LeetCode/0051-0100/069-Sqrt(x).cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 44ms 3 | // Memory Usage: 14.7 MB 4 | // Link: https://leetcode.com/submissions/detail/359211280/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _069_Sqrt 10 | { 11 | public int MySqrt(int x) 12 | { 13 | if (x < 1) { return x; } 14 | 15 | double last = 0.0, result = 1.0; 16 | while (last != result) 17 | { 18 | last = result; 19 | result = (result + x / result) / 2; 20 | } 21 | 22 | return (int)last; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /LeetCode.Test/0051-0100/096-UniqueBinarySearchTree-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _096_UniqueBinarySearchTree_Test 7 | { 8 | [TestMethod] 9 | public void MyTestMethod() 10 | { 11 | var solution = new _096_UniqueBinarySearchTree(); 12 | var result = solution.NumTrees(0); 13 | Assert.AreEqual(1, result); 14 | result = solution.NumTrees(1); 15 | Assert.AreEqual(1, result); 16 | result = solution.NumTrees(2); 17 | Assert.AreEqual(2, result); 18 | result = solution.NumTrees(3); 19 | Assert.AreEqual(5, result); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LeetCode.Test/0301-0350/0348-DesignTicTacToe-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0348_DesignTicTacToe_Test 7 | { 8 | [TestMethod] 9 | public void TicTacToe_1() 10 | { 11 | var toe = new _0348_DesignTicTacToe(3); 12 | 13 | Assert.AreEqual(0, toe.Move(0, 0, 1)); 14 | Assert.AreEqual(0, toe.Move(0, 2, 2)); 15 | Assert.AreEqual(0, toe.Move(2, 2, 1)); 16 | Assert.AreEqual(0, toe.Move(1, 1, 2)); 17 | Assert.AreEqual(0, toe.Move(2, 0, 1)); 18 | Assert.AreEqual(0, toe.Move(1, 0, 2)); 19 | Assert.AreEqual(1, toe.Move(2, 1, 1)); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LeetCode.Test/0351-0400/0393-UTF8Validation-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0393_UTF8Validation_Test 7 | { 8 | [TestMethod] 9 | public void ValidUtf8_1() 10 | { 11 | var solution = new _0393_UTF8Validation(); 12 | var result = solution.ValidUtf8(new int[] { 197, 130, 1 }); 13 | Assert.IsTrue(result); 14 | } 15 | 16 | [TestMethod] 17 | public void ValidUtf8_2() 18 | { 19 | var solution = new _0393_UTF8Validation(); 20 | var result = solution.ValidUtf8(new int[] { 235, 140, 4 }); 21 | Assert.IsFalse(result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0751-0800/0800-SimilarRGBColor-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0800_SimilarRGBColor_Test 7 | { 8 | [TestMethod] 9 | public void SimilarRGB_1() 10 | { 11 | var solution = new _0800_SimilarRGBColor(); 12 | var result = solution.SimilarRGB("#09f166"); 13 | Assert.AreEqual("#11ee66", result); 14 | } 15 | 16 | [TestMethod] 17 | public void SimilarRGB_2() 18 | { 19 | var solution = new _0800_SimilarRGBColor(); 20 | var result = solution.SimilarRGB("#1c9e03"); 21 | Assert.AreEqual("#229900", result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0901-0950/0933-NumberOfRecentCalls-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0933_NumberOfRecentCalls_Test 7 | { 8 | [TestMethod] 9 | public void PingTest_1() 10 | { 11 | var solution = new _0933_NumberOfRecentCalls(); 12 | var result = solution.Ping(1); 13 | Assert.AreEqual(1, result); 14 | 15 | result = solution.Ping(100); 16 | Assert.AreEqual(2, result); 17 | 18 | result = solution.Ping(3001); 19 | Assert.AreEqual(3, result); 20 | 21 | result = solution.Ping(3002); 22 | Assert.AreEqual(3, result); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /LeetCode/0151-0200/0169-MajorityElement.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 104ms 3 | // Memory Usage: 28.8 MB 4 | // Link: https://leetcode.com/submissions/detail/261553087/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0169_MajorityElement 10 | { 11 | public int MajorityElement(int[] nums) 12 | { 13 | int count = 0, candidate = 0; 14 | foreach (var num in nums) 15 | { 16 | if (count == 0) candidate = num; 17 | count += (num == candidate) ? 1 : -1; 18 | } 19 | 20 | return candidate; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LeetCode/0501-0550/0520-DetectCapital.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 72ms 3 | // Memory Usage: 23.3 MB 4 | // Link: https://leetcode.com/submissions/detail/343638258/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0520_DetectCapital 10 | { 11 | public bool DetectCapitalUse(string word) 12 | { 13 | var upper = 0; 14 | foreach (var ch in word) 15 | if (ch >= 'A' && ch <= 'Z') 16 | upper++; 17 | 18 | if (upper == 1 && word[0] <= 'Z') return true; 19 | return upper == 0 || upper == word.Length; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LeetCode.Test/0351-0400/0374-GuessNumberHigherOrLower-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0374_GuessNumberHigherOrLower_Test 7 | { 8 | [TestMethod] 9 | public void GuessNumber_1() 10 | { 11 | var solution = new _0374_GuessNumberHigherOrLower(6); 12 | var result = solution.GuessNumber(10); 13 | Assert.AreEqual(6, result); 14 | } 15 | 16 | [TestMethod] 17 | public void GuessNumber_2() 18 | { 19 | var solution = new _0374_GuessNumberHigherOrLower(4); 20 | var result = solution.GuessNumber(6); 21 | Assert.AreEqual(4, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0651-0700/0683-KEmptySlots-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0683_KEmptySlots_Test 7 | { 8 | [TestMethod] 9 | public void KEmptySlotsTest_1() 10 | { 11 | var solution = new _0683_KEmptySlots(); 12 | var result = solution.KEmptySlots(new int[] { 1, 3, 2 }, 1); 13 | Assert.AreEqual(2, result); 14 | } 15 | 16 | [TestMethod] 17 | public void KEmptySlotsTest_2() 18 | { 19 | var solution = new _0683_KEmptySlots(); 20 | var result = solution.KEmptySlots(new int[] { 1, 2, 3 }, 1); 21 | Assert.AreEqual(-1, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0701-0750/0703-KthLargestElementInAStream-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0703_KthLargestElementInAStream_Test 7 | { 8 | [TestMethod] 9 | public void KthLargestTest_1() 10 | { 11 | int k = 3; 12 | int[] arr = new int[] { 4, 5, 8, 2 }; 13 | 14 | var solution = new _0703_KthLargestElementInAStream(k, arr); 15 | Assert.AreEqual(4, solution.Add(3)); 16 | Assert.AreEqual(5, solution.Add(5)); 17 | Assert.AreEqual(5, solution.Add(10)); 18 | Assert.AreEqual(8, solution.Add(9)); 19 | Assert.AreEqual(8, solution.Add(4)); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LeetCode.Test/0701-0750/0704-BinarySearch-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0704_BinarySearch_Test 7 | { 8 | [TestMethod] 9 | public void Search_1() 10 | { 11 | var solution = new _0704_BinarySearch(); 12 | var result = solution.Search(new int[] { -1, 0, 3, 5, 9, 12 }, 9); 13 | Assert.AreEqual(4, result); 14 | } 15 | 16 | [TestMethod] 17 | public void Search_2() 18 | { 19 | var solution = new _0704_BinarySearch(); 20 | var result = solution.Search(new int[] { -1, 0, 3, 5, 9, 12 }, 2); 21 | Assert.AreEqual(-1, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0901-0950/0901-OnlineStockSpan-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0901_OnlineStockSpan_Test 7 | { 8 | [TestMethod] 9 | public void OnlineStockSpanTest_1() 10 | { 11 | var solution = new _0901_OnlineStockSpan(); 12 | Assert.AreEqual(1, solution.Next(100)); 13 | Assert.AreEqual(1, solution.Next(80)); 14 | Assert.AreEqual(1, solution.Next(60)); 15 | Assert.AreEqual(2, solution.Next(70)); 16 | Assert.AreEqual(1, solution.Next(60)); 17 | Assert.AreEqual(4, solution.Next(75)); 18 | Assert.AreEqual(6, solution.Next(85)); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /LeetCode.Test/1151-1200/1197-MinimumKnightMoves-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1197_MinimumKnightMoves_Test 7 | { 8 | [TestMethod] 9 | public void MinKnightMovesTest_1() 10 | { 11 | var solution = new _1197_MinimumKnightMoves(); 12 | var result = solution.MinKnightMoves(2, 1); 13 | Assert.AreEqual(1, result); 14 | } 15 | 16 | [TestMethod] 17 | public void MinKnightMovesTest_2() 18 | { 19 | var solution = new _1197_MinimumKnightMoves(); 20 | var result = solution.MinKnightMoves(5, 5); 21 | Assert.AreEqual(4, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0151-0200/0172-FactorialTrailingZeroes-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0172_FactorialTrailingZeroes_Test 7 | { 8 | [TestMethod] 9 | public void TrailingZeroes_1() 10 | { 11 | var solution = new _0172_FactorialTrailingZeroes(); 12 | var result = solution.TrailingZeroes(3); 13 | Assert.AreEqual(0, result); 14 | } 15 | 16 | [TestMethod] 17 | public void TrailingZeroes_2() 18 | { 19 | var solution = new _0172_FactorialTrailingZeroes(); 20 | var result = solution.TrailingZeroes(5); 21 | Assert.AreEqual(1, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0151-0200/0190-ReverseBits-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0190_ReverseBits_Test 7 | { 8 | [TestMethod] 9 | public void reverseBits_1() 10 | { 11 | var solution = new _0190_ReverseBits(); 12 | var result = solution.reverseBits((uint)43261596); 13 | Assert.AreEqual((uint)964176192, result); 14 | } 15 | 16 | [TestMethod] 17 | public void reverseBits_2() 18 | { 19 | var solution = new _0190_ReverseBits(); 20 | var result = solution.reverseBits((uint)4294967293); 21 | Assert.AreEqual((uint)3221225471, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0451-0500/0475-Heaters-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0475_Heaters_Test 7 | { 8 | [TestMethod] 9 | public void FindRadius_1() 10 | { 11 | var solution = new _0475_Heaters(); 12 | var result = solution.FindRadius(new int[] { 1, 2, 3 }, new int[] { 2 }); 13 | Assert.AreEqual(1, result); 14 | } 15 | 16 | [TestMethod] 17 | public void FindRadius_2() 18 | { 19 | var solution = new _0475_Heaters(); 20 | var result = solution.FindRadius(new int[] { 1, 2, 3, 4 }, new int[] { 1, 4 }); 21 | Assert.AreEqual(1, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0551-0600/0551-StudentAttendanceRecordI-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0551_StudentAttendanceRecordI_Test 7 | { 8 | [TestMethod] 9 | public void CheckRecord_1() 10 | { 11 | var solution = new _0551_StudentAttendanceRecordI(); 12 | var result = solution.CheckRecord("PPALLP"); 13 | Assert.IsTrue(result); 14 | } 15 | 16 | [TestMethod] 17 | public void CheckRecord_2() 18 | { 19 | var solution = new _0551_StudentAttendanceRecordI(); 20 | var result = solution.CheckRecord("PPALLL"); 21 | Assert.IsFalse(result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode/0051-0100/070-ClimbingStairs.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 40ms 3 | // Memory Usage: 14.5 MB 4 | // Link: https://leetcode.com/submissions/detail/352318308/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _070_ClimbingStairs 10 | { 11 | public int ClimbStairs(int n) 12 | { 13 | if (n < 4) { return n; } 14 | 15 | int x1 = 2, x2 = 3, temp; 16 | for (int i = 4; i <= n; i++) 17 | { 18 | temp = x1 + x2; 19 | x1 = x2; 20 | x2 = temp; 21 | } 22 | 23 | return x2; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /LeetCode/1101-1150/1118-NumberOfDaysInAMonth.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 36ms 3 | // Memory Usage: 14.6 MB 4 | // Link: https://leetcode.com/submissions/detail/338640136/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _1118_NumberOfDaysInAMonth 10 | { 11 | public int NumberOfDays(int Y, int M) 12 | { 13 | var days = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 14 | 15 | var result = days[M - 1]; 16 | if ((Y % 400 == 0 || Y % 100 != 0 && Y % 4 == 0) && M == 2) 17 | result++; 18 | 19 | return result; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LeetCode.Test/0101-0150/0136-SingleNumber-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0136_SingleNumber_Test 7 | { 8 | [TestMethod] 9 | public void SingleNumberTest_1() 10 | { 11 | var solution = new _0136_SingleNumber(); 12 | var result = solution.SingleNumber(new int[] { 2, 2, 1 }); 13 | Assert.AreEqual(1, result); 14 | } 15 | 16 | [TestMethod] 17 | public void SingleNumberTest_2() 18 | { 19 | var solution = new _0136_SingleNumber(); 20 | var result = solution.SingleNumber(new int[] { 4, 1, 2, 1, 2 }); 21 | Assert.AreEqual(4, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0301-0350/0338-CountingBits-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0338_CountingBits_Test 7 | { 8 | [TestMethod] 9 | public void CountBits_1() 10 | { 11 | var solution = new _0338_CountingBits(); 12 | var result = solution.CountBits(2); 13 | AssertHelper.AssertArray(new int[] { 0, 1, 1 }, result); 14 | } 15 | 16 | [TestMethod] 17 | public void CountBits_2() 18 | { 19 | var solution = new _0338_CountingBits(); 20 | var result = solution.CountBits(5); 21 | AssertHelper.AssertArray(new int[] { 0, 1, 1, 2, 1, 2 }, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0551-0600/0567-PermutationInString-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0567_PermutationInString_Test 7 | { 8 | [TestMethod] 9 | public void CheckInclusion_1() 10 | { 11 | var solution = new _0567_PermutationInString(); 12 | var result = solution.CheckInclusion("ab" , "eidbaooo"); 13 | Assert.IsTrue(result); 14 | } 15 | 16 | [TestMethod] 17 | public void CheckInclusion_2() 18 | { 19 | var solution = new _0567_PermutationInString(); 20 | var result = solution.CheckInclusion("ab", "eidboaoo"); 21 | Assert.IsFalse(result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0601-0650/0647-PalindromicSubstrings-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0647_PalindromicSubstrings_Test 7 | { 8 | [TestMethod] 9 | public void CountSubstrings_1() 10 | { 11 | var solution = new _0647_PalindromicSubstrings(); 12 | var result = solution.CountSubstrings("abc"); 13 | Assert.AreEqual(3, result); 14 | } 15 | 16 | [TestMethod] 17 | public void CountSubstrings_2() 18 | { 19 | var solution = new _0647_PalindromicSubstrings(); 20 | var result = solution.CountSubstrings("aaa"); 21 | Assert.AreEqual(6, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0951-1000/0985-SumOfEvenNumbersAfterQueries-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0985_SumOfEvenNumbersAfterQueries_Test 7 | { 8 | [TestMethod] 9 | public void SumEvenAfterQueries_1() 10 | { 11 | var solution = new _0985_SumOfEvenNumbersAfterQueries(); 12 | var result = solution.SumEvenAfterQueries(new int[] { 1, 2, 3, 4 }, new int[][] { 13 | new int[] { 1, 0 }, 14 | new int[] { -3, 1 }, 15 | new int[] { -4, 0 }, 16 | new int[] { 2, 3 }, 17 | }); 18 | AssertHelper.AssertArray(new int[] { 8, 6, 2, 4 }, result); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /LeetCode/0101-0150/0137-SingleNumberII.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 96ms 3 | // Memory Usage: 24.9 MB 4 | // Link: https://leetcode.com/submissions/detail/357933065/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0137_SingleNumberII 10 | { 11 | public int SingleNumber(int[] nums) 12 | { 13 | int seenOnce = 0, seenTwice = 0; 14 | 15 | foreach (int num in nums) 16 | { 17 | seenOnce = ~seenTwice & (seenOnce ^ num); 18 | seenTwice = ~seenOnce & (seenTwice ^ num); 19 | } 20 | 21 | return seenOnce; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode/1351-1400/1389-CreateTargetArrayInTheGivenOrder.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 228ms 3 | // Memory Usage: 30 MB 4 | // Link: https://leetcode.com/submissions/detail/326840305/ 5 | //----------------------------------------------------------------------------- 6 | 7 | using System.Collections.Generic; 8 | 9 | namespace LeetCode 10 | { 11 | public class _1389_CreateTargetArrayInTheGivenOrder 12 | { 13 | public int[] CreateTargetArray(int[] nums, int[] index) 14 | { 15 | var result = new List(); 16 | for (int i = 0; i < nums.Length; i++) 17 | result.Insert(index[i], nums[i]); 18 | 19 | return result.ToArray(); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LeetCode.Test/0751-0800/0771-JewelsAndStones-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0771_JewelsAndStones_Test 7 | { 8 | [TestMethod] 9 | public void NumJewelsInStonesTest_1() 10 | { 11 | var solution = new _0771_JewelsAndStones(); 12 | var result = solution.NumJewelsInStones("aA", "aAAbbbb"); 13 | Assert.AreEqual(3, result); 14 | } 15 | 16 | [TestMethod] 17 | public void NumJewelsInStonesTest_2() 18 | { 19 | var solution = new _0771_JewelsAndStones(); 20 | var result = solution.NumJewelsInStones("z", "ZZ"); 21 | Assert.AreEqual(0, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/1151-1200/1175-PrimeArrangements-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1175_PrimeArrangements_Test 7 | { 8 | [TestMethod] 9 | public void NumPrimeArrangements_1() 10 | { 11 | var solution = new _1175_PrimeArrangements(); 12 | var result = solution.NumPrimeArrangements(5); 13 | Assert.AreEqual(12, result); 14 | } 15 | 16 | [TestMethod] 17 | public void NumPrimeArrangements_2() 18 | { 19 | var solution = new _1175_PrimeArrangements(); 20 | var result = solution.NumPrimeArrangements(100); 21 | Assert.AreEqual(682289015, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/1251-1300/1286-IteratorForCombination-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1286_IteratorForCombination_Test 7 | { 8 | [TestMethod] 9 | public void IteratorForCombination_1() 10 | { 11 | _1286_IteratorForCombination iterator = new _1286_IteratorForCombination("abc", 2); // creates the iterator. 12 | Assert.AreEqual("ab", iterator.Next()); 13 | Assert.IsTrue(iterator.HasNext()); 14 | Assert.AreEqual("ac", iterator.Next()); 15 | Assert.IsTrue(iterator.HasNext()); 16 | Assert.AreEqual("bc", iterator.Next()); 17 | Assert.IsFalse(iterator.HasNext()); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LeetCode/0201-0250/0217-ContainsDuplicate.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 112ms 3 | // Memory Usage: 31.4 MB 4 | // Link: https://leetcode.com/submissions/detail/342501480/ 5 | //----------------------------------------------------------------------------- 6 | 7 | using System.Collections.Generic; 8 | 9 | namespace LeetCode 10 | { 11 | public class _0217_ContainsDuplicate 12 | { 13 | public bool ContainsDuplicate(int[] nums) 14 | { 15 | var set = new HashSet(); 16 | foreach (var num in nums) 17 | { 18 | if (set.Contains(num)) return true; 19 | set.Add(num); 20 | } 21 | return false; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode/0751-0800/0760-FindAnagramMappings.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 236ms 3 | // Memory Usage: 30 MB 4 | // Link: https://leetcode.com/submissions/detail/326845848/ 5 | //----------------------------------------------------------------------------- 6 | 7 | using System.Collections.Generic; 8 | 9 | namespace LeetCode 10 | { 11 | public class _0760_FindAnagramMappings 12 | { 13 | public int[] AnagramMappings(int[] A, int[] B) 14 | { 15 | var list = new List(B); 16 | 17 | var result = new int[A.Length]; 18 | for (int i = 0; i < A.Length; i++) 19 | result[i] = list.IndexOf(A[i]); 20 | 21 | return result; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode/0851-0900/0876-MiddleOfTheLinkedList.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 84ms 3 | // Memory Usage: 23.8 MB 4 | // Link: https://leetcode.com/submissions/detail/321762403/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0876_MiddleOfTheLinkedList 10 | { 11 | public ListNode MiddleNode(ListNode head) 12 | { 13 | var fast = head; 14 | var slow = head; 15 | 16 | while (fast != null && fast.next != null) 17 | { 18 | fast = fast.next.next; 19 | slow = slow.next; 20 | } 21 | 22 | return slow; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /LeetCode.Test/0101-0150/0137-SingleNumberII-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0137_SingleNumberII_Test 7 | { 8 | [TestMethod] 9 | public void SingleNumber_1() 10 | { 11 | var solution = new _0137_SingleNumberII(); 12 | var result = solution.SingleNumber(new int[] { 2, 2, 3, 2 }); 13 | Assert.AreEqual(3, result); 14 | } 15 | 16 | [TestMethod] 17 | public void SingleNumber_2() 18 | { 19 | var solution = new _0137_SingleNumberII(); 20 | var result = solution.SingleNumber(new int[] { 0, 1, 0, 1, 0, 1, 99 }); 21 | Assert.AreEqual(99, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0151-0200/0179-LargestNumber-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0179_LargestNumber_Test 7 | { 8 | [TestMethod] 9 | public void LargestNumber_1() 10 | { 11 | var solution = new _0179_LargestNumber(); 12 | var result = solution.LargestNumber(new int[] { 10, 2 }); 13 | Assert.AreEqual("210", result); 14 | } 15 | 16 | [TestMethod] 17 | public void LargestNumber_2() 18 | { 19 | var solution = new _0179_LargestNumber(); 20 | var result = solution.LargestNumber(new int[] { 3, 30, 34, 5, 9 }); 21 | Assert.AreEqual("9534330", result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0201-0250/0201-BitwiseANDOfNumbersRange-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0201_BitwiseANDOfNumbersRange_Test 7 | { 8 | [TestMethod] 9 | public void RangeBitwiseAnd_1() 10 | { 11 | var solution = new _0201_BitwiseANDOfNumbersRange(); 12 | var result = solution.RangeBitwiseAnd(5, 7); 13 | Assert.AreEqual(4, result); 14 | } 15 | 16 | [TestMethod] 17 | public void RangeBitwiseAnd_2() 18 | { 19 | var solution = new _0201_BitwiseANDOfNumbersRange(); 20 | var result = solution.RangeBitwiseAnd(0, 1); 21 | Assert.AreEqual(0, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0351-0400/0366-FindLeavesOfBinaryTree-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | using System.Collections.Generic; 3 | 4 | namespace LeetCode.Test 5 | { 6 | [TestClass] 7 | public class _0366_FindLeavesOfBinaryTree_Test 8 | { 9 | [TestMethod] 10 | public void FindLeaves_1() 11 | { 12 | var root = TestHelper.GenerateTree(new int?[] { 1, 2, 3, 4, 5 }); 13 | 14 | var solution = new _0366_FindLeavesOfBinaryTree(); 15 | var result = solution.FindLeaves(root); 16 | AssertHelper.AssertList(new List>() { 17 | new int[] { 4, 5, 3 }, 18 | new int[] { 2 }, 19 | new int[] { 1 }, 20 | }, result); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LeetCode.Test/0751-0800/0777-SwapAdjacentInLRString-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0777_SwapAdjacentInLRString_Test 7 | { 8 | [TestMethod] 9 | public void CanTransformTest() 10 | { 11 | var solution = new _0777_SwapAdjacentInLRString(); 12 | var result = solution.CanTransform("RXXLRXRXL", "XRLXXRRLX"); 13 | Assert.IsTrue(result); 14 | } 15 | 16 | [TestMethod] 17 | public void CanTransformTest_2() 18 | { 19 | var solution = new _0777_SwapAdjacentInLRString(); 20 | var result = solution.CanTransform("X", "L"); 21 | Assert.IsFalse(result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode/0101-0150/111-MinimumDepthOfBinaryTree.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 96ms 3 | // Memory Usage: 26.1 MB 4 | // Link: https://leetcode.com/submissions/detail/358687367/ 5 | //----------------------------------------------------------------------------- 6 | 7 | using System; 8 | 9 | namespace LeetCode 10 | { 11 | public class _111_MinimumDepthOfBinaryTree 12 | { 13 | public int MinDepth(TreeNode root) 14 | { 15 | if (root == null) return 0; 16 | if (root.left == null) return MinDepth(root.right) + 1; 17 | if (root.right == null) return MinDepth(root.left) + 1; 18 | return Math.Min(MinDepth(root.left), MinDepth(root.right)) + 1; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /LeetCode/1051-1100/1085-SumOfDigitsInTheMinimumNumber.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 72ms 3 | // Memory Usage: 24.1 MB 4 | // Link: https://leetcode.com/submissions/detail/327836848/ 5 | //----------------------------------------------------------------------------- 6 | 7 | using System.Linq; 8 | 9 | namespace LeetCode 10 | { 11 | public class _1085_SumOfDigitsInTheMinimumNumber 12 | { 13 | public int SumOfDigits(int[] A) 14 | { 15 | var min = A.Min(); 16 | var sum = 0; 17 | while (min > 0) 18 | { 19 | sum += min % 10; 20 | min /= 10; 21 | } 22 | 23 | return sum % 2 == 0 ? 1 : 0; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /LeetCode.Test/0151-0200/0155-MinStack-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0155_MinStack_Test 7 | { 8 | [TestMethod] 9 | public void MinStackTest() 10 | { 11 | _0155_MinStack minStack = new _0155_MinStack(); 12 | 13 | minStack.Push(-2); 14 | minStack.Push(0); 15 | minStack.Push(-3); 16 | 17 | var result = minStack.GetMin(); 18 | Assert.AreEqual(-3, result); 19 | 20 | minStack.Pop(); 21 | result = minStack.Top(); 22 | Assert.AreEqual(0, result); 23 | 24 | result = minStack.GetMin(); 25 | Assert.AreEqual(-2, result); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /LeetCode.Test/0151-0200/0167-TwoSumII-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0167_TwoSumII_Test 7 | { 8 | [TestMethod] 9 | public void TwoSumTest_1() 10 | { 11 | var solution = new _0167_TwoSumII(); 12 | var result = solution.TwoSum(new int[] { 2, 7, 11, 15 }, 9); 13 | AssertHelper.AssertArray(new int[] { 1, 2 }, result); 14 | } 15 | 16 | [TestMethod] 17 | public void TwoSumTest_2() 18 | { 19 | var solution = new _0167_TwoSumII(); 20 | var result = solution.TwoSum(new int[] { 2, 7, 11, 15 }, 10); 21 | AssertHelper.AssertArray(new int[] { }, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0351-0400/0389-FindTheDifference-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0389_FindTheDifference_Test 7 | { 8 | [TestMethod] 9 | public void FindTheDifference_1() 10 | { 11 | var solution = new _0389_FindTheDifference(); 12 | var result = solution.FindTheDifference("abcd", "abcde"); 13 | Assert.AreEqual('e', result); 14 | } 15 | 16 | [TestMethod] 17 | public void FindTheDifference_2() 18 | { 19 | var solution = new _0389_FindTheDifference(); 20 | var result = solution.FindTheDifference("abcd", "bedac"); 21 | Assert.AreEqual('e', result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0601-0650/0605-CanPlaceFlowers-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0605_CanPlaceFlowers_Test 7 | { 8 | [TestMethod] 9 | public void CanPlaceFlowers_1() 10 | { 11 | var solution = new _0605_CanPlaceFlowers(); 12 | var result = solution.CanPlaceFlowers(new int[] { 1, 0, 0, 0, 1 }, 1); 13 | Assert.IsTrue(result); 14 | } 15 | 16 | [TestMethod] 17 | public void CanPlaceFlowers_2() 18 | { 19 | var solution = new _0605_CanPlaceFlowers(); 20 | var result = solution.CanPlaceFlowers(new int[] { 1, 0, 0, 0, 1 }, 2); 21 | Assert.IsFalse(result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0651-0700/0665-NonDecreasingArray-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0665_NonDecreasingArray_Test 7 | { 8 | [TestMethod] 9 | public void CheckPossibility_1() 10 | { 11 | var solution = new _0665_NonDecreasingArray(); 12 | var result = solution.CheckPossibility(new int[] { 4, 2, 3 }); 13 | Assert.IsTrue(result); 14 | } 15 | 16 | [TestMethod] 17 | public void CheckPossibility_2() 18 | { 19 | var solution = new _0665_NonDecreasingArray(); 20 | var result = solution.CheckPossibility(new int[] { 4, 2, 1 }); 21 | Assert.IsFalse(result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode/0251-0300/0263-UglyNumber.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 40ms 3 | // Memory Usage: 14.7 MB 4 | // Link: https://leetcode.com/submissions/detail/352933164/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0263_UglyNumber 10 | { 11 | public bool IsUgly(int num) 12 | { 13 | if (num <= 0) return false; 14 | 15 | while (num > 1 && (num % 2 == 0)) 16 | num /= 2; 17 | while (num > 1 && (num % 3 == 0)) 18 | num /= 3; 19 | while (num > 1 && (num % 5 == 0)) 20 | num /= 5; 21 | 22 | return num == 1; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /LeetCode/0301-0350/0344-ReverseString.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 388ms 3 | // Memory Usage: 33.5 MB 4 | // Link: https://leetcode.com/submissions/detail/262779243/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0344_ReverseString 10 | { 11 | public void ReverseString(char[] s) 12 | { 13 | if (s == null || s.Length == 0) return; 14 | 15 | int i = 0, j = s.Length - 1; 16 | while (i < j) 17 | { 18 | var temp = s[i]; 19 | s[i] = s[j]; 20 | s[j] = temp; 21 | 22 | i++; j--; 23 | } 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /LeetCode/0951-1000/0976-LargestPerimeterTriangle.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 128ms 3 | // Memory Usage: 32.7 MB 4 | // Link: https://leetcode.com/submissions/detail/338633310/ 5 | //----------------------------------------------------------------------------- 6 | 7 | using System; 8 | 9 | namespace LeetCode 10 | { 11 | public class _0976_LargestPerimeterTriangle 12 | { 13 | public int LargestPerimeter(int[] A) 14 | { 15 | Array.Sort(A); 16 | for (int i = A.Length - 3; i >= 0; i--) 17 | { 18 | if (A[i] + A[i + 1] > A[i + 2]) 19 | return A[i] + A[i + 1] + A[i + 2]; 20 | } 21 | 22 | return 0; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /LeetCode.Test/0351-0400/0375-GuessNumberHigherOrLowerII-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0375_GuessNumberHigherOrLowerII_Test 7 | { 8 | [TestMethod] 9 | public void GetMoneyAmountTest_1() 10 | { 11 | var solution = new _0375_GuessNumberHigherOrLowerII(); 12 | var result = solution.GetMoneyAmount(10); 13 | Assert.AreEqual(16, result); 14 | } 15 | 16 | [TestMethod] 17 | public void GetMoneyAmountTest_2() 18 | { 19 | var solution = new _0375_GuessNumberHigherOrLowerII(); 20 | var result = solution.GetMoneyAmount(5); 21 | Assert.AreEqual(6, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/1001-1050/1046-LastStoneWeight-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1046_LastStoneWeight_Test 7 | { 8 | [TestMethod] 9 | public void LastStoneWeightTest_1() 10 | { 11 | var solution = new _1046_LastStoneWeight(); 12 | var result = solution.LastStoneWeight(new int[] { 2, 7, 4, 1, 8, 1 }); 13 | Assert.AreEqual(1, result); 14 | } 15 | 16 | [TestMethod] 17 | public void LastStoneWeightTest_2() 18 | { 19 | var solution = new _1046_LastStoneWeight(); 20 | var result = solution.LastStoneWeight(new int[] { 1, 1 }); 21 | Assert.AreEqual(0, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/1201-1250/1217-PlayWithChips-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1217_PlayWithChips_Test 7 | { 8 | [TestMethod] 9 | public void MinCostToMoveChips_1() 10 | { 11 | var solution = new _1217_PlayWithChips(); 12 | var result = solution.MinCostToMoveChips(new int[] { 1, 2, 3 }); 13 | Assert.AreEqual(1, result); 14 | } 15 | 16 | [TestMethod] 17 | public void MinCostToMoveChips_2() 18 | { 19 | var solution = new _1217_PlayWithChips(); 20 | var result = solution.MinCostToMoveChips(new int[] { 2, 2, 2, 3, 3 }); 21 | Assert.AreEqual(2, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode/0251-0300/0283-MoveZeroes.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 240ms 3 | // Memory Usage: 31.8 MB 4 | // Link: https://leetcode.com/submissions/detail/319930583/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0283_MoveZeroes 10 | { 11 | public void MoveZeroes(int[] nums) 12 | { 13 | if (nums == null || nums.Length <= 1) return; 14 | 15 | int cur = 0; 16 | for (int i = 0; i < nums.Length; i++) 17 | if (nums[i] != 0) 18 | nums[cur++] = nums[i]; 19 | 20 | for (; cur < nums.Length; cur++) 21 | nums[cur] = 0; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode/1301-1350/1342-NumberOfStepsToReduceANumberToZero.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 40ms 3 | // Memory Usage: 14.5 MB 4 | // Link: https://leetcode.com/submissions/detail/326415854/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _1342_NumberOfStepsToReduceANumberToZero 10 | { 11 | public int NumberOfSteps(int num) 12 | { 13 | var step = 0; 14 | while (num > 0) 15 | { 16 | if ((num & 1) == 1) 17 | step++; 18 | num /= 2; 19 | step++; 20 | } 21 | return step > 0 ? step - 1 : 0; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0301-0350/0346-MovingAverageFromDataStream-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0346_MovingAverageFromDataStream_Test 7 | { 8 | [TestMethod] 9 | public void Next_1() 10 | { 11 | var solution = new _0346_MovingAverageFromDataStream(3); 12 | var result = solution.Next(1); 13 | Assert.AreEqual(1, result); 14 | 15 | result = solution.Next(10); 16 | Assert.AreEqual(5.5, result); 17 | 18 | result = solution.Next(3); 19 | Assert.AreEqual(4.66666666666667, result, 0.000001); 20 | 21 | result = solution.Next(5); 22 | Assert.AreEqual(6, result); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /LeetCode.Test/0451-0500/0495-TeemoAttacking-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0495_TeemoAttacking_Test 7 | { 8 | [TestMethod] 9 | public void FindPoisonedDuration_1() 10 | { 11 | var solution = new _0495_TeemoAttacking(); 12 | var result = solution.FindPoisonedDuration(new int[] { 1, 4 }, 2); 13 | Assert.AreEqual(4, result); 14 | } 15 | 16 | [TestMethod] 17 | public void FindPoisonedDuration_2() 18 | { 19 | var solution = new _0495_TeemoAttacking(); 20 | var result = solution.FindPoisonedDuration(new int[] { 1, 2 }, 2); 21 | Assert.AreEqual(3, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0551-0600/0560-SubarraySumEqualsK-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0560_SubarraySumEqualsK_Test 7 | { 8 | [TestMethod] 9 | public void SubarraySumTest_1() 10 | { 11 | var solution = new _0560_SubarraySumEqualsK(); 12 | var result = solution.SubarraySum(new int[] { 1, 1, 1 }, 2); 13 | Assert.AreEqual(2, result); 14 | } 15 | 16 | [TestMethod] 17 | public void SubarraySumTest_2() 18 | { 19 | var solution = new _0560_SubarraySumEqualsK(); 20 | var result = solution.SubarraySum(new int[] { 1, 1, 1, -1, 1 }, 3); 21 | Assert.AreEqual(2, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode/0351-0400/0387-FirstUniqueCharacterInAString.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 80ms 3 | // Memory Usage: 31.6 MB 4 | // Link: https://leetcode.com/submissions/detail/334972755/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0387_FirstUniqueCharacterInAString 10 | { 11 | public int FirstUniqChar(string s) 12 | { 13 | var counts = new int[26]; 14 | foreach (var ch in s) 15 | counts[ch - 'a']++; 16 | 17 | for (int i = 0; i < s.Length; i++) 18 | if (counts[s[i] - 'a'] == 1) 19 | return i; 20 | 21 | return -1; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode/1451-1500/1470-ShuffleTheArray.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 240ms 3 | // Memory Usage: 32.6 MB 4 | // Link: https://leetcode.com/submissions/detail/351033051/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _1470_ShuffleTheArray 10 | { 11 | public int[] Shuffle(int[] nums, int n) 12 | { 13 | var results = new int[nums.Length]; 14 | 15 | var index = 0; 16 | for (int i = 0, j = n; i < n; i++, j++) 17 | { 18 | results[index++] = nums[i]; 19 | results[index++] = nums[j]; 20 | } 21 | 22 | return results; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /LeetCode.Test/0151-0200/0162-FindPeakElement-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0162_FindPeakElement_Test 7 | { 8 | [TestMethod] 9 | public void FindPeakElementTest_1() 10 | { 11 | var solution = new _0162_FindPeakElement(); 12 | var result = solution.FindPeakElement(new int[] { 1, 2, 3, 1 }); 13 | Assert.AreEqual(2, result); 14 | } 15 | 16 | [TestMethod] 17 | public void FindPeakElementTest_2() 18 | { 19 | var solution = new _0162_FindPeakElement(); 20 | var result = solution.FindPeakElement(new int[] { 1, 2, 1, 3, 5, 6, 4 }); 21 | Assert.AreEqual(5, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0151-0200/0169-MajorityElement-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0169_MajorityElement_Test 7 | { 8 | [TestMethod] 9 | public void MajorityElementTest_1() 10 | { 11 | var solution = new _0169_MajorityElement(); 12 | var result = solution.MajorityElement(new int[] { 3, 2, 3 }); 13 | Assert.AreEqual(3, result); 14 | } 15 | 16 | [TestMethod] 17 | public void MajorityElementTest_2() 18 | { 19 | var solution = new _0169_MajorityElement(); 20 | var result = solution.MajorityElement(new int[] { 2, 2, 1, 1, 1, 2, 2 }); 21 | Assert.AreEqual(2, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0201-0250/0207-CourseSchedule-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0207_CourseSchedule_Test 7 | { 8 | [TestMethod] 9 | public void CanFinishTest_1() 10 | { 11 | var solution = new _0207_CourseSchedule(); 12 | var result = solution.CanFinish(2, new int[][] { new int[] { 1, 0 } }); 13 | Assert.IsTrue(result); 14 | } 15 | 16 | [TestMethod] 17 | public void CanFinishTest_2() 18 | { 19 | var solution = new _0207_CourseSchedule(); 20 | var result = solution.CanFinish(2, new int[][] { new int[] { 1, 0 }, new int[] { 0, 1 } }); 21 | Assert.IsFalse(result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0201-0250/0240-SearchA2DMatrixII-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0240_SearchA2DMatrixII_Test 7 | { 8 | [TestMethod] 9 | public void SearchMatrixTest_1() 10 | { 11 | var input = new int[,] 12 | { 13 | { 1, 4, 7, 11, 15 }, 14 | { 2, 5, 8, 12, 19 }, 15 | { 3, 6, 9, 16, 22 }, 16 | { 10, 13, 14, 17, 24 }, 17 | { 18, 21, 23, 26, 30 } 18 | }; 19 | 20 | var solution = new _0240_SearchA2DMatrixII(); 21 | Assert.IsTrue(solution.SearchMatrix(input, 5)); 22 | Assert.IsFalse(solution.SearchMatrix(input, 20)); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /LeetCode.Test/0551-0600/0588-DesignInMemoryFileSystem-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0588_DesignInMemoryFileSystem_Test 7 | { 8 | [TestMethod] 9 | public void DesignInMemoryFileSystem_1() 10 | { 11 | var solution = new _0588_DesignInMemoryFileSystem(); 12 | Assert.AreEqual(0, solution.Ls("/").Count); 13 | solution.Mkdir("/a/b/c"); 14 | solution.AddContentToFile("/a/b/c", "Hello"); 15 | var list = solution.Ls("/"); 16 | Assert.AreEqual(1, list.Count); 17 | Assert.AreEqual("a", list[0]); 18 | 19 | Assert.AreEqual("Hello", solution.ReadContentFromFile("/a/b/c/d")); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LeetCode.Test/0651-0700/0682-BaseballGame-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0682_BaseballGame_Test 7 | { 8 | [TestMethod] 9 | public void CalPoints_1() 10 | { 11 | var solution = new _0682_BaseballGame(); 12 | var result = solution.CalPoints(new string[] { "5", "2", "C", "D", "+" }); 13 | Assert.AreEqual(30, result); 14 | } 15 | 16 | [TestMethod] 17 | public void CalPoints_2() 18 | { 19 | var solution = new _0682_BaseballGame(); 20 | var result = solution.CalPoints(new string[] { "5", "-2", "4", "C", "D", "9", "+", "+" }); 21 | Assert.AreEqual(27, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0651-0700/0696-CountBinarySubstrings-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0696_CountBinarySubstrings_Test 7 | { 8 | [TestMethod] 9 | public void CountBinarySubstrings_1() 10 | { 11 | var solution = new _0696_CountBinarySubstrings(); 12 | var result = solution.CountBinarySubstrings("00110011"); 13 | Assert.AreEqual(6, result); 14 | } 15 | 16 | [TestMethod] 17 | public void CountBinarySubstrings_2() 18 | { 19 | var solution = new _0696_CountBinarySubstrings(); 20 | var result = solution.CountBinarySubstrings("10101"); 21 | Assert.AreEqual(4, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0801-0850/0846-HandOfStraights-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0846_HandOfStraights_Test 7 | { 8 | [TestMethod] 9 | public void IsNStraightHand_1() 10 | { 11 | var solution = new _0846_HandOfStraights(); 12 | var result = solution.IsNStraightHand(new int[] { 1, 2, 3, 6, 2, 3, 4, 7, 8 }, 3); 13 | Assert.IsTrue(result); 14 | } 15 | 16 | [TestMethod] 17 | public void IsNStraightHand_2() 18 | { 19 | var solution = new _0846_HandOfStraights(); 20 | var result = solution.IsNStraightHand(new int[] { 1, 2, 3, 4, 5 }, 4); 21 | Assert.IsFalse(result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/1101-1150/1108-DefangingAnIPAddress-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1108_DefangingAnIPAddress_Test 7 | { 8 | [TestMethod] 9 | public void DefangIPaddr_1() 10 | { 11 | var solution = new _1108_DefangingAnIPAddress(); 12 | var result = solution.DefangIPaddr("1.1.1.1"); 13 | Assert.AreEqual("1[.]1[.]1[.]1", result); 14 | } 15 | 16 | [TestMethod] 17 | public void DefangIPaddr_2() 18 | { 19 | var solution = new _1108_DefangingAnIPAddress(); 20 | var result = solution.DefangIPaddr("255.100.50.0"); 21 | Assert.AreEqual("255[.]100[.]50[.]0", result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode/0901-0950/0941-ValidMountainArray.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 116ms 3 | // Memory Usage: 33.2 MB 4 | // Link: https://leetcode.com/submissions/detail/359259406/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0941_ValidMountainArray 10 | { 11 | public bool ValidMountainArray(int[] A) 12 | { 13 | int i = 0, N = A.Length; 14 | 15 | while (i + 1 < N && A[i] < A[i + 1]) 16 | i++; 17 | 18 | if (i == 0 || i == N - 1) return false; 19 | 20 | while (i + 1 < N && A[i] > A[i + 1]) 21 | i++; 22 | 23 | return i == N - 1; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /LeetCode/1201-1250/1217-PlayWithChips.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 84ms 3 | // Memory Usage: 24 MB 4 | // Link: https://leetcode.com/submissions/detail/334985149/ 5 | //----------------------------------------------------------------------------- 6 | 7 | using System; 8 | 9 | namespace LeetCode 10 | { 11 | public class _1217_PlayWithChips 12 | { 13 | public int MinCostToMoveChips(int[] chips) 14 | { 15 | int odd = 0, even = 0; 16 | foreach (var chip in chips) 17 | { 18 | if (chip % 2 == 0) 19 | even++; 20 | else 21 | odd++; 22 | } 23 | 24 | return Math.Min(odd, even); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /LeetCode/1301-1350/1317-ConvertIntegerToTheSumOfTwoNoZeroIntegers.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 196ms 3 | // Memory Usage: 25.5 MB 4 | // Link: https://leetcode.com/submissions/detail/339139993/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _1317_ConvertIntegerToTheSumOfTwoNoZeroIntegers 10 | { 11 | public int[] GetNoZeroIntegers(int n) 12 | { 13 | int p1 = 1, p2 = n - 1; 14 | 15 | while (p1.ToString().Contains("0") || p2.ToString().Contains("0")) 16 | { 17 | p1++; 18 | p2--; 19 | } 20 | 21 | return new int[] { p1, p2 }; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0551-0600/0575-DistributeCandies-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0575_DistributeCandies_Test 7 | { 8 | [TestMethod] 9 | public void DistributeCandies_1() 10 | { 11 | var solution = new _0575_DistributeCandies(); 12 | var result = solution.DistributeCandies(new int[] { 1, 1, 2, 2, 3, 3 }); 13 | Assert.AreEqual(3, result); 14 | } 15 | 16 | [TestMethod] 17 | public void DistributeCandies_2() 18 | { 19 | var solution = new _0575_DistributeCandies(); 20 | var result = solution.DistributeCandies(new int[] { 1, 1, 2, 3 }); 21 | Assert.AreEqual(2, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0701-0750/0717-1BitAnd2BitCharacters-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0717_1BitAnd2BitCharacters_Test 7 | { 8 | [TestMethod] 9 | public void IsOneBitCharacter_1() 10 | { 11 | var solution = new _0717_1BitAnd2BitCharacters(); 12 | var result = solution.IsOneBitCharacter(new int[] { 1, 0, 0 }); 13 | Assert.IsTrue(result); 14 | } 15 | 16 | [TestMethod] 17 | public void IsOneBitCharacter_2() 18 | { 19 | var solution = new _0717_1BitAnd2BitCharacters(); 20 | var result = solution.IsOneBitCharacter(new int[] { 1, 1, 1, 0 }); 21 | Assert.IsFalse(result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0951-1000/0969-PancakeSorting-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0969_PancakeSorting_Test 7 | { 8 | [TestMethod] 9 | public void PancakeSort_1() 10 | { 11 | var solution = new _0969_PancakeSorting(); 12 | var result = solution.PancakeSort(new int[] { 3, 2, 4, 1 }); 13 | AssertHelper.AssertList(new int[] { 3, 4, 2, 3, 1, 2 }, result); 14 | } 15 | 16 | [TestMethod] 17 | public void PancakeSort_2() 18 | { 19 | var solution = new _0969_PancakeSorting(); 20 | var result = solution.PancakeSort(new int[] { 1, 2, 3 }); 21 | Assert.AreEqual(0, result.Count); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/1051-1100/1079-LetterTilePossibilities-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1079_LetterTilePossibilities_Test 7 | { 8 | [TestMethod] 9 | public void NumTilePossibilities_1() 10 | { 11 | var solution = new _1079_LetterTilePossibilities(); 12 | var result = solution.NumTilePossibilities("AAB"); 13 | Assert.AreEqual(8, result); 14 | } 15 | 16 | [TestMethod] 17 | public void NumTilePossibilities_2() 18 | { 19 | var solution = new _1079_LetterTilePossibilities(); 20 | var result = solution.NumTilePossibilities("AAABBC"); 21 | Assert.AreEqual(188, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode/0251-0300/0280-WiggleSort.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 260ms 3 | // Memory Usage: 33.4 MB 4 | // Link: https://leetcode.com/submissions/detail/369274380/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0280_WiggleSort 10 | { 11 | public void WiggleSort(int[] nums) 12 | { 13 | for (int i = 0; i < nums.Length - 1; i++) 14 | { 15 | if (!(i % 2 == 0 ^ nums[i] > nums[i + 1])) 16 | { 17 | var temp = nums[i]; 18 | nums[i] = nums[i + 1]; 19 | nums[i + 1] = temp; 20 | } 21 | } 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode/1201-1250/1221-SplitAStringInBalancedStrings.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 68ms 3 | // Memory Usage: 21.9 MB 4 | // Link: https://leetcode.com/submissions/detail/326842297/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _1221_SplitAStringInBalancedStrings 10 | { 11 | public int BalancedStringSplit(string s) 12 | { 13 | var count = 0; 14 | var result = 0; 15 | foreach (var ch in s) 16 | { 17 | count += ch == 'L' ? 1 : -1; 18 | if (count == 0) 19 | result++; 20 | } 21 | 22 | return result; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /LeetCode.Test/0351-0400/0387-FirstUniqueCharacterInAString-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0387_FirstUniqueCharacterInAString_Test 7 | { 8 | [TestMethod] 9 | public void FirstUniqChar_1() 10 | { 11 | var solution = new _0387_FirstUniqueCharacterInAString(); 12 | var result = solution.FirstUniqChar("leetcode"); 13 | Assert.AreEqual(0, result); 14 | } 15 | 16 | [TestMethod] 17 | public void FirstUniqChar_2() 18 | { 19 | var solution = new _0387_FirstUniqueCharacterInAString(); 20 | var result = solution.FirstUniqChar("loveleetcode"); 21 | Assert.AreEqual(2, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0751-0800/0765-CouplesHoldingHands-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0765_CouplesHoldingHands_Test 7 | { 8 | [TestMethod] 9 | public void MinSwapsCouplesTest_1() 10 | { 11 | var solution = new _0765_CouplesHoldingHands(); 12 | var result = solution.MinSwapsCouples(new int[] { 0, 2, 1, 3 }); 13 | Assert.AreEqual(1, result); 14 | } 15 | 16 | [TestMethod] 17 | public void MinSwapsCouplesTest_2() 18 | { 19 | var solution = new _0765_CouplesHoldingHands(); 20 | var result = solution.MinSwapsCouples(new int[] { 3, 2, 0, 1 }); 21 | Assert.AreEqual(0, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/1001-1050/1044-LongestDuplicateSubstring-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1044_LongestDuplicateSubstring_Test 7 | { 8 | [TestMethod] 9 | public void LongestDupSubstring_1() 10 | { 11 | var solution = new _1044_LongestDuplicateSubstring(); 12 | var result = solution.LongestDupSubstring("banana"); 13 | Assert.AreEqual("ana", result); 14 | } 15 | 16 | [TestMethod] 17 | public void LongestDupSubstring_2() 18 | { 19 | var solution = new _1044_LongestDuplicateSubstring(); 20 | var result = solution.LongestDupSubstring("abcd"); 21 | Assert.AreEqual("", result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/1051-1100/1099-TwoSumLessThanK-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1099_TwoSumLessThanK_Test 7 | { 8 | [TestMethod] 9 | public void TwoSumLessThanK_1() 10 | { 11 | var solution = new _1099_TwoSumLessThanK(); 12 | var result = solution.TwoSumLessThanK(new int[] { 34, 23, 1, 24, 75, 33, 54, 8 }, 60); 13 | Assert.AreEqual(58, result); 14 | } 15 | 16 | [TestMethod] 17 | public void TwoSumLessThanK_2() 18 | { 19 | var solution = new _1099_TwoSumLessThanK(); 20 | var result = solution.TwoSumLessThanK(new int[] { 10, 20, 30 }, 15); 21 | Assert.AreEqual(-1, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/1151-1200/1165-SingleRowKeyboard-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _1165_SingleRowKeyboard_Test 7 | { 8 | [TestMethod] 9 | public void CalculateTime_1() 10 | { 11 | var solution = new _1165_SingleRowKeyboard(); 12 | var result = solution.CalculateTime("abcdefghijklmnopqrstuvwxyz", "cba"); 13 | Assert.AreEqual(4, result); 14 | } 15 | 16 | [TestMethod] 17 | public void CalculateTime_2() 18 | { 19 | var solution = new _1165_SingleRowKeyboard(); 20 | var result = solution.CalculateTime("pqrstuvwxyzabcdefghijklmno", "leetcode"); 21 | Assert.AreEqual(73, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode/0001-0050/027-RemoveElement.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 236ms 3 | // Memory Usage: 30.1 MB 4 | // Link: https://leetcode.com/submissions/detail/351897187/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _027_RemoveElement 10 | { 11 | public int RemoveElement(int[] nums, int val) 12 | { 13 | var lastIndex = nums.Length - 1; 14 | 15 | for (int i = 0; i <= lastIndex; i++) 16 | { 17 | if (nums[i] == val) 18 | { 19 | nums[i--] = nums[lastIndex--]; 20 | } 21 | } 22 | 23 | return lastIndex + 1; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /LeetCode/0601-0650/0617-MergeTwoBinaryTrees.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 112ms 3 | // Memory Usage: 26.8 MB 4 | // Link: https://leetcode.com/submissions/detail/262171481/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0617_MergeTwoBinaryTrees 10 | { 11 | public TreeNode MergeTrees(TreeNode t1, TreeNode t2) 12 | { 13 | if (t1 == null) return t2; 14 | if (t2 == null) return t1; 15 | 16 | var newNode = new TreeNode(t1.val + t2.val); 17 | newNode.left = MergeTrees(t1.left, t2.left); 18 | newNode.right = MergeTrees(t1.right, t2.right); 19 | return newNode; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LeetCode/1001-1050/1029-TwoCityScheduling.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 88ms 3 | // Memory Usage: 24.7 MB 4 | // Link: https://leetcode.com/submissions/detail/348826564/ 5 | //----------------------------------------------------------------------------- 6 | 7 | using System; 8 | 9 | namespace LeetCode 10 | { 11 | public class _1029_TwoCityScheduling 12 | { 13 | public int TwoCitySchedCost(int[][] costs) 14 | { 15 | Array.Sort(costs, (a, b) => (a[0] - a[1]) - (b[0] - b[1])); 16 | 17 | var n = costs.Length / 2; 18 | var result = 0; 19 | for (int i = 0; i < n; i++) 20 | result += costs[i][0] + costs[i + n][1]; 21 | 22 | return result; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /LeetCode/1301-1350/1313-DecompressRunLengthEncodedList.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 236ms 3 | // Memory Usage: 33.4 MB 4 | // Link: https://leetcode.com/submissions/detail/326429951/ 5 | //----------------------------------------------------------------------------- 6 | 7 | using System.Collections.Generic; 8 | 9 | namespace LeetCode 10 | { 11 | public class _1313_DecompressRunLengthEncodedList 12 | { 13 | public int[] DecompressRLElist(int[] nums) 14 | { 15 | var results = new List(); 16 | for (int i = 0; i < nums.Length; i += 2) 17 | for (int j = 0; j < nums[i]; j++) 18 | results.Add(nums[i + 1]); 19 | 20 | return results.ToArray(); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /LeetCode.Test/0101-0150/0121-BestTimeToBuyAndSellStock-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0121_BestTimeToBuyAndSellStock_Test 7 | { 8 | [TestMethod] 9 | public void MaxProfitTest_1() 10 | { 11 | var solution = new _0121_BestTimeToBuyAndSellStock(); 12 | var result = solution.MaxProfit(new int[] { 7, 1, 5, 3, 6, 4 }); 13 | Assert.AreEqual(5, result); 14 | } 15 | 16 | [TestMethod] 17 | public void MaxProfitTest_2() 18 | { 19 | var solution = new _0121_BestTimeToBuyAndSellStock(); 20 | var result = solution.MaxProfit(new int[] { 7, 6, 4, 3, 1 }); 21 | Assert.AreEqual(0, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0501-0550/0523-ContinuousSubarraySum-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0523_ContinuousSubarraySum_Test 7 | { 8 | [TestMethod] 9 | public void CheckSubarraySum_1() 10 | { 11 | var solution = new _0523_ContinuousSubarraySum(); 12 | var result = solution.CheckSubarraySum(new int[] { 23, 2, 4, 6, 7 }, 6); 13 | Assert.IsTrue(result); 14 | } 15 | 16 | [TestMethod] 17 | public void CheckSubarraySum_2() 18 | { 19 | var solution = new _0523_ContinuousSubarraySum(); 20 | var result = solution.CheckSubarraySum(new int[] { 23, 2, 6, 4, 7 }, 6); 21 | Assert.IsTrue(result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0651-0700/0697-DegreeOfAnArray-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0697_DegreeOfAnArray_Test 7 | { 8 | [TestMethod] 9 | public void FindShortestSubArray_1() 10 | { 11 | var solution = new _0697_DegreeOfAnArray(); 12 | var result = solution.FindShortestSubArray(new int[] { 1, 2, 2, 3, 1 }); 13 | Assert.AreEqual(2, result); 14 | } 15 | 16 | [TestMethod] 17 | public void FindShortestSubArray_2() 18 | { 19 | var solution = new _0697_DegreeOfAnArray(); 20 | var result = solution.FindShortestSubArray(new int[] { 1, 2, 2, 3, 1, 4, 2 }); 21 | Assert.AreEqual(6, result); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode.Test/0851-0900/0895-MaximumFrequencyStack-Test.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace LeetCode.Test 4 | { 5 | [TestClass] 6 | public class _0895_MaximumFrequencyStack_Test 7 | { 8 | [TestMethod] 9 | public void FreqStackTest() 10 | { 11 | var freqStack = new _0895_MaximumFrequencyStack(); 12 | 13 | freqStack.Push(5); 14 | freqStack.Push(7); 15 | freqStack.Push(5); 16 | freqStack.Push(7); 17 | freqStack.Push(4); 18 | freqStack.Push(5); 19 | Assert.AreEqual(5, freqStack.Pop()); 20 | Assert.AreEqual(7, freqStack.Pop()); 21 | Assert.AreEqual(5, freqStack.Pop()); 22 | Assert.AreEqual(4, freqStack.Pop()); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /LeetCode/0151-0200/0190-ReverseBits.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 44ms 3 | // Memory Usage: 15 MB 4 | // Link: https://leetcode.com/submissions/detail/358731194/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _0190_ReverseBits 10 | { 11 | public uint reverseBits(uint n) 12 | { 13 | n = (n >> 16) | (n << 16); 14 | n = ((n & 0xff00ff00) >> 8) | ((n & 0x00ff00ff) << 8); 15 | n = ((n & 0xf0f0f0f0) >> 4) | ((n & 0x0f0f0f0f) << 4); 16 | n = ((n & 0xcccccccc) >> 2) | ((n & 0x33333333) << 2); 17 | n = ((n & 0xaaaaaaaa) >> 1) | ((n & 0x55555555) << 1); 18 | return n; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /LeetCode/0251-0300/0252-MeetingRooms.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 100ms 3 | // Memory Usage: 26.9 MB 4 | // Link: https://leetcode.com/submissions/detail/342752135/ 5 | //----------------------------------------------------------------------------- 6 | 7 | using System; 8 | 9 | namespace LeetCode 10 | { 11 | public class _0252_MeetingRooms 12 | { 13 | public bool CanAttendMeetings(int[][] intervals) 14 | { 15 | Array.Sort(intervals, (a, b) => a[0].CompareTo(b[0])); 16 | for (int i = 0; i < intervals.Length - 1; i++) 17 | { 18 | if (intervals[i][1] > intervals[i + 1][0]) 19 | return false; 20 | } 21 | 22 | return true; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /LeetCode/0251-0300/0274-HIndex.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 84ms 3 | // Memory Usage: 24.4 MB 4 | // Link: https://leetcode.com/submissions/detail/357948617/ 5 | //----------------------------------------------------------------------------- 6 | 7 | using System; 8 | 9 | namespace LeetCode 10 | { 11 | public class _0274_HIndex 12 | { 13 | public int HIndex(int[] citations) 14 | { 15 | int n = citations.Length; 16 | 17 | int[] papers = new int[n + 1]; 18 | foreach (int c in citations) 19 | papers[Math.Min(n, c)]++; 20 | 21 | int k = n; 22 | for (int s = papers[n]; k > s; s += papers[k]) 23 | k--; 24 | return k; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /LeetCode/0451-0500/0495-TeemoAttacking.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 124ms 3 | // Memory Usage: 36.7 MB 4 | // Link: https://leetcode.com/submissions/detail/409239239/ 5 | //----------------------------------------------------------------------------- 6 | 7 | using System; 8 | 9 | namespace LeetCode 10 | { 11 | public class _0495_TeemoAttacking 12 | { 13 | public int FindPoisonedDuration(int[] timeSeries, int duration) 14 | { 15 | int n = timeSeries.Length; 16 | if (n == 0) return 0; 17 | 18 | int total = 0; 19 | for (int i = 0; i < n - 1; ++i) 20 | total += Math.Min(timeSeries[i + 1] - timeSeries[i], duration); 21 | return total + duration; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode/0801-0850/0829-ConsecutiveNumbersSum.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 40ms 3 | // Memory Usage: 14.4 MB 4 | // Link: https://leetcode.com/submissions/detail/372046471/ 5 | //----------------------------------------------------------------------------- 6 | 7 | using System; 8 | 9 | namespace LeetCode 10 | { 11 | public class _0829_ConsecutiveNumbersSum 12 | { 13 | public int ConsecutiveNumbersSum(int N) 14 | { 15 | var upper = (int)(Math.Sqrt(2 * N + 0.25) - 0.5); 16 | 17 | var count = 0; 18 | for (int i = 1; i <= upper; i++) 19 | { 20 | N -= i; 21 | if (N % i == 0) count++; 22 | } 23 | 24 | return count; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /LeetCode/1301-1350/1332-RemovePalindromicSubsequences.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 68ms 3 | // Memory Usage: 21.8 MB 4 | // Link: https://leetcode.com/submissions/detail/338611018/ 5 | //----------------------------------------------------------------------------- 6 | 7 | namespace LeetCode 8 | { 9 | public class _1332_RemovePalindromicSubsequences 10 | { 11 | public int RemovePalindromeSub(string s) 12 | { 13 | if (string.IsNullOrEmpty(s)) return 0; 14 | int lo = 0, hi = s.Length - 1; 15 | while (lo < hi) 16 | { 17 | if (s[lo] != s[hi]) return 2; 18 | lo++; 19 | hi--; 20 | } 21 | 22 | return 1; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /LeetCode/1351-1400/1375-BulbSwitcherIII.cs: -------------------------------------------------------------------------------- 1 | //----------------------------------------------------------------------------- 2 | // Runtime: 164ms 3 | // Memory Usage: 39.9 MB 4 | // Link: https://leetcode.com/submissions/detail/370195128/ 5 | //----------------------------------------------------------------------------- 6 | 7 | using System; 8 | 9 | namespace LeetCode 10 | { 11 | public class _1375_BulbSwitcherIII 12 | { 13 | public int NumTimesAllBlue(int[] light) 14 | { 15 | int last_on = 0; 16 | int result = 0; 17 | 18 | for (int i = 0; i < light.Length; i++) 19 | { 20 | last_on = Math.Max(last_on, light[i]); 21 | if (last_on == i + 1) result++; 22 | } 23 | 24 | return result; 25 | } 26 | } 27 | } 28 | --------------------------------------------------------------------------------