Leetcode 0012. Integer to Roman (python)
12. Integer to Roman题目Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. 12345678Symbol ValueI 1V 5X 10L 50C 100D 500M 1000 For example, 2 is written as II in Roman numeral, just two one's 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 le...
Leetcode 0011. Container With Most Water (python)
11. Container With Most Water题目You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the i-th line are (i, 0) and (i, height[i]). Find two lines that together with the x-axis form a container, such that the container contains the most water. Return the maximum amount of water a container can store. Notice that you may not slant the container. Example 1: 1234Input: height = [1,8,6,2,5,4,8,3,7]Output: 49Explanation: The above vertical...
Leetcode 0010. Regular Expression Matching (python)
10. Regular Expression Matching题目Given an input string s and a pattern p, implement regular expression matching with support for '.' and '*' where: '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). Example 1: 123Input: s = "aa", p = "a"Output: falseExplanation: "a" does not match the entire string "aa". Example 2: 123Inp...
Leetcode 0009. Palindrome Number (python)
9. Palindrome Number题目Given an integer x, return true if x is a palindrome, and false otherwise. Example 1: 123Input: x = 121Output: trueExplanation: 121 reads as 121 from left to right and from right to left. Example 2: 123Input: x = -121Output: falseExplanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: 123Input: x = 10Output: falseExplanation: Reads 01 from right to left. Therefore it is not a palindrome. Constra...
Leetcode 0008. String to Integer (atoi) (python)
8. String to Integer (atoi)题目Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer. The algorithm for myAtoi(string s) is as follows: Whitespace: Ignore any leading whitespace (" "). Signedness: Determine the sign by checking if the next character is '-' or '+', assuming positivity if neither present. Conversion: Read the integer by skipping leading zeros until a non-digit character is encountered or the end of the string is rea...
Leetcode 0007. Reverse Integer (python)
7. Reverse Integer题目Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-2³¹, 2³¹ - 1], then return 0. Assume the environment does not allow you to store 64-bit integers (signed or unsigned). Example 1: 12Input: x = 123Output: 321 Example 2: 12Input: x = -123Output: -321 Example 3: 12Input: x = 120Output: 21 Constraints: -2³¹ <= x <= 2³¹ - 1 题目大意将一个 32 位有符号整数的数字部分反转。如果反转后的整数溢出 32 位有符号整数范围...
Leetcode 0006. Zigzag Conversion (python)
6. Zigzag Conversion题目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) 123P A H NA P L S I I GY I R And then read line by line: "PAHNAPLSIIGYIR" Write the code that will take a string and make this conversion given a number of rows: 1string convert(string s, int numRows); Example 1: 12Input: s = "PAYPALISHIRING", numRows = 3Outpu...
Leetcode 0005. Longest Palindromic Substring (python)
5. Longest Palindromic Substring题目Given a string s, return the longest palindromic substring in s. Example 1: 123Input: s = "babad"Output: "bab"Explanation: "aba" is also a valid answer. Example 2: 12Input: s = "cbbd"Output: "bb" Constraints: 1 <= s.length <= 1000 s consist of only digits and English letters. 题目大意在给定字符串中找出最长的回文子串(palindromic substring)。回文串是指正读反读都一样的字符串。若存在多个等长的最长回文子串,返回任意一个即可。字符串长度上限为 1000,仅包含数字和英文字母。 你选用何种方法解题?本题的核...
Leetcode 0004. Median of Two Sorted Arrays (python)
4. Median of Two Sorted Arrays题目Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Example 1: 123Input: nums1 = [1,3], nums2 = [2]Output: 2.00000Explanation: merged array = [1,2,3] and median is 2. Example 2: 123Input: nums1 = [1,2], nums2 = [3,4]Output: 2.50000Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. 题目大意给定两个已排序的数组 nums1 和 nums2,找出这两个有序数组的中位数。...
Leetcode 0003. Longest Substring Without Repeating Characters (python)
3. Longest Substring Without Repeating Characters题目Given a string s, find the length of the longest substring without repeating characters. Example 1: 123Input: s = "abcabcbb"Output: 3Explanation: The answer is "abc", with the length of 3. Example 2: 123Input: s = "bbbbb"Output: 1Explanation: The answer is "b", with the length of 1. Example 3: 1234Input: s = "pwwkew"Output: 3Explanation: The answer is "wke", with the length of 3.No...
Leetcode 0002. Add Two Numbers
2. Add Two Numbers题目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. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Example 1: 123Input: l1 = [2,4,3], l2 = [5,6,4]Output: [7,0,8]Explanation: 342 + 465 = 807. Example 2: 12Input: l1 = [0], l2 = [0]Output: [0] Example 3: 12I...
Leetcode 0001.two sum(python)
1. Two Sum题目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. You can return the answer in any order. Example 1: 123Input: nums = [2,7,11,15], target = 9Output: [0,1]Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. Example 2: 12Input: nums = [3,2,4], target = 6Output: [1,2] Example 3: 12Input: nums =...

