본문 바로가기

Root

(25)
[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; ..
[Effective C#] 아이템1: 지역변수를 선언할 때는 var를 사용하는 것이 낫다 코드를 읽을 때 타입을 명시적으로 드러내야 하는 경우가 아니라면 var를 사용하는 것이 좋다. C# 언어가 익명 타입을 지원하기 위해서 타입을 암시적으로 선언할 수 있는 손쉬운 방법을 제공할 뿐만 아니라, 정확한 반환 타입을 알지 못한 채 올바르지 않은 타입을 명시적으로 지정하게 되면 득보다 실이 많기 때문이다. 예를 들어 IQueryable 컬렉션을 IEnumerable로 강제 형변환하게 되면 IQueryProvider가 제공하는 장점을 모두 잃게 된다. var f = GetMagicNumber();var total = 100 * f / 6; 다만 내장 숫자 타입을 선언할 때는 명시적으로 타입을 선언하는 편이 낫다.관련해서 위 코드를 보면, 컴파일러는 GetMagicNumber() 메서드의 반환 타입으..
adapter #1 https://www.ecourse.co.kr/courses/cpp_designpattern/lessons/adaptor/ C++ Design Pattern → adaptor - ecourse www.ecourse.co.kr  Adapter는 구조 패턴으로, 인터페이스를 클라이언트가 기대하는 형태의 인터페이스로 변환해주는 기능을 한다. class CoolText{ std::string text; //...public: CoolText(const std::string& text) : text(text) {} void show() { std::cout  문자열을 보관해 두었다가, 다양한 기능을 제공하여 문자열을 출력해주는 CoolText가 있고, 해당 클래스를 오래 전부터 사용해 왔다고 가정해 보자. str..
[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--) { ..
[LeetCode] 8. String to Integer (atoi) https://leetcode.com/problems/string-to-integer-atoi/description/ public class Solution{ public int MyAtoi(string s) { if (string.IsNullOrEmpty(s)) return 0; int start = 0; while ((start end) return 0; bool res = int.TryParse(s.Substring(start, end - start + 1), out int result); if (res == false) { return sign == 1..
[LeetCode] 7. Reverse Integer https://leetcode.com/problems/reverse-integer/description/ public class Solution{ public int Reverse(int x) { int answer = 0; int tmp = x > 0 ? 1 : -1; while (true) { int mod = x % 10; x = x / 10; if ((int.MaxValue - (mod * tmp) answer)) return 0; answer += mod; if (x == 0) bre..
[LeetCode] 6. Zigzag Conversion https://leetcode.com/problems/zigzag-conversion/description/ public class Solution{ public string Convert(string s, int numRows) { if (numRows == 1) return s; var sbs = new StringBuilder[numRows]; for (int i = 0; i