题解 | #两种排序方法#

两种排序方法

https://www.nowcoder.com/practice/839f681bf36c486fbcc5fcb977ffe432

class solution:
#     def my_input(self):
#         n = int(input())
#         words = []
#         for i in range(n):
#             words.append(input())
#         return n, words
    
    def isLengthsAlignment(self, words: list[str], n: int) -> bool:
        for i in range(n - 1):
            if len(words[i]) > len(words[i + 1]):
                return False
        return True
    
    def isLexiAlignment(self, words: list[str], n) -> bool:
        # 此题注意sort()函数和sorted()是用法区别,以及是否有返回值
        newWords = sorted(words)
        return newWords == words
    
if __name__ == '__main__':
    n = int(input())
    words = []
    for i in range(n):
        words.append(input())
    solution = solution()
#     n, words = solution.my_input()
    ans1 = solution.isLengthsAlignment(words, n)
    ans2 = solution.isLexiAlignment(words, n)
    if ans1 and ans2:
        print('both')
    elif ans1 and not ans2:
        print('lengths')
    elif not ans1 and ans2:
        print('lexicographically')
    else:
        print('none')
全部评论

相关推荐

评论
点赞
收藏
分享
牛客网
牛客企业服务