LeetCode 434. Number of Segments in a String 题解——Java
来源:互联网 发布:51单片机40引脚是 引脚 编辑:程序博客网 时间:2024/06/05 11:18
题目链接:https://leetcode.com/problems/number-of-segments-in-a-string/#/description
题目要求:以空格为分隔符,计算字符串的段数。
分析:此题初看简单,但是有些细节却需要注意
(1) 对于“”的输入情况,需要单独处理一下,因为split方法会返回1而不是正确的结果0。
(2)可能有多个空格相连的情况,比如输入“, , , , a, eaefa”
Java代码如下:
public class Solution { public int countSegments(String s) { if(s==null){ return 0; } s = s.trim(); if(s.equals("")){ return 0; } // “ +” :正则表达式,表示空格出现一次或多次 String[] result = s.split(" +"); return result.length; }}
0 0
- LeetCode 434. Number of Segments in a String 题解——Java
- Leetcode Number of Segments in a String
- LEETCODE--Number of Segments in a String
- leetcode oj java Number of Segments in a String
- LeetCode 434. Number of Segments in a String
- LeetCode 434. Number of Segments in a String
- 【Leetcode 434. Number of Segments in a String】
- leetcode 434. Number of Segments in a String
- LeetCode 434. Number of Segments in a String
- LeetCode 434. Number of Segments in a String
- Leetcode 434. Number of Segments in a String (Easy) (cpp)
- 【LeetCode】 434. Number of Segments in a String
- [leetcode]434. Number of Segments in a String
- [leetcode]434. Number of Segments in a String
- LeetCode 434. Number of Segments in a String
- Leetcode 434. Number of Segments in a String
- Leetcode-434. Number of Segments in a String
- LeetCode-434. Number of Segments in a String
- iOS 根类NSObject
- PAT 1116
- Java中抽象类和接口的区别及使用场景
- 算法提高 种树
- 详解JS位置、宽高属性之一:offset系列
- LeetCode 434. Number of Segments in a String 题解——Java
- UVA 1669 && HDU 4118 Holiday's Accommodatio (思路题目--统计子树结点 )
- hdu 1208 Pascal's Travels 记忆化搜索
- C语言学习笔记(一)
- class, 类,方法,python
- 第三方登录与分享
- 使用FastJSON,将对象或数组和JSON串互转
- Java打印日历
- 兰顿蚂蚁