Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit213f601

Browse files
number of segments in a string
1 parentab8aefc commit213f601

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
packageeasy;
2+
/**434. Number of Segments in a String My SubmissionsBack To Contest
3+
4+
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters.
5+
6+
Please note that the string does not contain any non-printable characters.
7+
8+
Example:
9+
10+
Input: "Hello, my name is John"
11+
Output: 5*/
12+
publicclassNumberofSegmentsinaString {
13+
14+
publicstaticintcountSegments(Strings) {
15+
if (s ==null ||s.isEmpty())return0;
16+
String[]segments =s.split(" ");
17+
intcount =0;
18+
for (Stringseg :segments){
19+
if (seg.equals(""))continue;
20+
count++;
21+
}
22+
returncount;
23+
}
24+
25+
publicstaticvoidmain(String...args){
26+
Stringtest =", , , , a, eaefa";
27+
System.out.println(countSegments(test));
28+
}
29+
}

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
|438|[Find All Anagrams in a String](https://leetcode.com/problems/find-all-anagrams-in-a-string/)|[Solution](../../blob/master/EASY/src/easy/FindAllAnagramsinaString.java)| O(n)|O(1)| Easy|
1414
|436|[Find Right Interval](https://leetcode.com/problems/find-right-interval/)|[Solution](../../blob/master/MEDIUM/src/medium/FindRightInterval.java) | O(nlogn) |O(n) | Medium| Binary Search
1515
|435|[Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/)|[Solution](../../blob/master/MEDIUM/src/medium/NonOverlappingIntervals.java) | O(nlogn) |O(1) | Medium| Greedy
16+
|434|[Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string/)|[Solution](../../blob/master/EASY/src/easy/NumberofSegmentsinaString.java)| O(n)|O(1)| Easy|
1617
|420|[Strong Password Checker](https://leetcode.com/problems/strong-password-checker/)|[Solution](../../blob/master/HARD/src/hard/StrongPasswordChecker.java)| ?| ?| Hard|
1718
|419|[Battleships in a Board](https://leetcode.com/problems/battleships-in-a-board/)|[Solution](../../blob/master/MEDIUM/src/medium/BattleshipsinaBoard.java) | O(n^2) |O(1) | Medium| DFS
1819
|417|[Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/)|[Solution](../../blob/master/MEDIUM/src/medium/PacificAtlanticWaterFlow.java) | O(m*n*Max(m,n)) |O(m*n) | Medium| DFS

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp