最新评论 (0)


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

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
So let’s just say we pass in the value ‘A’ to this method.
We pass in this value A, we want to get a count of the value that is passed in
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
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.
This method is supposed to return one.
再次调用这个函数时 传递另一个值
But then we call this method again and we pass in another value.
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.
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.
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
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
The first one is the put method.
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.
Another method is the get method.
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.
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.
And we also want to create an int value called count.
And I’m just gonna intialize this variable here.
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.
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.
Now this count is pretty much going to be used to keep track of a specific letter that is passed in.
So after I finish writing this if conditional
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
Then I want to increment the count by one.
And I also want to add the letter again to the hashmap.
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.
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 passing in A
再次 count(“A”) 现在应该得到的结果
Count passing in A again, what I’m supposed to get now.
是3 运行
Three, run it.
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
Then we’re going to get the count of the As that have been passed in
So if I would pass in B
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
The most significant value that’s going to be returned will be the last method call which will be 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.