Valid Parentheses

#import <Foundation/Foundation.h>

bool validParentheses(NSString *s) {
    // TODO: Return a boolean value indicating whether the
    // order of parentheses in the given string is valid
    if (s.length==0) {
        return 0;
    }
    int sum = 0;
    NSMutableArray *res = @[].mutableCopy;
    for (int i = 0; i<s.length; i++) {
        if ([s characterAtIndex:i]==(unichar)'(') {
            [res addObject:@('(')];
            sum++;
        }
        if ([s characterAtIndex:i]==(unichar)')') {
            if ([[res lastObject] isEqual:@('(')]) {
                [res removeObjectAtIndex:res.count-1];
            }else{
                [res addObject:@(')')];
            }
            sum--;
        }
    }
    if (sum!=0||[res count]!=0) {
        return 0;
    }
    return 1;
}

简化版:


#import <Foundation/Foundation.h>

bool validParentheses(NSString *s) {
  int brackets = 0;
  for (int i = 0; i < [s length]; i++) {
    if ([s characterAtIndex: i] == '(') brackets++;
    else if ([s characterAtIndex: i] == ')') if (--brackets < 0) return false;
  }
  return !brackets;
}
2017/12/25 posted in  codeWars

Follow that Spy

自己的麻烦些的代码:用了NSMapTable

#import <Foundation/Foundation.h>
NSString *findRoutes(NSArray *routes){
    if ([routes count]==1) {
        return [NSString stringWithFormat:@"%@, %@",routes[0][0],routes[0][1]];
    }
    NSMapTable *map = [NSMapTable mapTableWithKeyOptions:NSMapTableStrongMemory valueOptions:NSMapTableWeakMemory];
    NSMapTable *tol = [NSMapTable mapTableWithKeyOptions:NSMapTableStrongMemory valueOptions:NSMapTableWeakMemory];
    for (NSArray *tmp in routes) {
        [map setObject:tmp[1] forKey:tmp[0]];
        for (NSString* tmpTol in tmp) {
            if ([tol objectForKey:tmpTol]) {
                [tol setObject:@"1" forKey:tmpTol];
            }else{
                [tol setObject:@"0" forKey:tmpTol];
            }
        }
    }
    NSString *str = @"";
    NSString *end = @"";
    NSEnumerator *enumerator = [tol keyEnumerator];
    for (NSString *tmp in enumerator) {
        if ([[tol objectForKey:tmp] isEqualToString:@"0"]) {
            if (![map objectForKey:tmp]) {
                end = tmp;
                continue;
            }
            str = tmp;
        }
    }
    NSString *res = @"";
    res = [res stringByAppendingFormat:@"%@, ", str];
    while ([map objectForKey:str]) {
       res =  [res stringByAppendingFormat:@"%@, ", [map objectForKey:str]];
       str = [map objectForKey:str];
        if ([[map objectForKey:str] isEqualToString:end]) {
            res = [res stringByAppendingString:end];
            break;
        }
    }
    return res;
}

别人的简短代码:

#import <Foundation/Foundation.h>
NSString *findRoutes(NSArray *routes){

    NSMutableArray * routesM = [routes mutableCopy];
    NSMutableArray * answer = [@[routes[0][0]] mutableCopy];
    do{
        for(int i = (int)routesM.count - 1; i >= 0; i--){
            if([[answer lastObject] isEqualToString:routesM[i][0]]){
                [answer addObject:routesM[i][1]];
                [routesM removeObjectAtIndex:i];
            } else if([[answer firstObject] isEqualToString:routesM[i][1]]){
                [answer insertObject:routesM[i][0] atIndex:0];
                [routesM removeObjectAtIndex:i];
            }
        }
    } while (routesM.count > 0);
    return [answer componentsJoinedByString:@", "];
}
2017/12/23 posted in  codeWars

Simple Fun #4: Phone Call


#import <Foundation/Foundation.h>
int phoneCall(int min1, int min2_10, int min11, int s) {
    if (s<min1) return 0;
    int sum = 1;
    sum+= (s-min1)>min2_10*9 ? 9 : (s-min1)/min2_10;
    sum+= (s-min1-9*min2_10)<0 ? 0 : (s-min1-9*min2_10)/min11;
    return sum;
}
2017/12/23 posted in  codeWars

Don't give me five!

Don't give me five!

In this kata you get the start number and the end number of a region and should return the count of all numbers except numbers with a 5 in it. The start and the end number are both inclusive!

Examples:

1,9 -> 1,2,3,4,6,7,8,9 -> Result 8
4,17 -> 4,6,7,8,9,10,11,12,13,14,16,17 -> Result 12

The result may contain fives. ;-)
The start number will always be smaller than the end number. Both numbers can be also negative!

I'm very curious for your solutions and the way you solve it. Maybe someone of you will find an easy pure mathematics solution.

Have fun coding it and please don't forget to vote and rank this kata! :-)

#import <Foundation/Foundation.h>
int dontGiveMeFive(int start, int end)
{
  int sum = 0;
    for (int i = start; i<=end; i++) {
        if ([[NSString stringWithFormat:@"%i",i] rangeOfString:@"5"].location!=NSNotFound) {
            continue;
        }
        sum++;
    }
    return sum;
}
2017/12/23 posted in  codeWars

Moves in squared strings (I)


题目链接:https://www.codewars.com/kata/moves-in-squared-strings-i/train/objc

#import <Foundation/Foundation.h>

NSString *vertMirror(NSString *strng) {
    NSMutableString *res = [[NSMutableString alloc] init];
    NSArray *stringArray = [strng componentsSeparatedByString:@"\n"];
    for (NSString *tmpStr in stringArray) {
        NSMutableString * reverseString = [NSMutableString string];
        for(int i = 0 ; i < tmpStr.length; i ++){
            //倒序读取字符并且存到可变数组数组中
            unichar c = [tmpStr characterAtIndex:tmpStr.length- i -1];
            [reverseString appendFormat:@"%c",c];
        }
        
        if ([tmpStr isEqualToString:[stringArray lastObject]]) {
            [res appendString:reverseString];
            break;
        }
        
        [reverseString appendString:@"\n"];
        [res appendString:reverseString];
    }
    
    return res;
}

NSString *horMirror(NSString *strng) {
    NSMutableString *res = [[NSMutableString alloc] init];;
    NSArray *stringAry = [strng componentsSeparatedByString:@"\n"];
    for (int i = stringAry.count-1; i>=0; i--) {
        [res appendString:stringAry[i]];
        if (i==0) {
            break;
        }
        [res appendString:@"\n"];
    }
    return res;
}

NSString *oper(NSString *(*f)(NSString *), NSString *s) {
    return f(s);
}
2017/12/23 posted in  codeWars

Sum of many ints

题目链接:https://www.codewars.com/kata/moves-in-squared-strings-i/train/objc

注意long long类型。

#import <Foundation/Foundation.h>

long long f(long long n, long long m) {
    long long sum = 0;
    long long q = n/m;
    long long p = n%m;
    sum+= (m*(m-1)/2)*q;
    sum+= (p+1)*p/2;
    return sum;
}
2017/12/23 posted in  codeWars