LeetCode 520. Detect Capital 自己的解法

xiaoxiao2021-02-28  154

题目

Given a word, you need to judge whether the usage of capitals in it is right or not. 大意:给定一个单词,判断大写用法是否正确

如何判断

三种情况:每个字符全是大写,每个字符全是小写,除了首字符大写其它字符全小写

解法

也就是根据以上三种情况判断

class Solution { public boolean detectCapitalUse(String word) { if(word.toUpperCase().equals(word)) return true; if(word.charAt(0)>='A'&&word.charAt(0)<='Z'&&(word.substring(1).toLowerCase().equals(word.substring(1)))) return true; if(word.toLowerCase().equals(word)) return true; return false; } }
转载请注明原文地址: https://www.6miu.com/read-33485.html

最新回复(0)