HR_Counting Valleys

把字符串数字化之后应该从 i>0开始判断而不是 i>1 因此错了4个testcases。

 

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the countingValleys function below.
def countingValleys(n, s):
    s_num = []
    for i in range(n):
        if s[i:i+1] == 'U':
            if i == 0:
                s_num.append(1)
            else:
                s_num.append(s_num[i-1] + 1)
        else:
            #'D'
            if i == 0:
                s_num.append(-1)
            else:
                s_num.append(s_num[i - 1] - 1)
    count = 0
    for i in range(len(s_num)):
        if i > 0:
            if s_num[i] == 0 and s_num[i-1] == -1:
                count = count +1
    return count

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    n = int(input())

    s = input()

    result = countingValleys(n, s)

    fptr.write(str(result) + '\n')

    fptr.close()

  

posted @ 2018-09-30 16:07  夜歌乘年少  阅读(261)  评论(0编辑  收藏  举报