博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 删除重复字符_Google面试问题指南:使用Python删除重复出现的字符
阅读量:2519 次
发布时间:2019-05-11

本文共 5632 字,大约阅读时间需要 18 分钟。

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

如果您希望使用视频来解释这个问题, 。

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, I create a new tutorial video everyday revolving around starting your career in software.

如果您像我一样是视觉学习者, 我每天都会制作一个新的教学视频,围绕着您在软件领域的职业发展展开。

I’ve also posted the finished code on Github .

我也贴在完成的代码在Github 。

Thanks for watching, and good luck!

感谢收看,祝您好运!

.a #33

.a#33

翻译自:

python 删除重复字符

转载地址:http://dcgwd.baihongyu.com/

你可能感兴趣的文章
Settings app简单学习记录
查看>>
SQLAlchemy
查看>>
多线程
查看>>
使用缓存的9大误区(下)转载
查看>>
appium键值对的应用
查看>>
MyEclipse 8.X 通用算法
查看>>
selenium.Phantomjs设置浏览器请求头
查看>>
分布式数据库如何选择,几种分布式数据库优缺点一览
查看>>
BZOJ 4443: 小凸玩矩阵【二分图】
查看>>
苹果 OS X制作u盘启动盘
查看>>
Jquery便利对象
查看>>
MVC: Connection String
查看>>
idea常用设置汇总
查看>>
Node.SelectNodes
查看>>
Lambda表达式语法进一步巩固
查看>>
Vue基础安装(精华)
查看>>
Git 提交修改内容和查看被修改的内容
查看>>
PAT - 1008. 数组元素循环右移问题 (20)
查看>>
请求出现 Nginx 413 Request Entity Too Large错误的解决方法
查看>>
配置php_memcache访问网站的步骤
查看>>