본문 바로가기

Root

(25)
[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
[Effective C#] 아이템3: 캐스트보다는 is, as가 좋다 형변환을 수행하는 경우 캐스팅을 사용하기보다 as 연산자를 사용하는 것이 좋다. as를 사용하는 편이 더 안전하기도 하거니와 런타임에 더 효율적으로 동작한다. 다만 as나 is 연산자를 사용하면 사용자 정의 형변환은 수행되지 않는다. as 연산자와 캐스팅의 가장 큰 차이는 사용자 정의 형변환을 어떻게 다루는가 하는 점이다. as나 is 연산자는 런타임에 객체의 타입을 확인하고 필요에 따라 박싱을 수행하는 것을 제외하고는 어떠한 작업도 수행하지 않는다. 임의의 객체를 다른 타입으로 형변환하려면 이 객체는 지정한 타입이거나 혹은 지정한 타입을 상속한 타입이어야 한다. 그 외의 경우는 모두 실패한다. 반면 캐스팅을 사용하는 경우에는 객체를 지정한 타입으로 변환하기 위해서 형변환 연산자가 개입될 수 있다.
[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) { ..
[Effective C#] 아이템2: const보다는 readonly가 좋다 컴파일타임 상수보다는 런타임 상수를 사용하라. 컴파일타임 상수가 약간 더 빠르긴 하지만 런타임 상수에 비해 유연성이 상당히 떨어진다. 컴파일타임 상수는 성능이 매우 중요하고 상수의 값이 절대로 바뀌지 않는 경우에만 제한적으로 사용하는 것이 좋다. 컴파일타임 상수와 비교되는 런타임 상수의 가장 중요한 차이는 상수의 값이 런타임에 평가된다는 점일 것이다. 런타임 상수를 참조하는 코드를 컴파일하면 컴파일타임 상수처럼 코드를 값으로 대체하지 않고, readonly 변수에 대한 참조 코드를 생성한다. 이러한 차이로 인해 간혹 호환성 문제가 발생하곤 한다. Infrastructure라는 어셈블리 내에 const와 readonly 필드를 다음과 같이 정의했다고 하자.public class UsefulValues{ ..
template method https://www.ecourse.co.kr/courses/cpp_designpattern/lessons/template-method/ C++ Design Pattern → template method - ecourse www.ecourse.co.kr  template method는 행위 패턴으로, 알고리즘의 처리 과정은 변경하지 않고 알고리즘의 각 단계의 처리를 파생 클래스에서 재정의할 수 있게 한다. struct Shape{ virtual ~Shape() {} virtual void draw() = 0;};class Rect : Shape{public: void draw() override { //동일한 코드 //파생 클래스에 따라 달라지는 코드 //동일한 코드 }};class Circle ..
[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 ..