520. Detect Capital 的OJ代码笔记
来源:互联网 发布:java方法签名 异常 编辑:程序博客网 时间:2024/05/20 16:43
Given a word, you need to judge whether the usage of capitals in it is right or not.
We define the usage of capitals in a word to be right when one of the following cases holds:
- All letters in this word are capitals, like "USA".
- All letters in this word are not capitals, like "leetcode".
- Only the first letter in this word is capital if it has more than one letter, like "Google".
Example 1:
Input: "USA"Output: True
Example 2:
Input: "FlaG"Output: False
Note: The input will be a non-empty word consisting of uppercase and lowercase latin letters.
Subscribe to see which companies asked this question.
通过代码如下:
public class Solution {
public boolean detectCapitalUse(String word) {boolean flag=false;
int small=0;
int big=0;
char[] temp=word.toCharArray();
System.out.println("word:"+word);
if(word.length()==1){
if(temp[0]>='A' && temp[0]<='Z')
{
return true;
}
}
for(int i=0;i<temp.length;i++){
if(temp[i]>='A' && temp[i]<='Z')
{
big=big+1;
}
else if(temp[i] >= 'a' && temp[i]<='z')
{
small=small+1;
}
}
//第一种,判断是否全为大写
if(big==temp.length){
flag=true;
return flag;
}
//第二种,判断是否全为小写
else if(small==temp.length){
flag=true;
return flag;
}
else if(temp[word.length()-1]>='A' && temp[word.length()-1]<='Z'&&big==1 && small==(temp.length-1)){
flag=false;
return flag;
}
else if(big==1 && small==(temp.length-1) && temp[0]>='A' && temp[0]<='Z')
{
flag=true;
}
System.out.println("big:"+big+",small:"+small);
return flag;
}
}
0 0
- 520. Detect Capital 的OJ代码笔记
- 520. Detect Capital 使用正则表达式OJ代码
- [leetCode刷题笔记]520. Detect Capital
- LeetCode 520. Detect Capital 自己的解法
- 【Leetcode】520. Detect Capital
- 520. Detect Capital (E)
- LeetCode 520. Detect Capital
- LeetCode 520. Detect Capital
- 520. Detect Capital
- LeetCode-520. Detect Capital
- 520. Detect Capital
- leetcode 520. Detect Capital
- 520. Detect Capital
- 520. Detect Capital
- 520. Detect Capital
- 520. Detect Capital
- leetcode 520. Detect Capital
- [leetcode] 520. Detect Capital
- git
- Centos下我常用的一些命令
- String/StringBuffer/StringBuilder,Java序列化,线程安全,线程同步,ThreadLocal
- PAT甲级1029. Median (25)
- [LeetCode] 97. Interleaving String
- 520. Detect Capital 的OJ代码笔记
- ubuntu16.04下安装Scrapy
- 数塔(HDU-2804)
- Cassandra issue
- Cassandra issue
- 应用程序请求注册对Input事件的监听
- C++11 并发指南一(C++11 多线程初探)
- windows下面安装postgreSQL
- Ubuntu16.04+Cuda8.0+Caffe+ Opencv3.2+Matlab2015b的配置、安装与编译(一)