阅读本文大约需要2分钟
最近花了不少时间刷洛谷,有些题目使用python可以非常简洁地解决,下面给大家举几个例子
Implement strStr()
Implement strStr().
Return the index of the first occurrence of needle in haystack, or-1if needle is not part of haystack.
Example 1:
Input:haystack = "hello", needle = "ll"Output:2
Example 2:
Input:haystack = "aaaaa", needle = "bba"Output:-1
Permutations
Given a collection ofdistinctintegers, return all possible permutations.
Example:
Input:[1,2,3]Output:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]
Add Binary
Given two binary strings, return their sum (also a binary string).
The input strings are bothnon-emptyand contains only characters or .
Example 1:
Input:a = "11", b = "1"Output:"100"
Example 2:
Input:a = "1010", b = "1011"Output:"10101"
Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input:123Output:321
Example 2:
Input:-123Output:-321
Example 3:
Input:120Output:21
Palindrome Number
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Example 1:
Input:121Output:true
Example 2:
Input:-121Output:falseExplanation:From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:
Input:10Output:falseExplanation:Reads 01 from right to left. Therefore it is not a palindrome.
Add Binary
Given two binary strings, return their sum (also a binary string).
The input strings are bothnon-emptyand contains only characters or .
Example 1:
Input:a = "11", b = "1"Output:"100"
Example 2:
Input:a = "1010", b = "1011"Output:"10101"
Plus One
Given anon-emptyarray of digits representing a non-negative integer, plus one to the integer.
The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.
You may assume the integer does not contain any leading zero, except the number 0 itself.
Example 1:
Input:[1,2,3]Output:[1,2,4]Explanation:The array represents the integer 123.
Example 2:
Input:[4,3,2,1]Output:[4,3,2,2]Explanation:The array represents the integer 4321.
往期文章
联系我
领取专属 10元无门槛券
私享最新 技术干货