python 刪除重復字符_Google面試問題指南:使用Python刪除重復出現的字符

python 刪除重復字符

by Anthony Sistilli

安東尼·西斯蒂里(Anthony Sistilli)

Google面試問題指南:使用Python刪除重復出現的字符 (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面試風靡一時。 但是有時候,采訪可以使我們受益匪淺。 尤其是在我們真正想要的職位上。

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.

我很高興能在學生中接受多家頂級公司的采訪,并以軟件工程師的身份在硅谷找到一份工作。

My goal is to help you get that dream job you’ve always wanted!

我的目標是幫助獲得夢always以求的工作!

We’re going to go over a classic question that could show up on your next Google Interview.

我們將討論一個經典問題,該問題可能會在您的下一次Google采訪中出現。

Warning: if you’re a coding veteran, you probably already know how to solve this question!

警告:如果您是編碼方面的資深人士,您可能已經知道如何解決此問題!

If you’re trying to get an Internship or a Full-Time job next year, then you’ll definitely benefit from this article. ???

如果您想在明年獲得實習全職工作,那么您一定會從本文中受益。 ???

問題:給定字符串作為輸入,刪除所有重復出現的字符,然后返回新字符串。 (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.

如果您希望使用視頻來解釋這個問題, 我在這里做了一個 。

As we can see from the example above, the output is “abc” because we delete the second ‘a’, ‘b’, and ‘c’.

從上面的示例可以看到,輸出為“ abc”,因為我們刪除了第二個“ a”,“ b”和“ c”。

First things first, let’s set up our function in Python 2.7.

首先,讓我們在Python 2.7中設置函數。

def deleteReoccurringCharacters(string):

To tackle this question, we’re going to use a specific data structure called a HashSet.

為了解決這個問題,我們將使用一種稱為HashSet的特定數據結構。

You can think of a set as being similar to an array, with two main exceptions.

您可以認為集合類似于數組,但有兩個主要例外。

  1. It’s completely unordered

    完全沒有秩序

  2. It can’t contain duplicates

    它不能包含重復項

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.

因為它是無序的,所以我們還需要一個空字符串來存儲已按順序添加到集合中的字符。 這將是我們返回的字符串。

Let’s set both of those things up.

讓我們同時設置這兩件事。

def deleteReoccurringCharacters(string):    seenCharacters = set()    outputString = ''

Now that we’ve set up the data structures we need, let’s talk about our algorithm.

現在我們已經建立了所需的數據結構,下面我們來談談我們的算法。

Because of the way a set works in memory, it has a lookup time complexity of 0(1).

由于集合在內存中的工作方式,其查找時間復雜度為0(1)。

This means we can use it to check whether or not we’ve already visited a character!

這意味著我們可以使用它來檢查我們是否已經訪問過角色!

我們的算法 (Our algorithm)

Loop through all the characters in the initial string and do the following:

循環遍歷初始字符串中的所有字符,然后執行以下操作:

Step 1: Check if the character is in our set already
步驟1:檢查角色是否已經在我們的集合中
Step 2: If it’s not in the set, add it to the set and append it to the string
步驟2:如果不在集合中,請將其添加到集合中并將其附加到字符串中

Let’s see what that would look like in code ???

讓我們看一下代碼中的樣子嗎?

for char in string:    if char not in seenCharacters:        seenCharacters.add(char)        outputString += char

We don’t have to worry about an “else” case, because we don’t do anything with the reoccurring character itself.

我們不必擔心“其他”情況,因為我們無需對重復出現的角色本身做任何事情。

Now all that’s left to do is return the outputString.

現在剩下要做的就是返回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 outputString

And there you have it!

在那里,您擁有了!

Now if this was an interview, your recruiter would ask you about the time and space complexity.

現在,如果這是一次面試,您的招聘人員會問您時間和空間的復雜性。

Let’s do a little analysis.

讓我們做一點分析。

時間復雜度 (Time Complexity)

Iterating through the entire input string has a time complexity of O(n), since there are n characters in the string itself.

遍歷整個輸入字符串的時間復雜度為O(n),因為字符串本身包含n個字符。

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.

對于每個這些字符,我們都必須檢查是否已看到……。但是,由于HashSet的查找時間為O(1),因此,時間復雜度不會受到影響。

Leaving us with a final time complexity of O(n).

使我們的最終時間復雜度為O(n)。

空間復雜度 (Space Complexity)

Worst case scenario, we get a string with all unique characters. For example, “abcdef”.

最壞的情況是,我們得到一個包含所有唯一字符的字符串。 例如,“ abcdef”。

In that case, we would store all n elements in our string and our set.

在這種情況下,我們將所有n個元素存儲在字符串和集合中。

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).

這是一個很好的機會,可以詢問我們的面試官字符串中哪種字符算作唯一字符(大寫/小寫/數字/符號)。

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.

假設初始字符串將包含字母表中的小寫字母,由于字母表是有限的,因此我們的set和輸出字符串不得大于26個字符。

Leaving us with a worst case scenario space complexity of O(1).

最糟糕的情況是,我們的空間復雜度為O(1)。

您現在知道了如何解決Google面試問題! (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.

這個問題很容易在面試的初期出現,因為它很簡單……就像在線考試或第一次打來的電話一樣。

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.

如果您像我一樣是視覺學習者, 請觀看我進一步解釋該解決方案的視頻。 我每天都會制作一個新的教學視頻,圍繞著您在軟件領域的職業發展展開。

I’ve also posted the finished code on Github here.

我也貼在完成的代碼在Github 這里 。

Thanks for watching, and good luck!

感謝收看,祝您好運!

.a #33

.a#33

翻譯自: https://www.freecodecamp.org/news/solving-a-google-interview-question-python-2-code-included-eddefcaeffb2/

python 刪除重復字符

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/394605.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/394605.shtml
英文地址,請注明出處:http://en.pswp.cn/news/394605.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

cordova

命令行 npm install -g cordova cordova create MyApp cd MyApp cordova platform add android 當然也可以把android換成browser把自己的前端程序放在www文件夾內這里注意如果用android studio打包或運行的話,(即不用cordova),要把…

冒泡排序(Java版)

冒泡排序基本思想: 1.比較相鄰的元素,如果第一個比第二個大,就交換它們兩個。 2.對每一對相鄰元素做同樣的工作,從開始的第一對到結尾的最后一對。在這一點,最后的元素應該會是最大的數。 3.針對所有的元素重復以上的步…

計算機科學與技術專業的論文周報,畢業設計(實習)周報

本科畢業設計周報第1 周畢業生周記撰寫畢業論文開題報告(初稿),結合畢業設計所選的題目,查閱大量相關資料,主要針對該設計所涉及的背景,研究目的及意義,以及國內外的相關成熟技術進行篩選,提取部分核心內容…

excel導出_SpringBoot實現快速導出Excel

閱讀本文約需要6分鐘 大家好,我是你們的導師,我每天都會在這里給大家分享一些干貨內容(當然了,周末也要允許老師休息一下哈)。上次老師跟大家分享了下MyBatis 幾種通用的寫法的相關知識,今天跟大家分享SpringBoot實現快速導出Exce…

SignalR Self Host+MVC等多端消息推送服務(4)

由于工作太忙,一直沒時間更新博客,之前有很多朋友一直問我什么時候將后續的代碼發上來,一直沒時間,今天就長話短說,不寫文章了,直接上demo,里面將正式項目中用到的一些敏感信息修改了&#xff0…

項目中需要總結的內容

1.鐵塔項目的硬件總結 2.傳感器項目的硬件總結 3.燈控項目的硬件總結 控制燈閃爍的電路,SIM卡板子復位電路,繼電器控制電路轉載于:https://www.cnblogs.com/yuesheng/p/6086647.html

計算機應用計算機電算化題庫,2014年浙江省會計電算化客觀題題庫

第一套試題一、單選題1.在會計軟件初始設置中,錄入期初余額時(C)A.只要求錄入一級科目的期初余額 B.只要求錄入中間級科目的期初余額C.每級科目均需錄入期初余額 D.只要求錄入最末級科目的期初余額2.在總賬系中,要求能夠進行上下級…

使用一些我喜歡的東西開始使用ES6

by Todd Palmer托德帕爾默(Todd Palmer) 使用一些我喜歡的東西開始使用ES6 (Getting started with ES6 using a few of my favorite things) This tutorial walks you through some easy steps to get started learning the newest version of JavaScript: ES6.本教程將引導您…

A 子類繼承父類,子類的構造函數會覆蓋父類的構造函數

//子類 沒有定義 構造 函數時&#xff0c;默認繼承父類的構造方法&#xff1a;輸出結果為 Class A... // 子類 定義了 構造 函數時&#xff0c;就不會繼承父類的構造方法&#xff1a;輸出結果是 Class B... <?php class A{ public function __construct(){ echo &qu…

fifo算法_緩存算法FIFO、LFU、LRU

閱讀文本大概需要3分鐘。0x01&#xff1a;FIFO算法FIFO(First in First out)&#xff0c;先進先出。其實在操作系統的設計理念中很多地方都利用到了先進先出的思想&#xff0c;比如作業調度(先來先服務)&#xff0c;為什么這個原則在很多地方都會用到呢&#xff1f;因為這個原則…

Pile 0009: Vim命令梳理

正常模式&#xff08;按Esc或Ctrl[進入&#xff09; 左下角顯示文件名或為空插入模式&#xff08;按i鍵進入&#xff09; 左下角顯示--INSERT--可視模式&#xff08;按v鍵進入&#xff09; 左下角顯示--VISUAL-- i 在當前位置生前插入 I 在當前行首插入 a 在當前位置后插入 A 在…

Introduction of Version Control/Git, SVN

Introduction of Version Control/Git, SVN 什么是版本控制&#xff1f; 你可以把一個版本控制系統&#xff08;縮寫VCS&#xff09;理解為一個“數據庫”&#xff0c;在需要的時候&#xff0c;它可以幫你完整地保存一個項目的快照。當你需要查看一個之前的快照&#xff08;稱之…

怎樣設置計算機遠程桌面,電腦如何設置遠程連接,手把手教你如何遠程

說起遠程桌面很多用戶都認為是從WIN2000 SERVER才開始引入的&#xff0c;實際上我們可以在WIN98甚至是DOS中看到他的身影。遠程桌面采用的是一種類似TELNET的技術&#xff0c;他是從TELNET協議發展而來的。那么如何設置自動開機&#xff0c;下面&#xff0c;我們就來看看如何設…

查看這些有用的ECMAScript 2015(ES6)提示和技巧

by rajaraodv通過rajaraodv 查看這些有用的ECMAScript 2015(ES6)提示和技巧 (Check out these useful ECMAScript 2015 (ES6) tips and tricks) EcmaScript 2015 (aka ES6) has been around for couple of years now, and various new features can be used in clever ways. I…

inputstream轉fileinputstream對象_FileInputStream類:文件字節輸入流

API ----IO ----字節輸入輸出流練習 java.lang.Object 繼承者 java.io.InputStream 繼承者 java.io.FileInputStreampublic FileInputStream類速查速記&#xff1a;直接包裝File用于從記事本中讀數據 in是針對java來說的&#xff0c;從記事本讀入到java* 構造方法&#xff1a;…

IBM將推NVMe存儲解決方案

先前&#xff0c;IBM曾對外宣稱將開發新的NVMe解決方案&#xff0c;并推動行業參與者進一步探索新協議&#xff0c;以支持更快的數據傳輸。周日&#xff0c;IBM表示新的語言協議——NVMe&#xff08;非易失性存儲器&#xff09;正在逐步取代SAS和SATA等舊有的固態硬盤存儲標準。…

html5中3個盒子怎樣設置,Web前端開發任務驅動式教程(HTML5+CSS3+JavaScript)任務10 盒子模型及應用.pptx...

第五單元 盒子模型任務10 盒子模型及應用學習目標盒子模型的概念掌握邊框的設置內邊距的設置外邊距的設置學習目標了解:利用盒子模型布局網頁的優勢任務目標實戰演練——制作古詩文欣賞網頁強化訓練——制作散文賞析網頁知識準備1. 盒子模型的概念知識準備1. 盒子模型的概念CSS…

SQL手工注入入門級筆記(更新中)

一、字符型注入 針對如下php代碼進行注入&#xff1a; $sql"select user_name from users where name$_GET[name]"; 正常訪問URL:http://url/xxx.php?nameadmin 此時實際數據庫語句: select user_name from users where nameadmin 利用以上結果可想到SQL注入構造語句…

materialize_使用Materialize快速介紹材料設計

materialize什么是材料設計&#xff1f; (What is Material Design?) Material Design is a design language created by Google. According to material.io, Material Design aims to combine:Material Design是Google創建的一種設計語言。 根據material.io &#xff0c;Mate…

python處理完數據導入數據庫_python 將execl測試數據導入數據庫操作

import xlrd import pymysql # 打開execl表 book xlrd.open_workbook(XXXX測試用例.xlsx) sheet book.sheet_by_name(Sheet1) # print(sheet.nrows) # 創建mysql連接 conn pymysql.connect( host127.0.0.1, userroot, password123456, dbdemo1, port3306, charsetutf8 ) # 獲…