我正在尝试纠正我在采访中遇到的特定问题(问题是找到频率恰恰为f的最小元素)。我认为我已经在很大程度上设计了解决方案,但是问题是关于一些故障,而这又会产生一些错误的输出。我试过了所有的东西,检查并重新检查了代码,但问题仍然存在。由于我在java编程中是初学者,所以请不要太在意,所以请帮助我。以下是我的尝试:不生成预期输出的代码
public static int smallestKFreq(int a[], int n, int f)
{
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
// Map is used to store the count of
// elements present in the array
for (int i = 0; i < n; i ++)
if (map.containsKey(a[i]))
map.put(a[i], m.get(a[i]) + 1);
else map.put(a[i], 1);
// Traverse the map and find minimum
// element with frequency f.
int res = Integer.MAX_VALUE;
Set<Integer> s = map.keySet();
for (int temp : s)
if (map.get(temp) == f)
res = Math.min(res, temp);
return (res != Integer.MAX_VALUE)? res : 1;
}
*“守则产生编译时错误” * *** ***什么编译错误? –
先生它产生不同的输出!!!'{1,2,3,2,2,1,3}'然后输出是'3' – BaijNR
所以...... **不**编译时错误。 –