Leetcode/Top Interview 150 46

[Two Pointers][Easy] 125. Valid Palindrome

https://leetcode.com/problems/valid-palindrome/description  Q. 문자열 s 가 주어질 때, 문자와 숫자에 대해 palindrome한지 확인 하라.   Solution.  Palindrome check를 대상 문자일 때 수행한다.   코드.  bool isAlphabet(char cur){ if(cur>='A' && cur='a' && cur='0' && cur=0 && left=0 && !isAlphabet(s[right])) --right; if(left=0 && char.ToLower(s[left++])!=char.ToLower(s[right--])) return false; } ret..

[Array/String][Hard] 68. Text Justification

https://leetcode.com/problems/text-justification/description Q. 단어 집한 words가 주어진다. 이때 maxWidth와 규칙에 맞게 string을 출력하라.  Solution.  마지막 라인의 규칙 때문에 조금 예외처리가 들어가는 문제.   로직 자체는 큰 어려움이 없으나, 상황에 따른 처리들이 조금 필요하다.   코드 더보기 string getLine(string[] words, ref int idxStart, int maxWidth){ int len = 0; int idx = idxStart; List spaces = new List(); for(; idx maxWidth) { spaces.Re..

[Array/String][Easy] 28. Find the Index of the First Occurrence in a String

https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/description Q. 주어진 문자열 haystack에서 needle이 등장하는 첫번째 index를 반환하라.   Solution. haystack.IndexOf(needle);를 사용하면 한번에 찾아 질 것이다.  그렇지만, 로직대로 만들어 보면, needle과 같은 문자로 haystack이 시작하면 그 뒤로 끝까지 같나 확인 한다. 도중에 index 범위체크등을 넣어 준다.  더보기public int StrStr(string haystack, string needle) { if(haystack.Length =haystack.Length..

[Array/String][Medium] 6. Zigzag Conversion.

https://leetcode.com/problems/zigzag-conversion/description Q. string s가 주어질 때, 아래와 같이 지그재그로 변경하고 이후 변경된 s를 출력하라.   Solution.  우선 문제를 해석해 본다.   첫째열 string은 (세로로) 그대로 써 준다.  다음 역순으로 row버퍼에 접근하며 돌아가면서 row-2 개를 도중에 써 준다.   위 두 과정을 row 만큼의 버퍼에 s 의 길이가 다 할때까지 반복한다.   그리고 row대로 재구성된 s를 다시 이어 붙힌다.   로직대로 코드를 작성한다.  더보기 public string Convert(string s, int numRows) { List strings = new List(); for(..

[Array/String][Easy] 58. Length of Last Word

https://leetcode.com/problems/length-of-last-word/description Q. 주어진 문자열 s의 공백을 제외한 마지막 단어의 길이를 구하라.    Solution.  뒤에서 부터 공백을 제외한 문자를 counting 한다.   로직대로 코드를 작성한다.  public int LengthOfLastWord(string s) { int len = 0; bool started = false; for(int k = s.Length-1; k >= 0; --k) { if(s[k] == ' ') { if(started) return len; } else { ..

[Array/String][Medium] 12. Integer to Roman

https://leetcode.com/problems/integer-to-roman/descriptionQ. 주어진 정수를 로마 문자로 변경하라.   Solution.  정수를 로마 문자를 바꾸려면 우선, 4, 9, 40, 90 등에 대한 특수 문자에 대한 처리를 해야 한다.    또한 자리수 고려를 잘 해야 한다.   딱히 알고리즘이라고 할 것은 없고, 로직 전개대로 코드를 작성한다.  더보기public string IntToRoman(int num) { Dictionary dictStr = new Dictionary(); dictStr.Add(1, "I"); dictStr.Add(5, "V"); dictStr.Add(10, "X"); dictStr.Add(50, "L"); ..

[Array/String][Easy] 13. Roman To Integer

https://leetcode.com/problems/roman-to-integer/description Q. 주진 로마 문자를 숫자로 변환하라.  Solution이 문제는 1씩 모자라는 숫자들에 대해 특별 처리가 필요하며, 그것을 제외하면 특별한 내용은 없다.  더보기 public int RomanToInt(string s) { Dictionary dictValue = new Dictionary(); dictValue.Add("I", 1); dictValue.Add("V", 5); dictValue.Add("X", 10); dictValue.Add("L", 50); dictValue.Add("C", 100); dictValue.Add("D", 500); dic..

[Array/String][Medium] 80. Remove Duplicates from Sorted Array II

https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/description Q. 정렬된 정수배열 nums에서 해당 숫자를 최대 2번까지 nums에 출력하고 그 길이를 반환하라.  추가 메모리를 사용하지 말아라.  Solution.  기존 로직들과 매우 유사하며, 다만 2회 check 로직을 추가 한다.  public int RemoveDuplicates(int[] nums) { int idx = 1; int cnt = 1; int val = nums[0]; for(int q = 1; q   적절한 결과.

[Array/String][Easy] 26. Remove Duplicates from Sorted Array

https://leetcode.com/problems/remove-duplicates-from-sorted-array/description Q. 오름차순으로 정렬된 정수 배열 nums가 주어질때, 이 값을 중복을 제거하고 다시 nums에 출력하고 그 길이를 반환하라. 이 후 이 유효한 길이를 반환하라.  이때 추가 메모리를 사용하지 말아라. Solution.  바로 위의 문제와 아주 유사한 문제.   이미 정렬이 되어 있으므로, 중복이 없을 시만, 앞에서부터 overwrite 해 나간다.  public int RemoveDuplicates(int[] nums) { int idx = 1; int val = nums[0]; for(int k = 1; k   적절한 결과.