import org.junit.Test; /** * @Description 520:https://leetcode.com/problems/detect-capital/ * @Author zlb * @Date 2019/7/2 10:50 **/ public class DetectCapital { public boolean detectCapitalUse(String word) { int length = word.length(); char[] charArray = word.toCharArray(); String firstLetter = ""; int upperNum = 0; for (int i=0; i<length; i++) { String result = upperOrLower(charArray[i]); if (i==0) { firstLetter = result; } if ("upper".equals(result)) { upperNum++; } } if (upperNum == 0 || upperNum == length || (upperNum ==1 && "upper".equals(firstLetter))) { return true; } return false; } private String upperOrLower(char c) { if (c >= 'A' && c <= 'Z') { return "upper"; } return "lower"; } @Test public void test() { // String word = "Flag"; // String word = "leetcode"; String word = "USA"; System.out.println(detectCapitalUse(word)); } }