python 删除重复字符_Google面试问题指南:使用Python删除重复出现的字符
python 刪除重復(fù)字符
by Anthony Sistilli
安東尼·西斯蒂里(Anthony Sistilli)
Google面試問(wèn)題指南:使用Python刪除重復(fù)出現(xiàn)的字符 (Google Interview Question Guide: Delete Reoccurring Characters with Python)
Nowadays, Google interviews are all the rage. But sometimes, interviews can get the best of us. Especially if it’s for a position we really want.
如今,Google面試風(fēng)靡一時(shí)。 但是有時(shí)候,采訪可以使我們受益匪淺。 尤其是在我們真正想要的職位上。
I’ve had the pleasure of interviewing at multiple top companies as a student, and landing a job in Silicon Valley as a Software Engineer.
我很高興能在學(xué)生中接受多家頂級(jí)公司的采訪,并以軟件工程師的身份在硅谷找到一份工作。
My goal is to help you get that dream job you’ve always wanted!
我的目標(biāo)是幫助您獲得夢(mèng)always以求的工作!
We’re going to go over a classic question that could show up on your next Google Interview.
我們將討論一個(gè)經(jīng)典問(wèn)題,該問(wèn)題可能會(huì)在您的下一次Google采訪中出現(xiàn)。
Warning: if you’re a coding veteran, you probably already know how to solve this question!
警告:如果您是編碼方面的資深人士,您可能已經(jīng)知道如何解決此問(wèn)題!
If you’re trying to get an Internship or a Full-Time job next year, then you’ll definitely benefit from this article. ???
如果您想在明年獲得實(shí)習(xí)或全職工作,那么您一定會(huì)從本文中受益。 ???
問(wèn)題:給定字符串作為輸入,刪除所有重復(fù)出現(xiàn)的字符,然后返回新字符串。 (QUESTION: Given a string as your input, delete any reoccurring character, and return the new string.)
If you would prefer a video to explain the question, I made one here.
如果您希望使用視頻來(lái)解釋這個(gè)問(wèn)題, 我在這里做了一個(gè) 。
As we can see from the example above, the output is “abc” because we delete the second ‘a(chǎn)’, ‘b’, and ‘c’.
從上面的示例可以看到,輸出為“ abc”,因?yàn)槲覀儎h除了第二個(gè)“ a”,“ b”和“ c”。
First things first, let’s set up our function in Python 2.7.
首先,讓我們?cè)赑ython 2.7中設(shè)置函數(shù)。
def deleteReoccurringCharacters(string):To tackle this question, we’re going to use a specific data structure called a HashSet.
為了解決這個(gè)問(wèn)題,我們將使用一種稱為HashSet的特定數(shù)據(jù)結(jié)構(gòu)。
You can think of a set as being similar to an array, with two main exceptions.
您可以認(rèn)為集合類似于數(shù)組,但有兩個(gè)主要例外。
It’s completely unordered
完全沒(méi)有秩序
It can’t contain duplicates
它不能包含重復(fù)項(xiàng)
Because it’s unordered, we’ll also need an empty string to store the characters we’ve added to the set in order. This will be the string we return.
因?yàn)樗菬o(wú)序的,所以我們還需要一個(gè)空字符串來(lái)存儲(chǔ)已按順序添加到集合中的字符。 這將是我們返回的字符串。
Let’s set both of those things up.
讓我們同時(shí)設(shè)置這兩件事。
def deleteReoccurringCharacters(string): seenCharacters = set() outputString = ''Now that we’ve set up the data structures we need, let’s talk about our algorithm.
現(xiàn)在我們已經(jīng)建立了所需的數(shù)據(jù)結(jié)構(gòu),下面我們來(lái)談?wù)勎覀兊乃惴ā?
Because of the way a set works in memory, it has a lookup time complexity of 0(1).
由于集合在內(nèi)存中的工作方式,其查找時(shí)間復(fù)雜度為0(1)。
This means we can use it to check whether or not we’ve already visited a character!
這意味著我們可以使用它來(lái)檢查我們是否已經(jīng)訪問(wèn)過(guò)角色!
我們的算法 (Our algorithm)
Loop through all the characters in the initial string and do the following:
循環(huán)遍歷初始字符串中的所有字符,然后執(zhí)行以下操作:
Step 1: Check if the character is in our set already步驟1:檢查角色是否已經(jīng)在我們的集合中 Step 2: If it’s not in the set, add it to the set and append it to the string步驟2:如果不在集合中,請(qǐng)將其添加到集合中并將其附加到字符串中Let’s see what that would look like in code ???
讓我們看一下代碼中的樣子嗎?
for char in string: if char not in seenCharacters: seenCharacters.add(char) outputString += charWe don’t have to worry about an “else” case, because we don’t do anything with the reoccurring character itself.
我們不必?fù)?dān)心“其他”情況,因?yàn)槲覀儫o(wú)需對(duì)重復(fù)出現(xiàn)的角色本身做任何事情。
Now all that’s left to do is return the outputString.
現(xiàn)在剩下要做的就是返回outputString了。
Here’s what the finished code looks like:
這是完成的代碼,如下所示:
def deleteReoccurringCharacters(string): seenCharacters = set() outputString = '' for char in string: if char not in seenCharacters: seenCharacters.add(char) outputString += char return outputStringAnd there you have it!
在那里,您擁有了!
Now if this was an interview, your recruiter would ask you about the time and space complexity.
現(xiàn)在,如果這是一次面試,您的招聘人員會(huì)問(wèn)您時(shí)間和空間的復(fù)雜性。
Let’s do a little analysis.
讓我們做一點(diǎn)分析。
時(shí)間復(fù)雜度 (Time Complexity)
Iterating through the entire input string has a time complexity of O(n), since there are n characters in the string itself.
遍歷整個(gè)輸入字符串的時(shí)間復(fù)雜度為O(n),因?yàn)樽址旧戆琻個(gè)字符。
For each of those characters, we have to check whether or not we’ve seen the… However, since a HashSet has a lookup time of O(1), our time complexity isn’t impacted.
對(duì)于每個(gè)這些字符,我們都必須檢查是否已看到……。但是,由于HashSet的查找時(shí)間為O(1),因此,時(shí)間復(fù)雜度不會(huì)受到影響。
Leaving us with a final time complexity of O(n).
使我們的最終時(shí)間復(fù)雜度為O(n)。
空間復(fù)雜度 (Space Complexity)
Worst case scenario, we get a string with all unique characters. For example, “abcdef”.
最壞的情況是,我們得到一個(gè)包含所有唯一字符的字符串。 例如,“ abcdef”。
In that case, we would store all n elements in our string and our set.
在這種情況下,我們將所有n個(gè)元素存儲(chǔ)在字符串和集合中。
However, we’re also limited to size of the english alphabet.
但是,我們還限于英文字母的大小。
This is a good chance to ask our interviewer what type of characters count as unique in the string (uppercase / lowercase / numbers / symbols).
這是一個(gè)很好的機(jī)會(huì),可以詢問(wèn)我們的面試官字符串中哪種字符算作唯一字符(大寫(xiě)/小寫(xiě)/數(shù)字/符號(hào))。
Assuming that the initial string will contain lowercase letters from the alphabet, since the alphabet is finite, our set and output string can never be bigger than 26 characters.
假設(shè)初始字符串將包含字母表中的小寫(xiě)字母,由于字母表是有限的,因此我們的set和輸出字符串不得大于26個(gè)字符。
Leaving us with a worst case scenario space complexity of O(1).
最糟糕的情況是,我們的空間復(fù)雜度為O(1)。
您現(xiàn)在知道了如何解決Google面試問(wèn)題! (You now know how to solve a Google interview question!)
This question is likely to come up in the early stages of the interview due to it’s straightforwardness… Like the online test, or the first phone call.
這個(gè)問(wèn)題很容易在面試的初期出現(xiàn),因?yàn)樗芎?jiǎn)單……就像在線考試或第一次打來(lái)的電話一樣。
If you’re a visual learner like I am, check out this video I made explaining the solution further. I create a new tutorial video everyday revolving around starting your career in software.
如果您像我一樣是視覺(jué)學(xué)習(xí)者, 請(qǐng)觀看我進(jìn)一步解釋該解決方案的視頻。 我每天都會(huì)制作一個(gè)新的教學(xué)視頻,圍繞著您在軟件領(lǐng)域的職業(yè)發(fā)展展開(kāi)。
I’ve also posted the finished code on Github here.
我也貼在完成的代碼在Github 這里 。
Thanks for watching, and good luck!
感謝收看,祝您好運(yùn)!
.a #33
.a#33
翻譯自: https://www.freecodecamp.org/news/solving-a-google-interview-question-python-2-code-included-eddefcaeffb2/
python 刪除重復(fù)字符
總結(jié)
以上是生活随笔為你收集整理的python 删除重复字符_Google面试问题指南:使用Python删除重复出现的字符的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 列出薪金高于在部门30_我如何在五个月内
- 下一篇: python 微信bot_使用Tweep