Map和HashCode(Map and 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
最满意答案
您可以简化您的选择:选择硬币并重新选择硬币或不选择硬币(递归将涵盖所有选项)
更换:
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.
相关问答
更多-
递归状态monad用于在构建列表时累积值?(Recursive state monad for accumulating a value while building a list?)[2023-02-26]
这是一个简单的折叠 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名称。 您可以创建一个更简单的“帮助器”函数,它使用自己的 ...
-
异步递归函数(Async Recursive Function)[2023-06-17]
buildAddonMenu需要是一个async函数,并使用await运算符进行调用。 尽管无论如何菜单可能会异步继续构建(因为buildAddonMenu返回一个稍后可能会更新的对象),所以在同步调用buildAddonMenu后立即尝试渲染或检查菜单将失败 - 您只能获得由数据提供的第一级菜单项在同步通话中。 所以每次打电话给建立buildAddonMenu应采取的形式 menuObject = await buildAddonMenu(....) 包括在app.Intent调用。 对上述内容进行更 ... -
用于构建数组的递归函数(Recursive function to build array)[2023-09-02]
我会说像使用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 ... -
递归函数构建(Recursive function building)[2023-03-30]
您可以简化您的选择:选择硬币并重新选择硬币或不选择硬币(递归将涵盖所有选项) 更换: 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 ,这是一个用于可枚举函数的特殊关 ...
-
何时使用递归函数?(When to use recursive function?)[2023-01-19]
由于您正在从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)). 虽然你不能用之前提到的语言(变量实际上只是一个值的别名)。 为什么他 ...