hashmap.get()==运算符返回false [英] hashmap.get() == operator returning false
问题描述
我正在解决以下问题:
加里(Gary)是一个狂热的徒步旅行者.他精心跟踪自己的远足,并密切关注地形等小细节.在他的上一次远足期间,他正好走了n步.
Gary is an avid hiker. He tracks his hikes meticulously, paying close attention to small details like topography. During his last hike he took exactly n steps.
在他迈出的每一步中,他都指出这是上坡U步还是下坡D步.加里(Gary)的远足在海平面开始和结束,每次上或下的步数都代表海拔高度每变化1单位.我们定义以下术语:
For every step he took, he noted if it was an uphill, U, or a downhill, D step. Gary's hikes start and end at sea level and each step up or down represents a 1 unit change in altitude. We define the following terms:
一座山是一系列从海平面上升到从海平面上升到从海平面下降到海平面的连续步骤.
A mountain is a sequence of consecutive steps above sea level, starting with a step up from sea level and ending with a step down to sea level.
山谷是指一系列从海平面以下的连续台阶,从海平面向下的台阶开始,直至海平面的上升. 考虑到加里上次徒步旅行时上下的顺序,找到并打印出他所走过的山谷数.
A valley is a sequence of consecutive steps below sea level, starting with a step down from sea level and ending with a step up to sea level. Given Gary's sequence of up and down steps during his last hike, find and print the number of valleys he walked through.
例如,如果加里的路径为s = [DDUUUUDD],则他首先进入2个单位深度的山谷.然后,他爬上了2个单元高的山.最后,他回到海平面并结束了远足.
For example, if Gary's path is s = [DDUUUUDD], he first enters a valley 2 units deep. Then he climbs out an up onto a mountain 2 units high. Finally, he returns to sea level and ends his hike.
功能说明
在下面的编辑器中完成countingValleys函数.它必须返回一个整数,该整数表示加里所遍历的谷数.
Complete the countingValleys function in the editor below. It must return an integer that denotes the number of valleys Gary traversed.
countingValleys具有以下参数:
countingValleys has the following parameter(s):
n:Gary采取的步骤数
n: the number of steps Gary takes
s:描述他的路径的字符串 输入格式
s: a string describing his path Input Format
第一行包含一个整数,即加里远足的步数. 第二行包含一个字符串,其中包含描述其路径的字符.
The first line contains an integer , the number of steps in Gary's hike. The second line contains a single string , of characters that describe his path.
输出格式
打印一个整数,该数字表示加里在徒步旅行中经过的山谷数.
Print a single integer that denotes the number of valleys Gary walked through during his hike.
样本输入
8 乌杜杜(UDDDUDUU)
8 UDDDUDUU
样本输出
1
以下是我在Java中的实现.它适用于小型测试用例,但不适用于大型测试用例.
Below is my implementation in java. It works for the small test cases but not for the big ones.
static int countingValleys(int n, String s) {
//Use a hashmap to keep track of the number of moves.
HashMap<Character,Integer> map = new HashMap();
boolean sea = true;//check if we are at sea level
//if both D and U have the same total no, we are at sea level.
map.put('D',0);
map.put('U',0);
int valleys = 0;//count num of valleys
for(int i = 0; i < n; i++){
char key = s.charAt(i);
//check if we are at sea level
if(map.get('D') == map.get('U')){//<--PROBLEM
sea = true;
}
else
sea = false;
if(sea == true && key == 'D'){//if we are at sea level and our next key is D, we have a valley
valleys += 1;
}
map.put(key,map.get(key) + 1);//add move and update our hashmap
}
return valleys;
}
问题似乎出在"if(map.get('D')== map.get('U'))",似乎对于大数返回false,有人可以告诉我为什么吗?如果我将每个map.get()分配给一个变量,然后比较这些变量,它将起作用.
The problem seems to be at "if(map.get('D') == map.get('U'))", it seems to be returning false for big numbers, can someone tell me why? It works if I assign each map.get() to a variable and compare the variables instead.
我还使用"new Object()"类型在javascript中编写了完全相同的东西,它通过了所有测试用例,但是在带有hashmap的Java中不起作用,为什么?
I also wrote the exact same thing in javascript using the "new Object()" type and it passed all the test cases, but it is not working in java with hashmap, why is that?