본문 바로가기

Programing/Algorithm

(16)
[LeetCode] 36. Valid Sudoku https://leetcode.com/problems/valid-sudoku/description/ public class Solution{ public bool IsValidSudoku(char[][] board) { var zoneSetArr = new HashSet[3, 3]; for (int i = 0; i (); } } for (int i = 0; i (); var rowSet = new HashSet(); for (int j = 0; j
[LeetCode] 34. Find First and Last Position of Element in Sorted Array https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/description/ public class Solution{ public int[] SearchRange(int[] nums, int target) { return new int[] { GetStartIndex(nums, target), GetEndIndex(nums, target) }; } private int GetStartIndex(int[] nums, int target) { int start = 0; int end = nums.Length - 1; int start..
[LeetCode] 33. Search in Rotated Sorted Array https://leetcode.com/problems/search-in-rotated-sorted-array/description/ public int Search(int[] nums, int target){ return Recursive(nums, 0, nums.Length - 1, target);}private int Recursive(int[] nums, int start, int end, int target){ int pivot = (start + end) / 2; if (nums[pivot] == target) return pivot; if (start == end) return -1; if (end - start == 1) { ..
[LeetCode] 22. Generate Parentheses https://leetcode.com/problems/generate-parentheses/description/ public class Solution{ public IList GenerateParenthesis(int n) { var answer = new List(); Func(answer, "", "(", 0, 0, n); return answer; } private void Func(IList answer, string current, string piece, int leftCount, int rightCount, int n) { if (piece == "(") ++leftCount; e..
[LeetCode] 19. Remove Nth Node From End of List https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/ public class Solution{ public ListNode RemoveNthFromEnd(ListNode head, int n) { int nodeCount = 0; var current = head; while (current != null) { ++nodeCount; current = current.next; } if (nodeCount - n == 0) return head.next; current ..
[LeetCode] 17. Letter Combinations of a Phone Number https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/ public class Solution{ public IList LetterCombinations(string digits) { if (string.IsNullOrEmpty(digits)) return new List(); var map = new Dictionary>() { ['2'] = new List() { "a", "b", "c" }, ['3'] = new List() { "d", "e", "f" }, ['4'] = new Lis..
[LeetCode] 15. 3Sum https://leetcode.com/problems/3sum/description/ public IList> ThreeSum(int[] nums){ var plusDict = new Dictionary(); var minusDict = new Dictionary(); int zeroCount = 0; foreach (int num in nums) { if (num == 0) { ++zeroCount; } if (num >= 0) { if (plusDict.ContainsKey(num) == false) plusDict[num] = 0; ..
[LeetCode] 11. Container With Most Water https://leetcode.com/problems/container-with-most-water/description/ public class Solution{ public int MaxArea(int[] height) { int maxArea = 0; int maxLeftHeight = 0; for (int i = 0; i height[i]) continue; maxLeftHeight = height[i]; int maxRightHeight = 0; for (int j = height.Length - 1; j > i; j--) { ..