```java
import java.util.HashMap;
import java.util.Map;

public int[] twoSum(int[] nums, int target) {
    Map<Integer, Integer> map = new HashMap<>();
    for (int i = 0; i < nums.length; i++) {
        int complement = target - nums[i];
        if (map.containsKey(complement)) {
            return new int[]{map.get(complement), i};
        }
        map.put(nums[i], i);
    }
    throw new IllegalArgumentException("No two sum solution");
}
```

```cpp
#include <vector>
#include <unordered_map>

std::vector<int> twoSum(std::vector<int>& nums, int target) {
    std::unordered_map<int, int> map;
    for (int i = 0; i < nums.size(); i++) {
        int complement = target - nums[i];
        if (map.find(complement) != map.end()) {
            return {map[complement], i};
        }
        map[nums[i]] = i;
    }
    return {};
}
```


```python
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def addTwoNumbers(l1, l2):
    dummy = ListNode(0)
    current = dummy
    carry = 0

    while l1 or l2 or carry:
        sum_val = (l1.val if l1 else 0) + (l2.val if l2 else 0) + carry
        carry = sum_val // 10
        current.next = ListNode(sum_val % 10)
        current = current.next

        if l1: l1 = l1.next
        if l2: l2 = l2.next

    return dummy.next
```

```java
public class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
}

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    ListNode dummy = new ListNode(0);
    ListNode current = dummy;
    int carry = 0;

    while (l1 != null || l2 != null || carry != 0) {
        int sum = (l1 != null ? l1.val : 0) + (l2 != null ? l2.val : 0) + carry;
        carry = sum / 10;
        current.next = new ListNode(sum % 10);
        current = current.next;

        if (l1 != null) l1 = l1.next;
        if (l2 != null) l2 = l2.next;
    }

    return dummy.next;
}
```


```cpp
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
    if (nums1.size() > nums2.size()) {
        return findMedianSortedArrays(nums2, nums1);
    }

    int x = nums1.size();
    int y = nums2.size();
    int low = 0;
    int high = x;

    while (low <= high) {
        int partitionX = (low + high) / 2;
        int partitionY = (x + y + 1) / 2 - partitionX;

        int maxLeftX = (partitionX == 0) ? INT_MIN : nums1[partitionX - 1];
        int minRightX = (partitionX == x) ? INT_MAX : nums1[partitionX];

        int maxLeftY = (partitionY == 0) ? INT_MIN : nums2[partitionY - 1];
        int minRightY = (partitionY == y) ? INT_MAX : nums2[partitionY];

        if (maxLeftX <= minRightY && maxLeftY <= minRightX) {
            if ((x + y) % 2 == 0) {
                return (double(max(maxLeftX, maxLeftY) + min(minRightX, minRightY))) / 2;
            } else {
                return double(max(maxLeftX, maxLeftY));
            }
        } else if (maxLeftX > minRightY) {
            high = partitionX - 1;
        } else {
            low = partitionX + 1;
        }
    }
    return 0;
}
```

```java
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
    if (nums1.length > nums2.length) {
        return findMedianSortedArrays(nums2, nums1);
    }

    int x = nums1.length;
    int y = nums2.length;
    int low = 0;
    int high = x;

    while (low <= high) {
        int partitionX = (low + high) / 2;
        int partitionY = (x + y + 1) / 2 - partitionX;

        int maxLeftX = (partitionX == 0) ? Integer.MIN_VALUE : nums1[partitionX - 1];
        int minRightX = (partitionX == x) ? Integer.MAX_VALUE : nums1[partitionX];

        int maxLeftY = (partitionY == 0) ? Integer.MIN_VALUE : nums2[partitionY - 1];
        int minRightY = (partitionY == y) ? Integer.MAX_VALUE : nums2[partitionY];

        if (maxLeftX <= minRightY && maxLeftY <= minRightX) {
            if ((x + y) % 2 == 0) {
                return (double)(Math.max(maxLeftX, maxLeftY) + Math.min(minRightX, minRightY)) / 2;
            } else {
                return (double)Math.max(maxLeftX, maxLeftY);
            }
        } else if (maxLeftX > minRightY) {
            high = partitionX - 1;
        } else {
            low = partitionX + 1;
        }
    }
    return 0;
}
```


```cpp
#include <string>

std::string longestPalindromicSubstring(std::string s) {
  int n = s.size();
  if (n == 0) return "";
  int start = 0, maxLength = 1;

  for (int i = 0; i < n; ++i) {
    int l = i, r = i;

    while (r < n - 1 && s[r] == s[r + 1])
      r++;
    i = r;

    while (l > 0 && r < n - 1 && s[l - 1] == s[r + 1]) {
      l--;
      r++;
    }

    int length = r - l + 1;
    if (length > maxLength) {
      start = l;
      maxLength = length;
    }
  }

  return s.substr(start, maxLength);
}
```

abcddccbba babad abcddccdbbcccbddcba abcddccdbbcccccdbba abcddccdbbcccccdbbaz abcddccdbbccccdbba ab abcddccbbccccba cbbd abcddccbbba a abcdabc abcddccdbbbcccbba bab abcddcbba abcddccdbbcccbdbba a abcddcba abcddccbbbba aba abcddcbaz abcddccdbbccccddcba abcddccbbcccccba abccccba abcddccbbcccbbba abcddccbbccccddcba babad abcddccdbbccccbbba abcddccbbcccbba abcddccdbbccccddcbaz abcddccdbbcccccddcba abccba abcddccbbbbbba abcddccdbbccccddcbaz ab abcddccba abccbb abcddccdbbcccbcbba abcddccdbbcccbddcbaz abcba cbbd abcddccdbbcccccddcbaz abcddccdbbcccccbbba abcddccdbbccccdbbaz babad abba abcddccbbccccddcbaz abcddccbbccccdbba

abcddcba a bcccb ccbbcc ccbbcc ccccc cddc bbcccbb cddc cddc abcba bcccb abba ccbbcc bab bbcccbb cddc bbccccbb cddc bab bcddcb cddc a abccba bab bccb bbbbbb bcccb a bcccb ccbbcc bb cddc ccccc aba cddc a abcddcba a bab ccccc abccccba bbcccccbb cddc bcccccb ccccc bbcccbb bb

```java
public String longestPalindromicSubstring(String s) {
    int n = s.length();
    if (n == 0) return "";

    int start = 0, maxLength = 1;

    for (int i = 0; i < n; ++i) {
        int l = i, r = i;

        while (r < n - 1 && s.charAt(r) == s.charAt(r + 1))
            r++;
        i = r;

        while (l > 0 && r < n - 1 && s.charAt(l - 1) == s.charAt(r + 1)) {
            l--;
            r++;
        }

        int length = r - l + 1;
        if (length > maxLength) {
            start = l;
            maxLength = length;
        }
    }

    return s.substring(start, start + maxLength);
}
```


```python
def reverse(x: int) -> int:
    sign = -1 if x < 0 else 1
    x = abs(x)
    res = 0
    while x:
        res = res * 10 + x % 10
        x //= 10
    res *= sign
    return res if -2**31 <= res <= 2**31 - 1 else 0
```

```java
public int reverse(int x) {
    long res = 0;
    while (x != 0) {
        res = res * 10 + x % 10;
        x /= 10;
    }
    return (res < Integer.MIN_VALUE || res > Integer.MAX_VALUE) ? 0 : (int)res;
}
```


```cpp
int myAtoi(string s) {
    long result = 0;
    int i = 0, sign = 1;
    while (s[i] == ' ') i++;
    if (s[i] == '-' || s[i] == '+') sign = (s[i++] == '+') ? 1 : -1;
    while (isdigit(s[i])) {
        result = result * 10 + (s[i++] - '0');
        if (result * sign > INT_MAX) return INT_MAX;
        if (result * sign < INT_MIN) return INT_MIN;
    }
    return result * sign;
}
```

```java
public int myAtoi(String s) {
    long result = 0;
    int i = 0, sign = 1;
    while (i < s.length() && s.charAt(i) == ' ') i++;
    if (i < s.length() && (s.charAt(i) == '-' || s.charAt(i) == '+')) {
        sign = (s.charAt(i++) == '+') ? 1 : -1;
    }
    while (i < s.length() && Character.isDigit(s.charAt(i))) {
        result = result * 10 + (s.charAt(i++) - '0');
        if (result * sign > Integer.MAX_VALUE) return Integer.MAX_VALUE;
        if (result * sign < Integer.MIN_VALUE) return Integer.MIN_VALUE;
    }
    return (int) (result * sign);
}
```


```python
def is_palindrome(x: int) -> bool:
    if x < 0:
        return False
    original, reversed = x, 0
    while x > 0:
        reversed = reversed * 10 + x % 10
        x //= 10
    return original == reversed
```

false true true false true true false false true false true false true false true true true false true true true true false true false true

```java
public boolean isPalindrome(int x) {
    if (x < 0) return false;
    int original = x, reversed = 0;
    while (x > 0) {
        reversed = reversed * 10 + x % 10;
        x /= 10;
    }
    return original == reversed;
}
```


true false false true false false true true true true true false false false true false true false false false true false false false true false false false false true false true false true true false false false true true false true false true false true true true false true false true false false true false false false true false false false false false false true false false false false true false false true false false false false false false true true true true true false false false false false false false false true true false false true false false false true false false false false false false true

```java
public boolean isMatch(String s, String p) {
    int m = s.length(), n = p.length();
    boolean[][] dp = new boolean[m + 1][n + 1];
    dp[0][0] = true;

    for (int j = 1; j <= n; j++) {
        if (p.charAt(j - 1) == '*' && dp[0][j - 2]) {
            dp[0][j] = true;
        }
    }

    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            if (p.charAt(j - 1) == s.charAt(i - 1) || p.charAt(j - 1) == '.') {
                dp[i][j] = dp[i - 1][j - 1];
            } else if (p.charAt(j - 1) == '*') {
                dp[i][j] = dp[i][j - 2] || (dp[i - 1][j] && (s.charAt(i - 1) == p.charAt(j - 2) || p.charAt(j - 2) == '.'));
            }
        }
    }

    return dp[m][n];
}
```


```cpp
int maxArea(vector<int>& height) {
    int max_area = 0, left = 0, right = height.size() - 1;
    while (left < right) {
        max_area = max(max_area, min(height[left], height[right]) * (right - left));
        if (height[left] < height[right]) {
            left++;
        } else {
            right--;
        }
    }
    return max_area;
}
```

```python
def max_area(height):
    max_area, left, right = 0, 0, len(height) - 1
    while left < right:
        max_area = max(max_area, min(height[left], height[right]) * (right - left))
        if height[left] < height[right]:
            left += 1
        else:
            right -= 1
    return max_area
```


```java
public String longestCommonPrefix(String[] strs) {
    if (strs.length == 0) return "";

    for (int i = 0; i < strs[0].length(); ++i) {
        char c = strs[0].charAt(i);
        for (int j = 1; j < strs.length; ++j) {
            if (i == strs[j].length() || strs[j].charAt(i) != c) {
                return strs[0].substring(0, i);
            }
        }
    }
    return strs[0];
}
```

strs = \[ "flower ", "flow ", "flight "\]

["flow ", "flower ", "car "]
[" ", "abc ", "abcd "]
["dog ", "flight ", "car "]
["car ", "flow ", "racecar "]
["racecar ", "flow ", "car "]
["dog ", "racecar ", "car "]
["flow ", "flight ", "car "]
["dog ", "racecar ", "car "]
["flow ", "flow ", "racecar "]
["flower ", "flower ", "flower "]
["car ", "car ", "car "]
["dog ", "flower ", "racecar "]
["ab ", "abc ", "abcd "]
["flow ", "flower ", "racecar "]
["dog ", "flight ", "racecar "]
["flower ", "flow ", "flight "]
["flower ", "flower ", "racecar "]
["flow ", "flow ", "car "]
["flight ", "flight ", "flight "]
["flower ", "flow ", "racecar "]
["racecar ", "flight ", "car "]
["a ", "a ", "a "]
["flight ", "flow ", "racecar "]
["flower ", "flight ", "car "]
["flight ", "flower ", "car "]
["flight ", "flight ", "racecar "]
["flower ", "flow ", "flight "]
["dog ", "flower ", "car "]
["flight ", "flower ", "racecar "]
["car ", "flight ", "racecar "]
["flower ", "flow ", "car "]
["hello ", "world ", "hi "]
["flow ", "flight ", "racecar "]
["car ", "flower ", "racecar "]
["flow ", "flow ", "flow "]
["flower ", "flower ", "car "]
["apple ", "apricot ", "aperture "]
["flower ", "flight ", "racecar "]
["dog ", "flow ", "car "]
["racecar ", "flower ", "car "]
["racecar ", "racecar ", "racecar "]
["123 ", "12345 ", "123 "]
["dog ", "flow ", "racecar "]
[ "flower ", "flow ", "flight "]
["dog ", "dog ", "dog "]
["flight ", "flight ", "car "]
["flight ", "flow ", "car "]

a racecar dog ab fl fl flow ap flight car flower fl 123

```python
def longest_common_prefix(strs):
    if not strs:
        return ""

    for i, c in enumerate(strs[0]):
        for j in range(1, len(strs)):
            if i == len(strs[j]) or strs[j][i] != c:
                return strs[0][:i]
    return strs[0]
```


```java
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;

public List<List<Integer>> threeSum(int[] nums) {
    Arrays.sort(nums);
    List<List<Integer>> result = new ArrayList<>();

    for (int i = 0; i < nums.length - 2; i++) {
        if (i == 0 || nums[i] != nums[i - 1]) {
            int j = i + 1, k = nums.length - 1;
            while (j < k) {
                int sum = nums[i] + nums[j] + nums[k];
                if (sum == 0) {
                    result.add(Arrays.asList(nums[i], nums[j], nums[k]));
                    while (j < k && nums[j] == nums[j + 1]) j++;
                    while (j < k && nums[k] == nums[k - 1]) k--;
                    j++;
                    k--;
                } else if (sum < 0) {
                    j++;
                } else {
                    k--;
                }
            }
        }
    }

    return result;
}
```

```cpp
#include <vector>
#include <algorithm>

std::vector<std::vector<int>> threeSum(std::vector<int>& nums) {
    std::vector<std::vector<int>> result;
    std::sort(nums.begin(), nums.end());

    for (int i = 0; i < (int)nums.size() - 2; ++i) {
        if (i == 0 || nums[i] != nums[i - 1]) {
            int j = i + 1, k = nums.size() - 1;
            while (j < k) {
                int sum = nums[i] + nums[j] + nums[k];
                if (sum == 0) {
                    result.push_back({nums[i], nums[j], nums[k]});
                    while (j < k && nums[j] == nums[j + 1]) ++j;
                    while (j < k && nums[k] == nums[k - 1]) --k;
                    ++j;
                    --k;
                } else if (sum < 0) {
                    ++j;
                } else {
                    --k;
                }
            }
        }
    }

    return result;
}
```


```cpp
#include <algorithm>
#include <vector>

int threeSumClosest(std::vector<int>& nums, int target) {
    std::sort(nums.begin(), nums.end());
    int closest = nums[0] + nums[1] + nums[2];
    for (int i = 0; i < nums.size() - 2; ++i) {
        int left = i + 1;
        int right = nums.size() - 1;
        while (left < right) {
            int sum = nums[i] + nums[left] + nums[right];
            if (sum == target) {
                return sum;
            }
            if (abs(target - sum) < abs(target - closest)) {
                closest = sum;
            }
            if (sum < target) {
                left++;
            } else {
                right--;
            }
        }
    }
    return closest;
}
```

```java
import java.util.Arrays;

public int threeSumClosest(int[] nums, int target) {
    Arrays.sort(nums);
    int closest = nums[0] + nums[1] + nums[2];
    for (int i = 0; i < nums.length - 2; ++i) {
        int left = i + 1;
        int right = nums.length - 1;
        while (left < right) {
            int sum = nums[i] + nums[left] + nums[right];
            if (sum == target) {
                return sum;
            }
            if (Math.abs(target - sum) < Math.abs(target - closest)) {
                closest = sum;
            }
            if (sum < target) {
                left++;
            } else {
                right--;
            }
        }
    }
    return closest;
}
```