├── 001.two-sum ├── 001.two-sum.py ├── question.txt └── two-sum2.py ├── 002.add-two-numbers ├── 002.add-two-numbers.py └── question.md ├── 003.longest substring without repeating └── 003.longest substring without repeating.py ├── 004.median-of-two-sorted-arrays ├── 004.median-of-two-sorted-arrays.py └── question.md ├── 005.longest-palindromic-substring ├── 005.longest-palindromic-substring.py └── question.md ├── 006.zigzag-conversion ├── 006.zigzag-conversion.py └── question.md ├── 007.reverse-integer ├── 007.reverse-integer.py └── question.md ├── 008.string-to-integer-atoi ├── 008.string-to-integer-atoi.py └── question.md ├── 009.palindrome-number ├── 009.palindrome-number.py └── question.md ├── 010.regular-expression-matching ├── 010.regular-expression-matching.py └── question.md ├── 011.container-with-most-water ├── 011 .container-with-most-water.py └── question.md ├── 012.integer-to-roman ├── 012.integer-to-roman.py └── question.md ├── 013.roman-to-integer ├── 013.roman-to-integer.py └── question.md ├── 014.longest-common-prefix ├── 014.longest-common-prefix.py └── question.md ├── 015.3sum ├── 3sum.py └── question.md ├── 016.3sum-closest ├── 016.3sum-closest.py └── question.md ├── 017.letter-combinations-of-a-phone-number ├── 017.letter-combinations-of-a-phone-number.py └── question.md ├── 018.4sum ├── 018.4sum.py └── question.md ├── 019.remove-nth-node-from-end-of-list ├── 019.emove-nth-node-from-end-of-list.py └── question.md ├── 020.valid-parentheses ├── No.20.valid-parentheses.py └── question.md ├── 021.merge-two-sorted-lists ├── 21.merge-two-sorted-lists.py └── question.md ├── 022.generate-parentheses ├── generate-parentheses.py └── question.md ├── 023.merge-k-sorted-lists ├── merge-k-sorted-lists.py └── question.md ├── 024.swap-nodes-in-pairs ├── question.md └── swap-nodes-in-pairs.py ├── 025.reverse-nodes-in-k-group ├── question.md └── reverse-nodes-in-k-group.py ├── 026.remove-duplicates-from-sorted-array ├── question.md └── remove-duplicates-from-sorted-array.py ├── 027.remove-element ├── question.md └── remove-element.py └── README.md /001.two-sum/001.two-sum.py: -------------------------------------------------------------------------------- 1 | #Сղѧpython 2 | class Solution: 3 | # @return a tuple, (index1, index2) 4 | def twoSum(self, num, target): 5 | dict = {} 6 | for i in xrange(len(num)): 7 | x = num[i] 8 | if target-x in dict: 9 | return (dict[target-x]+1, i+1) 10 | dict[x] = i 11 | -------------------------------------------------------------------------------- /001.two-sum/question.txt: -------------------------------------------------------------------------------- 1 | Given an array of integers, return indices of the two numbers such that they add up to a specific target. 2 | 3 | You may assume that each input would have exactly one solution, and you may not use the same element twice. 4 | 5 | 6 | Example: 7 | 8 | Given nums = [2, 7, 11, 15], target = 9, 9 | 10 | Because nums[0] + nums[1] = 2 + 7 = 9, 11 | return [0, 1]. 12 | 13 | -------------------------------------------------------------------------------- /001.two-sum/two-sum2.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jan1995/LeetCode/18189d00563429ca075b22de047db439e1506b53/001.two-sum/two-sum2.py -------------------------------------------------------------------------------- /002.add-two-numbers/002.add-two-numbers.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jan1995/LeetCode/18189d00563429ca075b22de047db439e1506b53/002.add-two-numbers/002.add-two-numbers.py -------------------------------------------------------------------------------- /002.add-two-numbers/question.md: -------------------------------------------------------------------------------- 1 | You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. 2 | 3 | You may assume the two numbers do not contain any leading zero, except the number 0 itself. 4 | 5 | 6 | Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) 7 | Output: 7 -> 0 -> 8 -------------------------------------------------------------------------------- /003.longest substring without repeating/003.longest substring without repeating.py: -------------------------------------------------------------------------------- 1 | # liangduansuojin 2 | # class Solution(object): 3 | # def lengthOfLongestSubstring(self, s): 4 | # """ 5 | # :type s: str 6 | # :rtype: int 7 | # """ 8 | # sLen = len(s) 9 | # if sLen == 0 or sLen == 1: 10 | # return sLen 11 | # 12 | # letters = list(s) 13 | # 14 | # hashSet = {} 15 | # hashSet[letters[0]] = 1 16 | # 17 | # start = 0 18 | # end = 1 19 | # 20 | # maxLen = 1 21 | # 22 | # while end != sLen: 23 | # letter = letters[end] 24 | # 25 | # if hashSet.has_key(letter) and hashSet[letter] > 0: 26 | # hashSet[letters[start]] -= 1 27 | # start += 1 28 | # continue 29 | # 30 | # hashSet[letter] = 1 31 | # end += 1 32 | # if end - start > maxLen: 33 | # maxLen = end - start 34 | # 35 | # return maxLen 36 | 37 | s = 'pwwkew' 38 | # sLen = len(s) 39 | # if sLen == 0 or sLen == 1: 40 | # print(sLen) 41 | # 42 | # letters = list(s) 43 | # print(letters) 44 | # hashSet = {} 45 | # hashSet[letters[0]] = 1 46 | # 47 | # start = 0 48 | # end = 1 49 | # 50 | # maxLen = 1 51 | # 52 | # while end != sLen: 53 | # letter = letters[end] 54 | # 55 | # if hashSet.has_key(letter) and hashSet[letter] > 0: 56 | # hashSet[letters[start]] -= 1 57 | # start += 1 58 | # continue 59 | # 60 | # hashSet[letter] = 1 61 | # end += 1 62 | # if end - start > maxLen: 63 | # maxLen = end - start 64 | # 65 | # print(maxLen) 66 | 67 | 68 | # max_len = 0 69 | # if (len(s) == 1 or len(s) == 0): 70 | # max_len = len(s) 71 | # for i in range(0,len(s)-1): 72 | # for j in range(i+1, len(s)): 73 | # if s[j] in s[i:j]: 74 | # if j-i > max_len: 75 | # right = j 76 | # left = i 77 | # max_len = right-left 78 | # break 79 | # elif j == len(s) - 1: 80 | # if max_len < j - i +1: 81 | # max_len = j - i + 1 82 | # print(max_len) 83 | # print(right,left) 84 | 85 | # indexDict = {} 86 | # maxLength = currMax = 0 87 | # for i in range(len(s)): 88 | # if s[i] in indexDict and i - indexDict[s[i]] - 1 <= currMax: 89 | # if maxLength < currMax: 90 | # maxLength = currMax 91 | # currMax = i - indexDict[s[i]] - 1 92 | # currMax = currMax + 1 93 | # indexDict[s[i]] = i 94 | # # if currMax > maxLength: 95 | # # maxLength = currMax 96 | # # print(maxLength) 97 | # print(maxLength if currMax < maxLength else currMax) -------------------------------------------------------------------------------- /004.median-of-two-sorted-arrays/004.median-of-two-sorted-arrays.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jan1995/LeetCode/18189d00563429ca075b22de047db439e1506b53/004.median-of-two-sorted-arrays/004.median-of-two-sorted-arrays.py -------------------------------------------------------------------------------- /004.median-of-two-sorted-arrays/question.md: -------------------------------------------------------------------------------- 1 | There are two sorted arrays nums1 and nums2 of size m and n respectively. 2 | 3 | Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 4 | 5 | Example 1: 6 | 7 | nums1 = [1, 3] 8 | nums2 = [2] 9 | 10 | The median is 2.0 11 | 12 | 13 | 14 | Example 2: 15 | 16 | nums1 = [1, 2] 17 | nums2 = [3, 4] 18 | 19 | The median is (2 + 3)/2 = 2.5 20 | 21 | -------------------------------------------------------------------------------- /005.longest-palindromic-substring/005.longest-palindromic-substring.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jan1995/LeetCode/18189d00563429ca075b22de047db439e1506b53/005.longest-palindromic-substring/005.longest-palindromic-substring.py -------------------------------------------------------------------------------- /005.longest-palindromic-substring/question.md: -------------------------------------------------------------------------------- 1 | Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. 2 | 3 | Example: 4 | 5 | Input: "babad" 6 | 7 | Output: "bab" 8 | 9 | Note: "aba" is also a valid answer. 10 | 11 | 12 | 13 | Example: 14 | 15 | Input: "cbbd" 16 | 17 | Output: "bb" 18 | 19 | -------------------------------------------------------------------------------- /006.zigzag-conversion/006.zigzag-conversion.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jan1995/LeetCode/18189d00563429ca075b22de047db439e1506b53/006.zigzag-conversion/006.zigzag-conversion.py -------------------------------------------------------------------------------- /006.zigzag-conversion/question.md: -------------------------------------------------------------------------------- 1 | 2 | The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) 3 | 4 | P A H N 5 | A P L S I I G 6 | Y I R 7 | 8 | 9 | And then read line by line: "PAHNAPLSIIGYIR" 10 | 11 | 12 | Write the code that will take a string and make this conversion given a number of rows: 13 | 14 | string convert(string text, int nRows); 15 | 16 | convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR". 17 | -------------------------------------------------------------------------------- /007.reverse-integer/007.reverse-integer.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jan1995/LeetCode/18189d00563429ca075b22de047db439e1506b53/007.reverse-integer/007.reverse-integer.py -------------------------------------------------------------------------------- /007.reverse-integer/question.md: -------------------------------------------------------------------------------- 1 | Reverse digits of an integer. 2 | 3 | 4 | Example1: x = 123, return 321 5 | Example2: x = -123, return -321 6 | 7 | 8 | click to show spoilers. 9 | 10 | Have you thought about this? 11 | 12 | Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! 13 | 14 | If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100. 15 | 16 | Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases? 17 | 18 | For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. 19 | 20 | 21 | 22 | 23 | 24 | Note: 25 | The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows. 26 | -------------------------------------------------------------------------------- /008.string-to-integer-atoi/008.string-to-integer-atoi.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jan1995/LeetCode/18189d00563429ca075b22de047db439e1506b53/008.string-to-integer-atoi/008.string-to-integer-atoi.py -------------------------------------------------------------------------------- /008.string-to-integer-atoi/question.md: -------------------------------------------------------------------------------- 1 | Implement atoi to convert a string to an integer. 2 | 3 | Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. 4 | 5 | 6 | Notes: 7 | It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front. 8 | 9 | 10 | Update (2015-02-10): 11 | The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition. 12 | 13 | 14 | spoilers alert... click to show requirements for atoi. 15 | 16 | Requirements for atoi: 17 | 18 | The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value. 19 | 20 | The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function. 21 | 22 | If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed. 23 | 24 | If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned. 25 | 26 | -------------------------------------------------------------------------------- /009.palindrome-number/009.palindrome-number.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jan1995/LeetCode/18189d00563429ca075b22de047db439e1506b53/009.palindrome-number/009.palindrome-number.py -------------------------------------------------------------------------------- /009.palindrome-number/question.md: -------------------------------------------------------------------------------- 1 | Determine whether an integer is a palindrome. Do this without extra space. 2 | 3 | click to show spoilers. 4 | 5 | Some hints: 6 | 7 | Could negative integers be palindromes? (ie, -1) 8 | 9 | If you are thinking of converting the integer to string, note the restriction of using extra space. 10 | 11 | You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case? 12 | 13 | There is a more generic way of solving this problem. 14 | 15 | -------------------------------------------------------------------------------- /010.regular-expression-matching/010.regular-expression-matching.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jan1995/LeetCode/18189d00563429ca075b22de047db439e1506b53/010.regular-expression-matching/010.regular-expression-matching.py -------------------------------------------------------------------------------- /010.regular-expression-matching/question.md: -------------------------------------------------------------------------------- 1 | Implement regular expression matching with support for '.' and '*'. 2 | 3 | 4 | '.' Matches any single character. 5 | '*' Matches zero or more of the preceding element. 6 | 7 | The matching should cover the entire input string (not partial). 8 | 9 | The function prototype should be: 10 | bool isMatch(const char *s, const char *p) 11 | 12 | Some examples: 13 | isMatch("aa","a") → false 14 | isMatch("aa","aa") → true 15 | isMatch("aaa","aa") → false 16 | isMatch("aa", "a*") → true 17 | isMatch("aa", ".*") → true 18 | isMatch("ab", ".*") → true 19 | isMatch("aab", "c*a*b") → true 20 | -------------------------------------------------------------------------------- /011.container-with-most-water/011 .container-with-most-water.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jan1995/LeetCode/18189d00563429ca075b22de047db439e1506b53/011.container-with-most-water/011 .container-with-most-water.py -------------------------------------------------------------------------------- /011.container-with-most-water/question.md: -------------------------------------------------------------------------------- 1 | Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. 2 | 3 | Note: You may not slant the container and n is at least 2. 4 | -------------------------------------------------------------------------------- /012.integer-to-roman/012.integer-to-roman.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jan1995/LeetCode/18189d00563429ca075b22de047db439e1506b53/012.integer-to-roman/012.integer-to-roman.py -------------------------------------------------------------------------------- /012.integer-to-roman/question.md: -------------------------------------------------------------------------------- 1 | Given an integer, convert it to a roman numeral. 2 | 3 | 4 | Input is guaranteed to be within the range from 1 to 3999. -------------------------------------------------------------------------------- /013.roman-to-integer/013.roman-to-integer.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jan1995/LeetCode/18189d00563429ca075b22de047db439e1506b53/013.roman-to-integer/013.roman-to-integer.py -------------------------------------------------------------------------------- /013.roman-to-integer/question.md: -------------------------------------------------------------------------------- 1 | Given a roman numeral, convert it to an integer. 2 | 3 | Input is guaranteed to be within the range from 1 to 3999. -------------------------------------------------------------------------------- /014.longest-common-prefix/014.longest-common-prefix.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jan1995/LeetCode/18189d00563429ca075b22de047db439e1506b53/014.longest-common-prefix/014.longest-common-prefix.py -------------------------------------------------------------------------------- /014.longest-common-prefix/question.md: -------------------------------------------------------------------------------- 1 | Write a function to find the longest common prefix string amongst an array of strings. 2 | -------------------------------------------------------------------------------- /015.3sum/3sum.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jan1995/LeetCode/18189d00563429ca075b22de047db439e1506b53/015.3sum/3sum.py -------------------------------------------------------------------------------- /015.3sum/question.md: -------------------------------------------------------------------------------- 1 | Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. 2 | 3 | Note: The solution set must not contain duplicate triplets. 4 | 5 | 6 | For example, given array S = [-1, 0, 1, 2, -1, -4], 7 | 8 | A solution set is: 9 | [ 10 | [-1, 0, 1], 11 | [-1, -1, 2] 12 | ] 13 | -------------------------------------------------------------------------------- /016.3sum-closest/016.3sum-closest.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jan1995/LeetCode/18189d00563429ca075b22de047db439e1506b53/016.3sum-closest/016.3sum-closest.py -------------------------------------------------------------------------------- /016.3sum-closest/question.md: -------------------------------------------------------------------------------- 1 | Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. 2 | 3 | 4 | For example, given array S = {-1 2 1 -4}, and target = 1. 5 | 6 | The sum that is closest to the target is 2. (-1 + 2 + 1 = 2). 7 | -------------------------------------------------------------------------------- /017.letter-combinations-of-a-phone-number/017.letter-combinations-of-a-phone-number.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jan1995/LeetCode/18189d00563429ca075b22de047db439e1506b53/017.letter-combinations-of-a-phone-number/017.letter-combinations-of-a-phone-number.py -------------------------------------------------------------------------------- /017.letter-combinations-of-a-phone-number/question.md: -------------------------------------------------------------------------------- 1 | Given a digit string, return all possible letter combinations that the number could represent. 2 | 3 | 4 | 5 | A mapping of digit to letters (just like on the telephone buttons) is given below. 6 | 7 | 8 | 9 | Input:Digit string "23" 10 | Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]. 11 | 12 | 13 | 14 | Note: 15 | Although the above answer is in lexicographical order, your answer could be in any order you want. 16 | -------------------------------------------------------------------------------- /018.4sum/018.4sum.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jan1995/LeetCode/18189d00563429ca075b22de047db439e1506b53/018.4sum/018.4sum.py -------------------------------------------------------------------------------- /018.4sum/question.md: -------------------------------------------------------------------------------- 1 | Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. 2 | 3 | Note: The solution set must not contain duplicate quadruplets. 4 | 5 | 6 | 7 | For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0. 8 | 9 | A solution set is: 10 | [ 11 | [-1, 0, 0, 1], 12 | [-2, -1, 1, 2], 13 | [-2, 0, 0, 2] 14 | ] 15 | -------------------------------------------------------------------------------- /019.remove-nth-node-from-end-of-list/019.emove-nth-node-from-end-of-list.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jan1995/LeetCode/18189d00563429ca075b22de047db439e1506b53/019.remove-nth-node-from-end-of-list/019.emove-nth-node-from-end-of-list.py -------------------------------------------------------------------------------- /019.remove-nth-node-from-end-of-list/question.md: -------------------------------------------------------------------------------- 1 | Given a linked list, remove the nth node from the end of list and return its head. 2 | 3 | 4 | For example, 5 | 6 | 7 | Given linked list: 1->2->3->4->5, and n = 2. 8 | 9 | After removing the second node from the end, the linked list becomes 1->2->3->5. 10 | 11 | 12 | 13 | Note: 14 | Given n will always be valid. 15 | Try to do this in one pass. 16 | -------------------------------------------------------------------------------- /020.valid-parentheses/No.20.valid-parentheses.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jan1995/LeetCode/18189d00563429ca075b22de047db439e1506b53/020.valid-parentheses/No.20.valid-parentheses.py -------------------------------------------------------------------------------- /020.valid-parentheses/question.md: -------------------------------------------------------------------------------- 1 | Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. 2 | 3 | The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not. 4 | -------------------------------------------------------------------------------- /021.merge-two-sorted-lists/21.merge-two-sorted-lists.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jan1995/LeetCode/18189d00563429ca075b22de047db439e1506b53/021.merge-two-sorted-lists/21.merge-two-sorted-lists.py -------------------------------------------------------------------------------- /021.merge-two-sorted-lists/question.md: -------------------------------------------------------------------------------- 1 | Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. -------------------------------------------------------------------------------- /022.generate-parentheses/generate-parentheses.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jan1995/LeetCode/18189d00563429ca075b22de047db439e1506b53/022.generate-parentheses/generate-parentheses.py -------------------------------------------------------------------------------- /022.generate-parentheses/question.md: -------------------------------------------------------------------------------- 1 | 2 | Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. 3 | 4 | 5 | 6 | For example, given n = 3, a solution set is: 7 | 8 | 9 | [ 10 | "((()))", 11 | "(()())", 12 | "(())()", 13 | "()(())", 14 | "()()()" 15 | ] 16 | -------------------------------------------------------------------------------- /023.merge-k-sorted-lists/merge-k-sorted-lists.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jan1995/LeetCode/18189d00563429ca075b22de047db439e1506b53/023.merge-k-sorted-lists/merge-k-sorted-lists.py -------------------------------------------------------------------------------- /023.merge-k-sorted-lists/question.md: -------------------------------------------------------------------------------- 1 | 2 | Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 3 | -------------------------------------------------------------------------------- /024.swap-nodes-in-pairs/question.md: -------------------------------------------------------------------------------- 1 | 2 | Given a linked list, swap every two adjacent nodes and return its head. 3 | 4 | 5 | 6 | For example, 7 | Given 1->2->3->4, you should return the list as 2->1->4->3. 8 | 9 | 10 | 11 | Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed. 12 | -------------------------------------------------------------------------------- /024.swap-nodes-in-pairs/swap-nodes-in-pairs.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jan1995/LeetCode/18189d00563429ca075b22de047db439e1506b53/024.swap-nodes-in-pairs/swap-nodes-in-pairs.py -------------------------------------------------------------------------------- /025.reverse-nodes-in-k-group/question.md: -------------------------------------------------------------------------------- 1 | 2 | Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. 3 | 4 | 5 | 6 | k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. 7 | 8 | You may not alter the values in the nodes, only nodes itself may be changed. 9 | 10 | Only constant memory is allowed. 11 | 12 | 13 | For example, 14 | Given this linked list: 1->2->3->4->5 15 | 16 | 17 | 18 | For k = 2, you should return: 2->1->4->3->5 19 | 20 | 21 | 22 | For k = 3, you should return: 3->2->1->4->5 23 | -------------------------------------------------------------------------------- /025.reverse-nodes-in-k-group/reverse-nodes-in-k-group.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode(object): 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.next = None 6 | 7 | class Solution(object): 8 | def reverseKGroup(self, head, k): 9 | """ 10 | :type head: ListNode 11 | :type k: int 12 | :rtype: ListNode 13 | """ 14 | def reverseList(head, k): 15 | pre = None 16 | cur = head 17 | while cur and k > 0: 18 | tmp = cur.next 19 | cur.next = pre 20 | pre = cur 21 | cur = tmp 22 | k -= 1 23 | head.next = cur 24 | return cur, pre 25 | 26 | length = 0 27 | p = head 28 | while p: 29 | length += 1 30 | p = p.next 31 | if length < k: 32 | return head 33 | step = length / k 34 | ret = None 35 | pre = None 36 | p = head 37 | while p and step: 38 | next, newHead = reverseList(p, k) 39 | if ret is None: 40 | ret = newHead 41 | if pre: 42 | pre.next = newHead 43 | pre = p 44 | p = next 45 | step -= 1 46 | return ret 47 | -------------------------------------------------------------------------------- /026.remove-duplicates-from-sorted-array/question.md: -------------------------------------------------------------------------------- 1 | 2 | Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. 3 | 4 | 5 | Do not allocate extra space for another array, you must do this in place with constant memory. 6 | 7 | 8 | 9 | For example, 10 | Given input array nums = [1,1,2], 11 | 12 | 13 | Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length. 14 | -------------------------------------------------------------------------------- /026.remove-duplicates-from-sorted-array/remove-duplicates-from-sorted-array.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jan1995/LeetCode/18189d00563429ca075b22de047db439e1506b53/026.remove-duplicates-from-sorted-array/remove-duplicates-from-sorted-array.py -------------------------------------------------------------------------------- /027.remove-element/question.md: -------------------------------------------------------------------------------- 1 | Given an array and a value, remove all instances of that value in place and return the new length. 2 | 3 | 4 | Do not allocate extra space for another array, you must do this in place with constant memory. 5 | 6 | The order of elements can be changed. It doesn't matter what you leave beyond the new length. 7 | 8 | 9 | Example: 10 | Given input array nums = [3,2,2,3], val = 3 11 | 12 | 13 | Your function should return length = 2, with the first two elements of nums being 2. -------------------------------------------------------------------------------- /027.remove-element/remove-element.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Jan1995/LeetCode/18189d00563429ca075b22de047db439e1506b53/027.remove-element/remove-element.py -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LeetCode 2 | 3 | 1.为了公众号读者查阅代码方便 ,小詹在公号更新后会稍作延迟将代码整理到本仓库 。 4 | 5 | 2.每一个文件夹对应着题目和代码解答 ,代码中小詹已经写了较清楚详细的注释 ,便于读者理解 。 6 | 7 | 3.关于每个单独题目的具体思路分析 ,可以关注个人公众号【小詹学python】查询 。 8 | 9 | 4.为方便 ,将公众号刷题目录链接附上 : 10 | https://mp.weixin.qq.com/s/uhNn1c2a-0TcJ7L4fslHIg 11 | --------------------------------------------------------------------------------