未登录,请登录后再发表信息
最新评论 (0)
播放视频

编程面试题4:如何在哈希表中数出唯一的字符串

IQ 4: How to count number of unique strings in HashMap?

[音乐]
[music]
面试题4
Interview question 4
我们开始
Let’s get started
写一个函数 它返回相同字符串作为参数的调用次数
Write a function that returns the count of unique string values passed into a method
什么意思
What do I mean by that
在同一个函数里 接收一个字符串作为参数
Let’s just assume we have calls to the same method which take in a string parameter.
就像这样
Something like this.
函数“Count” 有一个字符串类型的参数
we have a method called Count that has a parameter of type String
让这个函数调用一个值“A”
So let’s just say we pass in the value ‘A’ to this method.
计算函数对参数A的调用次数
We pass in this value A, we want to get a count of the value that is passed in
这第一个函数就只是简单地返回1
So this first method would just simply return one.
如果再次在同一个函数里调用相同的参数
If we call that same method again right below it with the same parameter passed in
返回值应该是2
the value that’s supposed to be returned would be two.
如果第三次调用函数 返回值应该是3
If we call that that method a third time, the value that’s supposed to be returned would be three.
看这里 这是另一个例子
Here’s a look, here’s another example.
函数是相同的 但在函数里调用的参数是不同的
We had the same method, but for this method you can see here we’re passing in different values.
再次从顶端开始 把A作为“Count”方法的参数
So let’s start from the top once more, we have a method called Count, we pass in A.
它应该返回1
This method is supposed to return one.
再次调用这个函数时 传递另一个值
But then we call this method again and we pass in another value.
这里返回值应当是1
This value this the value that’s supposed to be returned here would be one.
因为这是一个全新的参数 所以会返回1
The reason that one is being returned is because this is an entirely new value.
我们已经从A切换到B
We’ve switched from A to B.
再次调用这个函数并调用B的值 返回值就是2
And if we call this method one more time and pass in B, the value we get is two.
基本上 函数最后调用的值
So basically the last method that is passed in is
就是我们最需要的 为了让它显得更清楚一点
the value that we are most concerned about,and to make this a little bit more clearer
接着在下面多解释一点
then explain it a little bit more down here.
调用相同的字符串 Count就会相应地增加
So basically the count is incremented for the same string passed in
当一个新的字符串被传入时 它就会被重置
and should be reset when a new string is passed in.
这清楚地解释了这里所发生的事
So this explains clearly what we’re doing here.
如果这个函数调用3次A
If we pass in three As to this method
count就会相应增加 这是结果为3的原因
the count is supposed to be incremented, that’s why we’re getting here.
但如果先调用A 然后再调用B
But if we pass in A and then we pass in B here
count B的返回值就会重置
the return value for this count B will reset the count.
然后从这里开始 如果调用另一个B count就会增加
And then from here if we pass in another B, this count is supposed to be incremented.
另一个需要注意的是 最后一次的函数调用是最重要的
The most another thing to take note of is that the most important function call will be the last function call
不管上一次调用的是什么 那都是最重要的函数调用
So basically whatever method is called last will be the method that is the most important to us
或者说就是我们正在寻找的值
or the value that we are actually looking for
为了完成这个count函数 第一步我们需要做的
And in order to implement this count function, the first thing we need to do
是创建一个叫做哈希表的东西
is create this thing called hashmap.
这里我们需要的数据结构基本就是哈希表了
The data structure that we need is a hashmap pretty much.
那哈希表是什么呢
Now what is hashmap.
哈希表是一个关联数组 由键值对组成
Hashmap implements an associative array, that consists of key value pairs.
这就是哈希表的样子 这是哈希表里的对象
This is what a hashmap looks like.Have the object of the hashmap
在两种数据类型里 一个是字符串 一个是整型
Within have two data types, a string and an integer
这个字符串会和传递进count函数的值进行关联
This string is going to be associated with the value that is passed into the count method
这个整数将用来记录参数调用的次数
And this integer is going to be used to keep track of the count of the element passed in
当你处理哈希表时 有两个方法需要考虑
And when you’re dealing with hashmaps there are two methods you need to consider
第一个是put方法
The first one is the put method.
这个put方法是一个接收键和键值的方法
This put method is a method that takes on a key and a value.
它是用来给哈希表增加一个元素的
And it’s basically a method that’s used to add an element to a hashmap.
这个特别的函数将包含两个值
And this particular element is going to consist of two values
就是一个键和与之关联的键值
which would be a key and the value associated with that particular key.
另一个是get方法
Another method is the get method.
get是根据传递进来的键获取值的方法
The get method is pretty much a method that is supposed to retrieve the value based on the key that’s passed in.
例如 如果这里传入的是A
So for example if you were to pass in the A here
在这里传递的是键
then basically if you were to pass in the value here
与这个键所关联的值就会返回
you would have a value that is associated with this key that is going to be returned when executing this method
一个能让它清楚一点的方法就是写代码
And I think one way to make this a little bit clear is just our coding.
第一步 实现count函数
So the first thing I want to do is implement the count function
看起来就像这样
which is going to look something like this.
好 现在写一个方法 “public static count”
Alright so I’m gonna now write the method, public static count
“public static int” 让这个方法返回一个整数
Well public static int and we want this method to return a number
同时让这个方法接收一个字符串类型的参数
And we also want this method to take in a parameter of type String.
把它命名为“myLetter”
You’re going to name it myLetter.
在实际执行这个方法之前
Now before we actually start the implementation of this method
我们要在这两个方法之外创建两个变量
we want to create two variables outside this method.
一个是哈希表 另一个是整型的count
We want to create a hashmap and we also want to create an integer of count.
就像我在以前的幻灯片中解释过的那样
So just like I explained in the slide before
输入“static HashMap”
we’re going to go static hashmap
这个值将会是一个字符串
And as I said before, this value is going to be a string.
而这里的值是一个整型 它代表count
And the value here is going to be an integer which represents the count.
我们将把这个哈希表命名为hm 然后等于 new HashMap
We’re going to name this hashmap hm so just equal to a new hashmap.
同时创建一个新的int值count
And we also want to create an int value called count.
顺便对这个变量初始化
And I’m just gonna intialize this variable here.
变量count用于记录传入函数的元素
Now this count variable is used to keep track of the element that is passed in to this method.
我马上就会向你们展示如何做
I’m going to show you more about how to do this right now.
第一步是 这个方法将会接收一个字母
So the first thing we want to do is we first of all know that this method is going to take in a letter
A或者B 不论接收哪个 这里都会接收一个字母
Either A B, whatever letter is going to take, we’re going to take in a letter here.
第一件事是增加一个if语句
So the first thing we want to do is add an if condition.
“if hm.get(myLetter) == null”
if hm dot get myLetter equals null
然后
then I want to do something
如果我试着在哈希表里寻找一个字母
So basically what I’m saying here is if I try to look for a letter in this hashmap
这里这个作为参数传递的字母并不存在
and this letter that’s passed in here doesn’t exist
然后我要把那个字母加进哈希表里
then I want to add that letter to the hashmap
这样 “hm.put(myLetter)”
I am going to do that like this, hm dot put myLetter
对于这里的这个值 我要传递的是1
And then for this value over here I want to pass in one
这就是说 如果一个字母不存在基于传递值构建的哈希表里
So this is basically stating that if a letter doesn’t exist within the hashmap based on the letter that’s passed in
就把那个字母添加进哈希表 把count设置为1
Then I want to add that letter to the hashmap and I want the count to be set to one.
另一件事是 同样把这里声明过的count设置为1
Another thing I want to do is also set the count that I’ve declared up here equal to one.
现在count被用来记录所传入的特定字母
Now this count is pretty much going to be used to keep track of a specific letter that is passed in.
在写完这个if条件语句后
So after I finish writing this if conditional
接着在它下面增加else
I’m going to add an else clause right below it.
其它的 如果这个else语句里的字母存在
else if the letter so basically in this else clause if the letter does exist
接着把count增加1
Then I want to increment the count by one.
[打字中]
[typing]
我也要再次把字母加进哈希表里
And I also want to add the letter again to the hashmap.
将传递进的字母的计数增加1
And increment the count of the letter passed in by one
“hm.put(myLetter , count)”
put my letter and count.
就在这个else语句的末尾 输入“return hm.get (myLetter)”
At the end right after this else clause I just want to return hm.get myLetter.
好了
and that’s it.
那几乎就是函数应有的样子了
That’s pretty much what the function is supposed to look like.
现在下一件事是测试 向上回到主函数里
Now the next thing to do will be to test it out so I want to go up to my main method here.
调用一次这个count函数
And call this count method once.
从这个最简单的例子开始 首先只传递A的值
Let’s start off with the easiest example first so I just simply pass in A.
把它打印到控制台
We print this out to the console as well
只传递A的值 到这上面来 保存
I simply pass in an A and I’ll go up here I save.
结果应该会是什么呢? 这个我之前解释过了
What do you think I’m supposed to get back, I’ve explained this before.
好吧 我们看看
Well let’s see.
到这下面来 得到的结果是1
Go down here, we get one.
这个以前解释过了 当你只传递了……结果就是1
Explained this earlier you get one when you pass in only……
当你只调用一次这个方法 所得到的结果A的计数1
when you call this method only once this is the value that you’ll get back to get one count for A
所以 让它变得复杂一点 但不会太难
So make this a little bit more complex but shouldn’t be too hard
count(“A”)
Count passing in A
再次 count(“A”) 现在应该得到的结果
Count passing in A again, what I’m supposed to get now.
是3 运行
Three, run it.
正好就是我们所期望的值3
We’re getting three exactly what we are expecting
就像我以前在陈述里说的
Now as I said before in my presentation
最后的方法调用将会是最重要的
the last method call is going to be the most important
所以这就是我最关注的值
So this is the value that I’m most concerned about
有三个“A” 如果你打印它 到这下面 count(A)
There’s three As and if you print it out down here count A
就会得到所调用的A的计数
Then we’re going to get the count of the As that have been passed in
如果我调用B
So if I would pass in B
我只会得到2
I would only get two
因为只有两个B被调用 即使在上面用A调用它
Because there is only two Bs that are being passed in and even though I’m calling it A up here
最后一个方法调用的值B就是最重要的值
The most significant value that’s going to be returned will be the last method call which will be B
结果就是所有这些B的计数
So I’m going to get a count of both of those Bs
再次运行 结果是2
Run this again, we get two
很简单吧
And that’s pretty much it pretty straightforward here
我们把它放大一分钟
We zoom out for a minute
如果你有任何问题 请在下面的评论里留言
If you have any questions, please leave them in the comments below
如果你觉得在这里学有所得 别忘了给视频点赞
Don’t forget to like the video if you felt you’ve learned something here
同样别忘了订阅 下次再见
Also don’t forget to subscribe and until next time.

发表评论

译制信息
视频概述

编程面试题4,如何在哈希表中数出唯一的字符串。

听录译者

[B]倔强

翻译译者

B11101001

审核员

审核团1024

视频来源

https://www.youtube.com/watch?v=-ARqK794Btk

相关推荐