Shawngbk / Leecode

Questions of Leecode
0 stars 0 forks source link

434. Number of Segments in a String #156

Open Shawngbk opened 7 years ago

Shawngbk commented 7 years ago

题目要求计算以‘ ’分割后,有多少元素

public class Solution { public int countSegments(String s) { if(s.length() == 0 || s == null) return 0; int count = 0; for(int i = 0; i < s.length(); i++) { if((i == 0 || s.charAt(i-1) == ' ') && s.charAt(i) != ' ') { count++; } } return count; } }