Leetcode 279

[Sliding Window][Hard] 30. Substring with Concatenation of All Words

https://leetcode.com/problems/substring-with-concatenation-of-all-words/description Q. 문자열 s와 단어 배열 words가 주어질 때, words를 조합해서 구성할 수 있는 문자열이 s에 존재하면, 그 시작 위치 index를 반환하라.  Solution.  우선 문자열을 확인 할 frequency buffer가 hash type으로 필요하다.  그리고 sub string을 left to right 로 sliding 해 가면서 check 한다.   도중에 실패하면, 바로 다음 위치에서 시작한다.  성공한 문자를 찾아도 시작지점 바로 다음 위치에서 시작한다.   로직을 코드로 작성한다.더보기bool IsStartWith(string s, i..

[Sliding Window][Medium] 209. Minimum Size Subarray Sum

https://leetcode.com/problems/minimum-size-subarray-sum/description  Q. 주어진 정수 배열 nums에 대해서 그 sub array 합이 target보다 크거나 같은, 최소 subarray 길이를 찾아라.    Solution 푸는 과정이 sliding window라는 것을 알면 쉬운 문제.   윈도우의 합이 target이상인 구간에서 길이를 최소 길이로 갱신하고,  left 를 당기며 구간 합에서 left를 제거한다.   위 로직의 연속. public int MinSubArrayLen(int target, int[] nums) { int left = 0; int right = 0; int sum = 0; int minLen = ..

[Two Pointers][Medium] 167. Two Sum II - Input Array is Sorted.

https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description  Q. 오름차순 정렬된 정수배열 numbers가 주어질 때, 두 수의 합이 target이 되는 두 index를 찾아라.   Solution.  정렬된 배열이 주어지면, 대다수 검색 방법이 정해져 있다.   Binary Search를 사용하도록 한다.   어떤 대상에 대해?  현재 값과 그 합이 target이 되는 수를 검색하면 된다.   코드를 작성해 본다. int findNum(int[] numbers, int target, int left, int right){ if(left > right) return -1; int mid = left + (right..

[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"); ..