首页 \ 问答 \ Map和HashCode(Map and HashCode)

Map和HashCode(Map and HashCode)

使基于散列的集合的唯一hashCode更快地工作的原因是什么?还有什么不使hashCode可变?

在这里阅读但是不明白,所以我读了一些其他资源并最终得到了这个问题。

谢谢。


What is the reason to make unique hashCode for hash-based collection to work faster?And also what is with not making hashCode mutable?

I read it here but didn't understand, so I read on some other resources and ended up with this question.

Thanks.


原文:https://stackoverflow.com/questions/20160757
更新时间:2023-11-12 14:11

最满意答案

您可以简化您的选择:选择硬币并重新选择硬币或不选择硬币(递归将涵盖所有选项)

更换:

res1+=howManyCoins_aux(coins,size,sum-coins[i],chosen);
res2+=howManyCoins_aux(coins,size,sum-coins[i],chosen-1);

res1 = howManyCoins_aux(coins, size, sum-coins[i], chosen);
res2 = howManyCoins_aux(coins, size, sum, chosen-1);

如果你只能选择每个硬币中的一个:(这是变化问题

res1 = howManyCoins_aux(coins, size, sum-coins[i], chosen-1);
res2 = howManyCoins_aux(coins, size, sum, chosen-1);

你的算法有点不清楚,我认为有很多重复。 您可以摆脱for循环,并将您的功能更改为:(未经测试)

int howManyCoins_aux(int *coins, int size, int sum, int chosen, int pos)
{
  if (sum == 0) return chosen;
  if (sum < 0 || pos == size) return 9999999;
  int res1 = 9999999;
  if (coins[pos] >= sum)
    res1 = howManyCoins_aux(coins, size, sum-coins[pos], chosen+1, pos);
  int res2 = howManyCoins_aux(coins, size, sum, chosen, pos+1);
  return min(res1, res2);
}

话虽这么说,递归可能不是要走的路(它需要很长时间,即使在小数据集上也是如此)。 听起来像整数编程 。 在链接中有几个选项可以解决它。


You can simplify your choices to: Choosing the coin and being able to re-choose the coin or not choosing the coin (recursion will cover all option)

Replace:

res1+=howManyCoins_aux(coins,size,sum-coins[i],chosen);
res2+=howManyCoins_aux(coins,size,sum-coins[i],chosen-1);

with

res1 = howManyCoins_aux(coins, size, sum-coins[i], chosen);
res2 = howManyCoins_aux(coins, size, sum, chosen-1);

If you can only pick 1 of each coin: (which is the change-making problem)

res1 = howManyCoins_aux(coins, size, sum-coins[i], chosen-1);
res2 = howManyCoins_aux(coins, size, sum, chosen-1);

Your algorithm is somewhat unclear, and I think there's a lot of repetition. You can get rid of the for loop, and change your function to something like: (untested)

int howManyCoins_aux(int *coins, int size, int sum, int chosen, int pos)
{
  if (sum == 0) return chosen;
  if (sum < 0 || pos == size) return 9999999;
  int res1 = 9999999;
  if (coins[pos] >= sum)
    res1 = howManyCoins_aux(coins, size, sum-coins[pos], chosen+1, pos);
  int res2 = howManyCoins_aux(coins, size, sum, chosen, pos+1);
  return min(res1, res2);
}

That being said, recursion probably isn't the way to go (it will take very long, even on small datasets). Sounds like integer programming. There are a few options for solving it in the link.

相关问答

更多
  • 这是一个简单的折叠 foo :: [Integer] -> ([Integer], Int) foo [] = ([], 0) foo (x : xs) = let (rs, n) = foo xs in (2 * x : rs, if x == 5 then n + 1 else n) 或用foldr表达 foo' :: [Integer] -> ([Integer], Int) foo' = foldr f ([], 0) where f x ( ...
  • factorial是一个全局标签,因此可以进行符号插入 。 请参阅Linux上的抱歉动态库状态 。 (另外, 使用LD_PRELOAD和一些文档 插入malloc的示例 )。 在创建共享库时,不会假定call factorial指令的目标是在同一文件中定义的factorial: label 。 那是因为你使用了.globl factorial 。 正如Jester所指出的那样,您应该为call目标定义一个单独的本地标签,以便保留全局factorial名称。 您可以创建一个更简单的“帮助器”函数,它使用自己的 ...
  • buildAddonMenu需要是一个async函数,并使用await运算符进行调用。 尽管无论如何菜单可能会异步继续构建(因为buildAddonMenu返回一个稍后可能会更新的对象),所以在同步调用buildAddonMenu后立即尝试渲染或检查菜单将失败 - 您只能获得由数据提供的第一级菜单项在同步通话中。 所以每次打电话给建立buildAddonMenu应采取的形式 menuObject = await buildAddonMenu(....) 包括在app.Intent调用。 对上述内容进行更 ...
  • 我会说像使用array_walk()来解析数组 function insert_array(&$item1, $key, $userdata) { if($item1 === $userdata['Product_ID']) { $item1 = $userdata['insert']; } } $data['product_id'] = PRODUCT_ID; $data['insert'] = $b; array_walk($a,'insert_array',$dat ...
  • 您可以简化您的选择:选择硬币并重新选择硬币或不选择硬币(递归将涵盖所有选项) 更换: res1+=howManyCoins_aux(coins,size,sum-coins[i],chosen); res2+=howManyCoins_aux(coins,size,sum-coins[i],chosen-1); 同 res1 = howManyCoins_aux(coins, size, sum-coins[i], chosen); res2 = howManyCoins_aux(coins, size, ...
  • 块中的return关键字不会仅仅结束块,它将结束父函数(在本例中为def children )。 所以,你没有像你打算的那样返回map的结果,而是在第一次迭代后返回整个函数。 它仍会激发递归调用,但在每次递归调用中都会发生相同的行为。 这就是为什么你在结果中看到嵌套,但每个级别只有一个元素而不是数组。 所以,你可以简单地删除return单词并保持其他所有内容相同。 请记住,除非您强制从方法中提前退回,否则return从来就不是必需的。 或者,您可以用next替换return ,这是一个用于可枚举函数的特殊关 ...
  • 由于您正在从setTimeout调用方法,因此您并没有真正使用递归。 当函数重新执行时,初始化将退出..( 每次执行都不会阻塞下一个 ) 在javascript中使用递归操作DOM是错误的,因为只有在整个递归完成后才会重绘页面。所以,您将失去动画的所有中间步骤并从第一步移动到最后一步。 正如在问题的评论中提到的,你可以用CSS单独实现这种动画。 只需通过CSS设置DOM节点的transition属性 ,然后通过应用新类( 或直接通过javascript )更改属性 div{ margin-left: ...
  • 如果要以递归方式构建列表(向量等),请使用以下模式: private: void InternalBuild(std::vector & vec) { // Add data to vec // Possibly call recursively InternalBuild(vec); } public: std::vector RecursiveBuild() { st ...
  • 第一个变种 (defn hierarchy* [res x] (if-not x res (recur (conj res x) (get m x)))) (defn hierarchy [x] (hierarchy* [] x)) 第2 (defn hierarchy [x] (loop [res [] next-x x] (if-not next-x res (recur (conj res next-x) (get m ...
  • 所以,这里有很多话要说。 首先,与大多数声明性语言一样,变量不能真正改变价值。 这意味着X = 1.将按照您的预期将1与X统一,但如果您在查询中添加X = 2. ( X = 1, X = 2. ),Prolog将false 。 这背后的原因是你不能将1与2统一并且X真正变为1 ,因此X不能统一为2 。 虽然,与Haskell,Ocaml等不同,您可以部分绑定变量,如X = h(Y). 。 然后,您将能够进一步统一它X = h(a(Z)). 虽然你不能用之前提到的语言(变量实际上只是一个值的别名)。 为什么他 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。