在我尝试采用真实的面试之前,我已经采用了这个例子测试.面临的挑战是正确实施均衡指数问题.
我编写了某种解决方案,仅适用于简单示例和一些边缘情况.
这是代码:
typedef vector<int> container_t;
typedef container_t::const_iterator iterator_t;
int find_equilibrium(const container_t &A);
int equi (const container_t &A)
{
const std::size_t length = A.size();
if (length == 0)
return -1;
if (length == 1 && A[0] == 0)
return -1;
if (length == 1 && A[0] != 0)
return 0;
return find_equilibrium(A);
}
int find_equilibrium(const container_t &A)
{
unsigned int i = 0;
int sum = 0;
for (iterator_t iter = A.begin(); iter != A.end(); ++iter, …
Run Code Online (Sandbox Code Playgroud) 我正在阅读关于RosettaCode的示例,并且不完全知道以下行正在做什么.
let min,subheap = findMin heap' in let rtn = root topnode
Run Code Online (Sandbox Code Playgroud)
它似乎findMin heap'
是自包含的执行单元.我不知道它与"in"运算符的关系,也不了解在"in"运算符中使用let语句.
这是整个方法
let rec private findMin heap =
match heap with | [] -> raise Empty_Heap //guarded so should never happen
| [node] -> root node,[]
| topnode::heap' ->
let min,subheap = findMin heap' in let rtn = root topnode
match subheap with
| [] -> if rtn.k > min.k then min,[] else rtn,[]
| minnode::heap'' ->
let rmn = root minnode
if …
Run Code Online (Sandbox Code Playgroud) 我正在使用Rosetta Code的教程来计算Levenshtein距离.看起来他们的代码在Swift2中,所以我Binary operator '+' cannot be applied to operands of type '[Int]' and 'Repeated<String.CharacterView>'
这样做时会出现这个错误:var cur = [i + 2] + empty
where let empty = repeatElement(s, count: 0)
.我怎么能这样做?