π About the author: Hello, I'm brother cheshen, cheshen at No. 18 Fuxue road π₯
π Personal homepage: A blog that should have no place to live_ Cheshen, 18 Fuxue Road_ CSDN blog
π give the thumbs-up β comment β Collection = = form a habit (one button three times) π
π This series mainly aims to brush various titles of LeetCode website and achieve the improvement of self-ability β‘
β‘ I hope you can give me more support π€~ Let's come on together π
- Column< LeetCode ladder>
On Thursday, xdm, Beijing is hot again today. The weather changes too fast. Grab food every day and eat enough in the canteen! The epidemic is still repeated. Please wear masks ~ continue. Come on, join brother Che Shen today to improve your Python Programming and interview skills and brush the ladder~
Here are my ladder integration rules:
At least one question per day: one question points + 10 points
If you do one more question (or solve it in more than one way), the score on that day will be + 20 points (+ 10 + 10)
If you have done more than three questions, you will get + 20 points from the third question (for example, if you have done three questions, the score is - 10 + 10 + 20 = 40; if you have done four questions, the score is - 10 + 10 + 20 + 20 = 60)
The initial score is 100 points
If there is no problem for one day, deduct - 10 points (except Saturday and Sunday, note: rest)
insist!!!
Primary algorithm
List of brush questions
character string
stem
Given a string, find its first non repeating character and return its index. If it does not exist, return - 1.
Example:
s = "leetcode"
Return 0
s = "loveleetcode"
Return 2
Tip: you can assume that the string contains only lowercase letters.
Hashtable
analysis:
Find the characters that do not repeat. It is obvious that let's use the dictionary and hash table to solve the problem. Determine the number of each character through the dictionary, and then select the characters that appear only once, and select the first one, and return its corresponding index number.
class Solution: def firstUniqChar(self, s: str) -> int: # Hashtable hashmap = {} for i in s: if i not in hashmap: hashmap.update({i:s.count(i)}) # Store the number of numbers in the hash table for key,value in hashmap.items(): # Traverse the key values of the hash table if hashmap[key] == 1: return s.index(key) return -1
Hash table speed performance is good~
Index count
analysis:
We can also take the string as a single character into a list, count through count to see the number in it, traverse all items and delete redundant items. If it is greater than 1, it will not be returned; if there is only one character, it will return its corresponding index number. If it does not exist, it will return - 1
class Solution: def firstUniqChar(self, s: str) -> int: # Take out the single letter in s as a single string list1 = list(map(str, s)) n = len(list1) temp = 0 list2 = list(map(str, s)) for i in list1: if list1.count(i) > 1: list2.remove(i) # If yes, delete the corresponding value in list2 # Determine whether the above situation exists for j in list1: if list1.count(j) == 1: temp += 1 if temp > 0: return list1.index(list2[0]) # Finished deleting in query list2 return -1
Well, the courage is commendable. It's beyond the time limit. I don't think it should be
Let's optimize it again
Refine the judgment of temp
Do not traverse all parameters that directly read list2 to see if there are still values after deleting the same values. If there are, return the first one. If not, return - 1.
class Solution: def firstUniqChar(self, s: str) -> int: # Take out the single letter in s as a single string list1 = list(map(str, s)) n = len(list1) list2 = list(map(str, s)) for i in list1: if list1.count(i) > 1: list2.remove(i) # If yes, delete the corresponding value in list2 # Determine whether the above situation exists if len(list2) > 0: return list1.index(list2[0]) # Finished deleting in query list2 return -1
It's still too delicious. It's beyond the time limit
But it must work.
Let's just try built-in functions
class Solution: def firstUniqChar(self, s: str) -> int: for i in range(len(s)): if s.count(s[i]) == 1: return i return -1
Real time proof, really slow, not considered.
Twice traversal
We can use two iterations to output. In the first iteration, the number of occurrences of each number is saved in a list. In the second iteration, if the value is 1, the index value of 1 is output. Otherwise, it returns - 1
class Solution: def firstUniqChar(self, s: str) -> int: c1 = [] n = len(s) for i in range(n): c1.append(s.count(s[i])) for j in range(n): if c1[j] == 1: return j return -1
Courage is commendable. Why is it beyond the time limit o(β₯οΉβ₯) o
find and rfind
The skeleton of this method is extremely strange. It's not too wonderful. One search from the front and one search from the back. If the subscripts are equal, it means that it only appears once
class Solution: def firstUniqChar(self, s: str) -> int: for x in s: if s.find(x) == s.rfind(x): return s.find(x) return -1
Much faster.
On the whole, hash table method is still the fastest method. If there are better methods, you can leave a message below~
References
Author: GODEYESCAO
Link: https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/xn5z8r/?discussion=71JehT
Source: LeetCode
Score today: + 10 + 10 + 20
Total score: 410
come on.
β€ Keep reading Paper, keep taking notes, keep learning, and keep brushing LeetCode β€!!!
Stick to the questions!!! Hit the ladder!!!
β‘To Be No.1
β‘β‘ Ha ha ha ha
β‘ Creation is not easy β‘οΌ Crossing energy β€ Follow, collect and like β€ Three is the best
α¦( Β΄ο½₯α΄ο½₯` )
β€
γ
Clearly pay is far higher than return, but still choose to do it. Just to help you in the future that has not been touched. Maybe this is silly white sweet.
γ