520. Detect Capital(检测数组的大小写)
2024-08-30 19:32:44
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".
Otherwise, we define that this word doesn't use capitals in a right way.
Example 1:
Input: "USA"
Output: True
Example 2:
Input: "FlaG"
Output: False 思路:将单词转换为大写得到up,将单词转换为小写得到low,若word与up或与low相等,则返回true,
否则去掉word的首字母得到last,若last转换为小写后仍与last相等,则返回true,
否则返回false。
public boolean detectCapitalUse(String word) {
int len=word.length();
String up = word.toUpperCase();
String low = word.toLowerCase();
if (word.equals(up) || word.equals(low))
return true;
String last = word.substring(1, len);
if (last.toLowerCase().equals(last))
return true;
return false;
}
最新文章
- IOS的Crash情况在Crashlytics平台上统计解决方案的一点遗憾(截止到2015年6月14日)
- Python中通过cx_Oracle访问数据库遇到的问题总结
- this指针基础介绍
- [terry笔记]Oracle10g/11g安装-redhat5.5
- 使用PHP获取汉字的拼音(全部与首字母)
- ExtJS4 动态加载
- 常用的bat命令
- Altium Designer规划电路板
- Android应用盈利广告平台的嵌入方法详解
- 【应用】Markdown 在线阅读器
- 我的Python成长之路---第三天---Python基础(13)---2016年1月16日(雾霾)
- JForum2.1.9 安装过程
- 以太坊开发DApp实战教程——用区块链、星际文件系统(IPFS)、Node.js和MongoDB来构建电商平台(一)
- python3学习笔记2---引用http://python3-cookbook.readthedocs.io/zh_CN/latest/2
- MySQL提示“too many connections”的解决办法
- 变量类型、构造器、封装以及 LeetCode 每日一题
- django查询数据库无法过滤月份的解决
- 4.2 explain 之 select_type
- CF1099F Cookies
- Vue:$set和$delete
热门文章
- 【BZOJ2733】永无乡(线段树,启发式合并)
- JS中的call()和apply()方法区别
- hihoCoder #1055 : 刷油漆 [ 树形dp ]
- msp430入门编程40
- Shell脚本的编写,sed的使用以及一些正则表达式
- PHP错误处理函数set_error_handler()的用法[转载]
- MAC地址泛红攻击
- python字符串连接方法效率比较
- 机器学习技法总结(五)Adaptive Boosting, AdaBoost-Stump,决策树
- activiti自己定义流程之自己定义表单(一):环境配置