본문 바로가기

Programing/Algorithm

(16)
[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
[LeetCode] 5. Longest Palindromic Substring https://leetcode.com/problems/longest-palindromic-substring/description/ public class Solution{ public string LongestPalindrome(string s) { var answer = $"{s.First()}"; if (s.Length == 1) return answer; for (int i = 0; i = 0) && (s[left] == s[right])) GetLongestPalindrome(in s, left, right, ref answer); if (s[i] == s[right]) ..
[LeetCode] 4. Median of Two Sorted Arrays https://leetcode.com/problems/median-of-two-sorted-arrays/description/ public class Solution { public double FindMedianSortedArrays(int[] nums1, int[] nums2) { int totalLength = nums1.Length + nums2.Length; bool even = totalLength % 2 == 0; int halfLength = (totalLength) / 2; int index1 = 0; int index2 = 0; double currentNum = 0.0; double pr..
[LeetCode] 3. Longest Substring Without Repeating Characters https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ public class Solution { public int LengthOfLongestSubstring(string s) { int anwser = 0; int startIndex = 0; var set = new HashSet(); for (int i = 0; i
[LeetCode] 2. Add Two Numbers https://leetcode.com/problems/add-two-numbers/description/ public class Solution { public ListNode AddTwoNumbers(ListNode l1, ListNode l2) { var head = new ListNode(); var current = head; bool carry = false; while (((l1 != null) || (l2 != null)) || carry) { int sum = 0; if (carry) { ++sum; car..
[LeetCode] 1. Two Sum https://leetcode.com/problems/two-sum/description/ public class Solution { public int[] TwoSum(int[] nums, int target) { var dict = new Dictionary(); for (int i = 0; i