This is the 16th day of my participation in the First Challenge 2022
First, write first
I heard you’re still writing a two-layer for loop to solve the sum of two numbers?
LeetCode 2: Sum of two numbers transfer gate: the median of two sorted arrays, “most” technical solution!
LeetCode 第 3 题 : Longest loop transport gate: Horse-and-cart algorithm to solve longest loop! Manacher
LeetCode 4 题 conversion of string to integer (ATOI) : Solve ATOI with “foolish old man removing mountains” method, think clever!
Continue solving LeetCode, the longest public prefix
One of LeetCode’s “most classic” algorithm problems: the sum of three numbers
The sum of the nearest three numbers, wonderful
LeetCode: Valid parentheses
Delete duplicates in LeetCode: delete duplicates in LeetCode
The container that holds the most water is the container that holds the most water
LeetCode 11 String multiplication Transfer gate: LeetCode string multiplication, what do you do?
Question 12: Reverse string, for the interview, looking forward to your participation. “Use the utility in the API is recommended in the project. But if you Use it in an interview, You will definitely fail.”
Today’s topic
Write a function that reverses the input string.
Input:"hello"Output:"olleh"Input:"A man, a plan, a canal: Panama"Output:"amanaP :lanac a ,nalp a ,nam A"
Copy the code
Three, the analysis
This problem, at first glance, is so simple, it should be the easiest problem to brush so far, a complete traversal of the string, rearrangement. Analysis of correct ideas:
Fourth, the problem solving
- A shortcut:
One line of code
class Solution(object) :
def reverseString(self, s) :
""" :type s: str :rtype: str """
return s[::-1]
Copy the code
Slicing operation, the previous Python | these skills that you won’t be small knowledge? (A) The slicing operation is introduced in detail.
Two lines of code
(1) reduce function
class Solution(object) :
def reverseString(self, s) :
""" :type s: str :rtype: str """
from functools import reduce
return reduce(lambda x,y:y+x,s)
Copy the code
The reduce() function accumulates elements in a sequence of arguments.
Function performs the following operations on all the data in a data set (linked list, tuple, etc.) : operates on the first and second elements of the set with function (which has two parameters) passed to Reduce, and then calculates the result with the third data using function function, finally obtaining a result.
Lambda expressions are usually used when a function is needed but you don’t want to bother naming a function, i.e. anonymous functions.
(2) reversed function
class Solution(object) :
def reverseString(self, s) :
""" :type s: str :rtype: str """
s = list(s)
return "".join(reversed(s))
Copy the code
The reversed function returns a reversed iterator. Invert an iterable, such as a list or collection, and return an iterated object. The string is converted into a list, the reversed function is called, and the join function is used to join the list iteratively.
- Normal thinking
Traversal, substitution values, the idea is given in the analysis
Time complexity: O(n^2)
class Solution(object) :
def reverseString(self, s) :
""" :type s: str :rtype: str """
result = list(s)
for i in range(len(result)//2):
temp = result[len(result)-i-1]
result[len(result)-i-1] = result[i]
result[i] = temp
return ' '.join(result)
Copy the code
- Submit the results
Test data: 476 groups Running time: 48ms beat percentage :18.15%
Although beat people are not many, but I have ideas, I am proud ~
No, in fact, the above line, two lines of code to solve the problem super convenient, also considered a small skill.
Five, doubt
Written (18 years is still in reading, ‘) today, want to talk about learning, don’t know if you have tried a personal learning, I tried, from freshman to sophomore, almost a person, I like people to solve the problem, of course, such as a freshman sophomore year practice, and almost half of the people in the class I had more or less communication, so learning, the return calculate can, But to learn Python, I am the first or second person in our class and even in our school. Few people learn it. We are all chewing on JavaWeb, front-end, small programs and so on.
A person was really hard, encounter problems and looking for someone to discuss all have no, ok I met miss li, particularly big help to me, to join the lab have a place to study, to guide my study direction, supervision and I learn, brush leetcode also very lucky, I met a senior light city, in the thought, education to be higher than me a lot of senior, give me a lot of advice, At the same time, we have been insisting on exchanging ideas, which is the reason why I haven’t given up until now. What I think is: One day, I brush leetcode 200, 300, 400, problem, thought, the ability has the very big promotion, I want to write a book, brush, self-help, theory of knowledge, such as public, I have hundreds of original article, I read a few books, such as when I take an examination of grind, I think I can, come on everybody, at any time, don’t give up, Unless you find a better way, you have to make sure it’s a great way.
Six, the concluding
Persistence and hard work: results.
Like to see the message forwarding, four support, the original is not easy. Ok, see you next time, I love the cat love technology, more love si si’s old cousin Da Mian ଘ(˙꒳˙)ଓ Di Di