将代码从目标c转换为C#

时间:2014-05-23 19:59:56

标签: c# objective-c

我尝试将此代码从目标c转换为c#,但没有运气

int solution(NSMutableArray *A) {

    NSMutableArray* B = [A mutableCopy];

    int current = 0;
    int jumps = 0;

    while (current >= 0 && current < [B count]) {
        if (![[B objectAtIndex:current] isEqual:@"none"]) {
            int x = [[B objectAtIndex:current] intValue];
            int next = current + x;
            [B replaceObjectAtIndex:current withObject:@"none"];
            current = next;
            jumps += 1;


        }
        else{
            return -1;
        }
    }

    return jumps;


}

我的版本

    public int solution(int[] A)
    {

        if (A.Length == 0) return -1;

         int[] B = new int[A.Length];
         Array.Copy(A, B, A.Length);

         int current = 0;
         int jumps = 0;

         while (current >= 0 && current < B.Length)
         {
             if (B[current] != int.MinValue)
             {
                 int x = B[current];
                 int next = current + x;
                 B[current] = int.MinValue;
                 current = next;
                 jumps += 1;
             }
             else
             {
                 return -1;
             }
         }

         return jumps;


    }

您怎么看?

1 个答案:

答案 0 :(得分:1)

这样的东西?

public int Solution(List<string> a) {

List<string> b = a;

int current = 0;
int jumps = 0;

while (current >= 0 && current < b.Count) {
    if (b[current] != "none") {
        int x = int.Parse(b[current]);
        int next = current + x;
        b[current] = "none";
        current = next;
        jumps += 1;
    }
    else{
        return -1;
    }
}

return jumps;
}