当前位置:网站首页>Force buckle day31
Force buckle day31
2022-07-06 07:25:00 【Congruence_ vinegar】
We define , When , The capitalization of words is correct :
- All the letters are uppercase , such as
"USA"
. - All the letters in a word are not capitalized , such as
"leetcode"
. - If a word contains more than one letter , Only the first letter is capitalized , such as
"Google"
.
Give you a string word
. If capitalized correctly , return true
; otherwise , return false
.
All letters are uppercase or lowercase ===True
Only the first letter is capitalized ===True
Law 1 : Rough
ASCII A lowercase letter 26 Letters :97~122 Capitalization 26 Letters :65~90
class Solution {
public boolean detectCapitalUse(String word) {
// All letters are uppercase or lowercase Yes
// Only the first letter is capitalized Yes
// Other mistakes
int small=0;
int big=0;
char first=word.charAt(0);
for(int i=0;i<word.length();i++){
char c=word.charAt(i);
if(c>=65&&c<=90){// Capitalization
big++;
}else if(c>=97&&c<=122){// A lowercase letter
small++;
}
}
if(big==word.length()||small==word.length()){// The case where the letters are all uppercase or lowercase
return true;
}
for(int i=1;i<word.length();i++){
char c=word.charAt(i);
if(first>=65&&first<=90){// title case
if(!(c>=97&&c<=122)){// Others are not lowercase
return false;
}
}else{// The initial is not capitalized
return false;
}
}
return true;// title case , Other lowercase cases
}
}
improvement : character ( If the first letter is upper case and the rest is lower case, just (big==1&&first>=65&&first<=90))
class Solution {
public boolean detectCapitalUse(String word) {
// All letters are uppercase or lowercase Yes
// Only the first letter is capitalized Yes
// Other mistakes
int small=0;
int big=0;
char first=word.charAt(0);
for(int i=0;i<word.length();i++){
char c=word.charAt(i);
if(c>=65&&c<=90){// Capitalization
big++;
}else if(c>=97&&c<=122){// A lowercase letter
small++;
}
}
if(big==word.length()||small==word.length()||(big==1&&first>=65&&first<=90)){
return true;
}
return false;
}
}
improvement : Array methods
class Solution {
public boolean detectCapitalUse(String word) {
char[] c = word.toCharArray();
int upper = 0;// Number of capital letters
int lower = 0;// Number of lowercase letters
for(int i=0;i<c.length;i++) {
if(c[i]>='a') {
lower++;
}else {
upper++;
}
}
if(upper==c.length) { // All capitals
return true;
}
if(lower==c.length) {// Full lowercase
return true;
}
if(upper==1 && c[0]<'a') {// title case , This can prove that this capital letter must be the first letter
return true;
}
return false;
}
}
521、 The longest special sequence Ⅰ
Here are two strings a
and b
, Please return In these two strings The longest special sequence . If it doesn't exist , Then return to -1
「 The longest special sequence 」 The definition is as follows : The sequence is The longest subsequence unique to a string ( That is, it cannot be a subsequence of another string ) . character string s
The subsequence of is from s
A string that can be obtained by deleting any number of characters in .
for example ,“abc”
yes “aebdc”
The subsequence , Because you can delete “aebdc”
Use the underscore character in to get “abc”
. “aebdc”
The subsequence of also includes “aebdc”
、 “aeb”
and “”
( An empty string ).
String is used to judge equality equals
class Solution {
public int findLUSlength(String a, String b) {
if(a.equals(b)){
return -1;
}else{
return Math.max(a.length(),b.length());
}
}
}
1189、“ balloon ” Maximum number of
Give you a string text
, You need to use text
To piece together as many words as possible "balloon"( balloon ).
character string text
Each letter in can only be used once at most . Please return the maximum number of words you can piece together "balloon".
Two l Or two o It can only be done once . so if(c=='l'||c=='o'){ min= Math.min(min,map.get(c)/2) ; }
class Solution {
public int maxNumberOfBalloons(String text) {
Map<Character,Integer>map=new HashMap<>();
for(int i=0;i<text.length();i++){
char c=text.charAt(i);
if(map.containsKey(c)){
map.put(c,map.get(c)+1);
}else{
map.put(c,1);
}
}
String str="balloon";
int min=Integer.MAX_VALUE;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (!map.containsKey(c)) {
return 0;
}else{
if(c=='l'||c=='o'){
min= Math.min(min,map.get(c)/2);
}else{
min=Math.min(min,map.get(c));
}
}
}
return min;
}
}
边栏推荐
- After the hot update of uniapp, "mismatched versions may cause application exceptions" causes and Solutions
- TS Basics
- Multi attribute object detection on rare aircraft data sets: experimental process using yolov5
- C - Inheritance - polymorphism - virtual function member (lower)
- js對象獲取屬性的方法(.和[]方式)
- Ble of Jerry [chapter]
- word中如何删除某符号前面或后面所有的文字
- Go learning --- use reflection to judge whether the value is valid
- How MySQL merges data
- 学go之路(二)基本类型及变量、常量
猜你喜欢
QT color is converted to string and uint
Solution to the problem of breakthrough in OWASP juice shop shooting range
杰理之BLE【篇】
Ble of Jerry [chapter]
【MySQL学习笔记32】mvcc
Relevant introduction of clip image
【线上问题处理】因代码造成mysql表死锁的问题,如何杀掉对应的进程
How are the open source Netease cloud music API projects implemented?
Set picture annotation in markdown
[online problem processing] how to kill the corresponding process when the MySQL table deadlock is caused by the code
随机推荐
(4) Web security | penetration testing | network security web site source code and related analysis
Oracle database 11gr2 uses TDE transparent data encryption to report an error ora28353. If you run to close the wallet, you will report an error ora28365. If you run to open the wallet, you will repor
leecode-C语言实现-15. 三数之和------思路待改进版
Ali's redis interview question is too difficult, isn't it? I was pressed on the ground and rubbed
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
ORACLE列转行--某字段按指定分隔符转多行
TypeScript 函数定义
NiO programming introduction
杰理之BLE【篇】
Cookie Technology & session Technology & ServletContext object
Ble of Jerry [chapter]
Raspberry pie 3B update VIM
杰理之BLE【篇】
First knowledge of OpenGL es learning (1)
[online problem processing] how to kill the corresponding process when the MySQL table deadlock is caused by the code
SSM learning
Introduction to the basics of network security
Go learning -- implementing generics based on reflection and empty interfaces
CDN acceleration and cracking anti-theft chain function
How MySQL merges data