题解 | #把数字翻译成字符串#

把数字翻译成字符串

https://www.nowcoder.com/practice/046a55e6cd274cffb88fc32dba695668

using System;
using System.Collections.Generic;
using System.Linq;


class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 解码
     * @param nums string字符串 数字串
     * @return int整型
     */
    public int solve (string nums) {
        if(nums == "0") return 0;
        int res = 1;
        int[] pre = new int[2];
        pre[0] = 1;
        pre[1] = 1;
        for(int i = 1; i < nums.Length; i++){
            if(nums[i] == '0' && (nums[i-1] - '0' > 2 || nums[i-1] == '0')){
                return 0;
            }
            if(nums[i] != '0' && (nums[i] - '0' <= 6 && nums[i - 1] == '2' || nums[i - 1] == '1') ){
                Console.WriteLine("pre:" + pre[i % 2]);
                res = res + pre[i % 2];
            }
            Console.WriteLine(res);
            pre[i % 2] = res;
        }
        return res;
    }
}

全部评论

相关推荐

02-16 10:35
已编辑
西安科技大学 后端
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务