假设我正在经营一家书店,有人订购了一份图书清单。B,C,D,A,A,A(参数)如果所有这些书都在我的库存数组中找到,我需要返回true。如果传入的列表中有同一本书的两个实例(在本例中为A),我需要检查库存中是否有那么多该书的实例。我遍历了参数数组和库存数组,但是到目前为止,我的代码返回了true,即使我的库存中只有一本A书。我陷入了编写代码来检查库存中是否有3A书的逻辑上。本质上,客户是在下订单,我需要告诉他们我是否有他想要的书。订单列表将按零售价值的降序排列。所以,这三本A级的书将会紧邻在一起,如果这样做更容易的话。这是我到目前为止所掌握的框架。
public boolean checkAvailability(customersOrder listToCheck) {
for(int i = 0; i<listToCheck.items.length; i++){ //listToCheck's items array
for(int j = 0; j<items.length; j++){ //inventory array
if(listToCheck.items[i].equals(items[j])){
return true;
}
}
}
return false;
}发布于 2014-11-28 00:26:45
这非常依赖于您存储数据的方式,最简单和最通用的方法几乎适用于所有的数据设计,但是:
现在,当您查看是否有足够的书时,可以检查是否有足够的书来涵盖订单中的每一本书。
发布于 2014-11-28 00:48:05
因为这看起来像家庭作业(过于简单的限制性数据结构),所以只是一个想法
Walk the listToCheck
For the ith element, say A we handle all As
If it occurs before i too, skip (you handled it already)
Count the same items >= i: `quantity`
Loop the stock to find `#quantity` items.
If after the loop #quantity not reached, return failure
If after the walk of listToCheck reached
return success如果相同的项目一个接一个地出现,则可以简单地确定数量,并且循环变得更简单。
https://stackoverflow.com/questions/27175056
复制相似问题