给定一个单词,你需要判断单词的大写使用是否正确。
我们定义,在以下情况时,单词的大写用法是正确的:
全部字母都是大写,比如"USA"。
单词中所有字母都不是大写,比如"leetcode"。
如果单词不只含有一个字母,只有首字母大写, 比如 "Google"。
否则,我们定义这个单词没有正确使用大写字母。
示例 1:
输入: "USA"
输出: True
示例 2:
输入: "FlaG"
输出: False
注意: 输入是由大写和小写拉丁字母组成的非空单词。
来源
链接:https://leetcode-cn.com/problems/detect-capital
package demo4.jd;
import java.util.Scanner;
public class guazi {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(detectCapitalUse(sc.nextLine()));
}
public static boolean detectCapitalUse(String word) {
if ( word == null || word == "" )return false;
int count = 0;
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) <= 'Z' && word.charAt(i) >= 'A')
count++;
}
if (count == word.length() ||
(count == 1 && word.charAt(0) >= 'A'&& word.charAt(0) <= 'Z')
|| count == 0)
return true;
return false;
}
}
给定一个整数数组 A,坡是元组 (i, j),其中 i < j 且 A[i] <= A[j]。这样的坡的宽度为 j - i。
找出 A 中的坡的最大宽度,如果不存在,返回 0 。
示例 1:
输入:[6,0,8,2,1,5]
输出:4
解释:
最大宽度的坡为 (i, j) = (1, 5): A[1] = 0 且 A[5] = 5.
示例 2:
输入:[9,8,1,0,1,9,4,0,4,1]
输出:7
解释:
最大宽度的坡为 (i, j) = (2, 9): A[2] = 1 且 A[9] = 1.
提示:
2 <= A.length <= 50000
0 <= A[i] <= 50000
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
System.out.println(maxWidthRamp(arr));
}
public static int maxWidthRamp(int[] A) {
// 存储最大值
int gap = Integer.MIN_VALUE;
// 外层循环从头开始
for (int i = 0; i < A.length; i++) {
// 内层循环从尾开始,缩短循环次数
for (int j = A.length - 1; j >= 0; j--) {
// 如果j-i < gap,也就没有替换的必要
if (j - i < gap) {
break;
}
if (A[j] >= A[i]) {
// 如果发现最大值,该层循环也没有必要进行下去,后面的循环j值--,gap更小
gap = j - i;
break;
}
}
}
return gap;
}