├── .DS_Store ├── 0001-two-sum ├── 0001-two-sum.py ├── README.md └── algorithm.txt ├── 0002-add-two-numbers ├── 0002-add-two-numbers.cpp └── README.md ├── 0009-palindrome-number ├── 0009-palindrome-number.py └── README.md ├── 0013-roman-to-integer ├── 0013-roman-to-integer.py └── README.md ├── 0014-longest-common-prefix ├── 0014-longest-common-prefix.py └── README.md ├── 0020-valid-parentheses ├── 0020-valid-parentheses.py └── README.md ├── 0021-merge-two-sorted-lists ├── 0021-merge-two-sorted-lists.cpp ├── NOTES.md └── README.md ├── 0024-swap-nodes-in-pairs ├── 0024-swap-nodes-in-pairs.cpp ├── 0024-swap-nodes-in-pairs.py ├── NOTES.md └── README.md ├── 0028-find-the-index-of-the-first-occurrence-in-a-string ├── 0028-find-the-index-of-the-first-occurrence-in-a-string.cpp ├── README.md ├── haystackNeedle.cpp └── haystackNeedle.py ├── 0066-plus-one ├── 0066-plus-one.cpp └── README.md └── README.md /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zmoussam/leet-code-problems/70113e5c92166200aa3fe8fe06f90316390f4952/.DS_Store -------------------------------------------------------------------------------- /0001-two-sum/0001-two-sum.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def twoSum(self, nums, target): 3 | """ 4 | :type nums: List[int] 5 | :type target: int 6 | :rtype: List[int] 7 | """ 8 | hash_map = {} 9 | for i in range(len(nums)): 10 | needValue = target - nums[i] 11 | if needValue in hash_map: 12 | return [hash_map[needValue], i] 13 | else: 14 | hash_map[nums[i]] = i 15 | -------------------------------------------------------------------------------- /0001-two-sum/README.md: -------------------------------------------------------------------------------- 1 |
Given an array of integers nums
and an integer target
, return indices of the two numbers such that they add up to target
.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
4 | 5 |You can return the answer in any order.
6 | 7 |8 |
Example 1:
9 | 10 |11 | Input: nums = [2,7,11,15], target = 9 12 | Output: [0,1] 13 | Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. 14 |15 | 16 |
Example 2:
17 | 18 |19 | Input: nums = [3,2,4], target = 6 20 | Output: [1,2] 21 |22 | 23 |
Example 3:
24 | 25 |26 | Input: nums = [3,3], target = 6 27 | Output: [0,1] 28 |29 | 30 |
31 |
Constraints:
32 | 33 |2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
41 | Follow-up: Can you come up with an algorithm that is less than
O(n2)
time complexity?
--------------------------------------------------------------------------------
/0001-two-sum/algorithm.txt:
--------------------------------------------------------------------------------
1 | Given:
2 | nums:[int]
3 | target: int
4 | Return:
5 | array of indices adding io target
6 |
7 | -----------------------------------------
8 | for each num, index in nums:
9 | for each num2, index2 in nums:
10 | if num + num2 = target
11 | Return [index, index2]
12 | end
13 | end
14 | end
15 | ------------------------------------------
16 | ------------------------------------------
17 | prevValues = {}
18 | for each num, index in nums:
19 | neededValue = target - num;
20 | index2 = prevValues[neededValue]
21 | if index2 != null
22 | Return[index2, index]
23 | else
24 | prevValues[num] = index
25 | --------------------------------------------
--------------------------------------------------------------------------------
/0002-add-two-numbers/0002-add-two-numbers.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Definition for singly-linked list.
3 | * struct ListNode {
4 | * int val;
5 | * ListNode *next;
6 | * ListNode() : val(0), next(nullptr) {}
7 | * ListNode(int x) : val(x), next(nullptr) {}
8 | * ListNode(int x, ListNode *next) : val(x), next(next) {}
9 | * };
10 | */
11 | class Solution {
12 | public:
13 | ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
14 | ListNode *result = new ListNode(0, NULL);
15 | ListNode *r = result;
16 | int c = 0;
17 | while (l1 || l2 || c)
18 | {
19 | if (l1) r->val += l1->val;
20 | if (l2) r->val += l2->val;
21 | c = r->val / 10;
22 | r->val = r->val % 10;
23 | if (l1)
24 | l1 = l1->next;
25 | if (l2)
26 | l2 = l2->next;
27 | if (l1 || l2 || c != 0)
28 | r->next = new ListNode(c, NULL);
29 | r = r->next;
30 | }
31 | return result;
32 | }
33 |
34 | };
--------------------------------------------------------------------------------
/0002-add-two-numbers/README.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 contains a single digit. Add the two numbers and return the sum 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 |
Example 1:
7 |9 | Input: l1 = [2,4,3], l2 = [5,6,4] 10 | Output: [7,0,8] 11 | Explanation: 342 + 465 = 807. 12 |13 | 14 |
Example 2:
15 | 16 |17 | Input: l1 = [0], l2 = [0] 18 | Output: [0] 19 |20 | 21 |
Example 3:
22 | 23 |24 | Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] 25 | Output: [8,9,9,9,0,0,0,1] 26 |27 | 28 |
29 |
Constraints:
30 | 31 |[1, 100]
.0 <= Node.val <= 9
Given an integer x
, return true
if x
is a palindrome, and false
otherwise.
4 |
Example 1:
5 | 6 |7 | Input: x = 121 8 | Output: true 9 | Explanation: 121 reads as 121 from left to right and from right to left. 10 |11 | 12 |
Example 2:
13 | 14 |15 | Input: x = -121 16 | Output: false 17 | Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. 18 |19 | 20 |
Example 3:
21 | 22 |23 | Input: x = 10 24 | Output: false 25 | Explanation: Reads 01 from right to left. Therefore it is not a palindrome. 26 |27 | 28 |
29 |
Constraints:
30 | 31 |-231 <= x <= 231 - 1
36 | Follow up: Could you solve it without converting the integer to a string? -------------------------------------------------------------------------------- /0013-roman-to-integer/0013-roman-to-integer.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def romanToInt(self, s): 3 | """ 4 | :type s: str 5 | :rtype: int 6 | """ 7 | roman = { 8 | "I": 1, 9 | "V": 5, 10 | "X": 10, 11 | "L": 50, 12 | "C": 100, 13 | "D": 500, 14 | "M": 1000 15 | } 16 | result = 0 17 | l = len(s) 18 | for i in range(l): 19 | if i + 1 < l and roman[s[i]] < roman[s[i + 1]]: 20 | result -= roman[s[i]] 21 | else : 22 | result += roman[s[i]] 23 | return result -------------------------------------------------------------------------------- /0013-roman-to-integer/README.md: -------------------------------------------------------------------------------- 1 |
Roman numerals are represented by seven different symbols: I
, V
, X
, L
, C
, D
and M
.
4 | Symbol Value 5 | I 1 6 | V 5 7 | X 10 8 | L 50 9 | C 100 10 | D 500 11 | M 100012 | 13 |
For example, 2
is written as II
in Roman numeral, just two ones added together. 12
is written as XII
, which is simply X + II
. The number 27
is written as XXVII
, which is XX + V + II
.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII
. Instead, the number four is written as IV
. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX
. There are six instances where subtraction is used:
I
can be placed before V
(5) and X
(10) to make 4 and 9. X
can be placed before L
(50) and C
(100) to make 40 and 90. C
can be placed before D
(500) and M
(1000) to make 400 and 900.Given a roman numeral, convert it to an integer.
24 | 25 |26 |
Example 1:
27 | 28 |29 | Input: s = "III" 30 | Output: 3 31 | Explanation: III = 3. 32 |33 | 34 |
Example 2:
35 | 36 |37 | Input: s = "LVIII" 38 | Output: 58 39 | Explanation: L = 50, V= 5, III = 3. 40 |41 | 42 |
Example 3:
43 | 44 |45 | Input: s = "MCMXCIV" 46 | Output: 1994 47 | Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. 48 |49 | 50 |
51 |
Constraints:
52 | 53 |1 <= s.length <= 15
s
contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M')
.s
is a valid roman numeral in the range [1, 3999]
.Write a function to find the longest common prefix string amongst an array of strings.
2 | 3 |If there is no common prefix, return an empty string ""
.
6 |
Example 1:
7 | 8 |9 | Input: strs = ["flower","flow","flight"] 10 | Output: "fl" 11 |12 | 13 |
Example 2:
14 | 15 |16 | Input: strs = ["dog","racecar","car"] 17 | Output: "" 18 | Explanation: There is no common prefix among the input strings. 19 |20 | 21 |
22 |
Constraints:
23 | 24 |1 <= strs.length <= 200
0 <= strs[i].length <= 200
strs[i]
consists of only lowercase English letters.Given a string s
containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
An input string is valid if:
4 | 5 |12 |
Example 1:
13 | 14 |15 | Input: s = "()" 16 | Output: true 17 |18 | 19 |
Example 2:
20 | 21 |22 | Input: s = "()[]{}" 23 | Output: true 24 |25 | 26 |
Example 3:
27 | 28 |29 | Input: s = "(]" 30 | Output: false 31 |32 | 33 |
34 |
Constraints:
35 | 36 |1 <= s.length <= 104
s
consists of parentheses only '()[]{}'
.You are given the heads of two sorted linked lists list1
and list2
.
Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists.
4 | 5 |Return the head of the merged linked list.
6 | 7 |8 |
Example 1:
9 |Input: list1 = [1,2,4], list2 = [1,3,4] 11 | Output: [1,1,2,3,4,4] 12 |13 | 14 |
Example 2:
15 | 16 |Input: list1 = [], list2 = [] 17 | Output: [] 18 |19 | 20 |
Example 3:
21 | 22 |Input: list1 = [], list2 = [0] 23 | Output: [0] 24 |25 | 26 |
27 |
Constraints:
28 | 29 |[0, 50]
.-100 <= Node.val <= 100
list1
and list2
are sorted in non-decreasing order.Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.)
2 | 3 |4 |
Example 1:
5 |Input: head = [1,2,3,4] 7 | Output: [2,1,4,3] 8 |9 | 10 |
Example 2:
11 | 12 |Input: head = [] 13 | Output: [] 14 |15 | 16 |
Example 3:
17 | 18 |Input: head = [1] 19 | Output: [1] 20 |21 | 22 |
23 |
Constraints:
24 | 25 |[0, 100]
.0 <= Node.val <= 100
Given two strings needle
and haystack
, return the index of the first occurrence of needle
in haystack
, or -1
if needle
is not part of haystack
.
4 |
Example 1:
5 | 6 |7 | Input: haystack = "sadbutsad", needle = "sad" 8 | Output: 0 9 | Explanation: "sad" occurs at index 0 and 6. 10 | The first occurrence is at index 0, so we return 0. 11 |12 | 13 |
Example 2:
14 | 15 |16 | Input: haystack = "leetcode", needle = "leeto" 17 | Output: -1 18 | Explanation: "leeto" did not occur in "leetcode", so we return -1. 19 |20 | 21 |
22 |
Constraints:
23 | 24 |1 <= haystack.length, needle.length <= 104
haystack
and needle
consist of only lowercase English characters.You are given a large integer represented as an integer array digits
, where each digits[i]
is the ith
digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0
's.
Increment the large integer by one and return the resulting array of digits.
4 | 5 |6 |
Example 1:
7 | 8 |9 | Input: digits = [1,2,3] 10 | Output: [1,2,4] 11 | Explanation: The array represents the integer 123. 12 | Incrementing by one gives 123 + 1 = 124. 13 | Thus, the result should be [1,2,4]. 14 |15 | 16 |
Example 2:
17 | 18 |19 | Input: digits = [4,3,2,1] 20 | Output: [4,3,2,2] 21 | Explanation: The array represents the integer 4321. 22 | Incrementing by one gives 4321 + 1 = 4322. 23 | Thus, the result should be [4,3,2,2]. 24 |25 | 26 |
Example 3:
27 | 28 |29 | Input: digits = [9] 30 | Output: [1,0] 31 | Explanation: The array represents the integer 9. 32 | Incrementing by one gives 9 + 1 = 10. 33 | Thus, the result should be [1,0]. 34 |35 | 36 |
37 |
Constraints:
38 | 39 |1 <= digits.length <= 100
0 <= digits[i] <= 9
digits
does not contain any leading 0
's.