活跃农民
- 积分
- 554
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2009-8-5
- 最后登录
- 1970-1-1
|
本帖最后由 gongzhen 于 2019-9-19 07:26 编辑
发一下我的代码吧 是OC写的。
- #import <Foundation/Foundation.h>
- #import <stdio.h>
- @interface MenuItemSolution : NSObject
- - (NSString *)findMenuFor:(double)target;
- @end
- @interface MenuItem : NSObject
- @property(nonatomic, strong) NSString *name;
- @property(nonatomic, assign) double price;
- - (instancetype)initWith:(NSString *)name price:(double)price;
- @end
- @implementation MenuItem
- - (instancetype)initWith:(NSString *)name price:(double)price
- {
- if (self = [super init]) {
- self.name = name;
- self.price = price;
- }
- return self;
- }
- @end
- @interface MenuItemSolution()
- @property(nonatomic, strong) NSMutableArray<MenuItem *> *list;
- @end
- @implementation MenuItemSolution
- -(instancetype)init {
- if (self = [super init]) {
- self.list = [NSMutableArray array];
- [self.list addObject:[[MenuItem alloc] initWith:@"a" price:1.8]];
- [self.list addObject:[[MenuItem alloc] initWith:@"b" price:1.5]];
- [self.list addObject:[[MenuItem alloc] initWith:@"c" price:1.5]];
- [self.list addObject:[[MenuItem alloc] initWith:@"d" price:1.5]];
- [self.list sortUsingComparator:^NSComparisonResult(MenuItem * _Nonnull obj1, MenuItem * _Nonnull obj2) {
- if (obj1.price == obj2.price) {
- return 0;
- } else if (obj1.price > obj2.price) {
- return 1;
- } else {
- return -1;
- }
- }];
- [self.list enumerateObjectsUsingBlock:^(MenuItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
- NSLog(@"%@:%f", obj.name, obj.price);
- }];
- }
- return self;
- }
- - (NSString *)findMenuFor:(double)target {
- NSMutableArray<NSString *> *temp = [NSMutableArray array];
- NSMutableArray<NSMutableArray<NSString *> *> *result = [NSMutableArray array];
- NSMutableArray *visited = [NSMutableArray new];
- for(int i = 0; i < self.list.count; i++) {
- [visited addObject:@(0)];
- }
- [self dfs:result temp:temp target:target visited:visited index:0];
- NSMutableString *str = [NSMutableString string];
- for(NSMutableArray<NSString *> *subList in result) {
- [str appendString:@"["];
- for(NSString *item in subList) {
- [str appendString:item];
- [str appendString:@","];
- }
- [str appendString:@"],"];
- }
- return [str copy];
- }
- - (void)dfs:(NSMutableArray<NSMutableArray<NSString *> *> *)result
- temp:(NSMutableArray<NSString *> *)temp
- target:(double)target
- visited:(NSMutableArray *)visited
- index:(int)index {
- if (index >= (int)self.list.count || target < 0) {
- return;
- }
- if (target == 0) {
- [result addObject:[temp copy]];
- return;
- }
-
- for (int i = index; i < self.list.count; i++) {
- MenuItem *item = [self.list objectAtIndex:i];
- [temp addObject:item.name];
- [visited replaceObjectAtIndex:i withObject:@(1)];
- [self dfs:result temp:temp target:target - item.price visited:visited index:i + 1];
- [temp removeLastObject];
- [visited replaceObjectAtIndex:i withObject:@(0)];
- }
- }
- @end
- int main (int argc, const char * argv[])
- {
- @autoreleasepool {
- MenuItemSolution *obj = [[MenuItemSolution alloc] init];
- NSString *res = [obj findMenuFor:3.0];
- NSLog(@"res:%@", res); /// res:[b,c,],[b,d,],[c,d,],
- }
- }
复制代码 |
|