当前位置:网站首页>Leetcode- number of words in string - simple
Leetcode- number of words in string - simple
2022-06-13 05:49:00 【AnWenRen】
title :434 Number of words in string - Simple
subject
Count the number of words in the string , The words here refer to consecutive characters that are not spaces .
Please note that , You can assume that the string does not contain any non printable characters .
Example
Input : "Hello, my name is John"
Output : 5
explain : The word here refers to consecutive characters that are not spaces , therefore "Hello," Count as 1 Word .
Code Java
// Division method Not tested
public int countSegments(String s) {
String[] strings = s.split(" ");
return strings.length;
}
public int countSegments1(String s) {
int count = 0;
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i - 1) == ' ' && s.charAt(i) != ' ') {
count ++;
}
}
if (s.charAt(0) == ' ')
return count;
return count + 1;
}
边栏推荐
猜你喜欢

Feel the power of shardingsphere JDBC through the demo

Information collection for network security (2)

Building a stand-alone version of Nacos series

OpenGL馬賽克(八)

Concurrent programming -- source code analysis of thread pool

Vagrant virtual machine installation, disk expansion and LAN access tutorial

17 servicetask of flowable task

Hainan University Postgraduate Entrance Examination electronic information (085400) landing experience

Mysql database backup and restore:

Database design
随机推荐
顶部下滑沉浸式dialog
Three paradigms of MySQL
The 13th week of the second semester of sophomore year
Byte buddy print execution time and method link tracking
18 flowable task manualtask and receivetask
Mobile end adaptation scheme
Tongweb adapts to openrasp
Bicolor case
Sentinel series introduction to service flow restriction
Basic operations of MySQL auto correlation query
9. Errorstartevent and errorboundaryevent of error events
2021.9.30学习日志-postman
Leetcode- intersection of two arrays ii- simple
Top slide immersive dialog
10 signalstartevent and signalcatchingevent of flowable signal events
Concurrent programming -- source code analysis of thread pool
Pychart encountered time zone problem when connecting to MySQL timezone
MySQL fuzzy query and sorting by matching degree
Integration of sentinel series Nacos to realize rule synchronization and persistence
Tongweb crawl performance log script