Leecode刷题之旅-C语言/python-434 字符串中的单词数

/*
 * @lc app=leetcode.cn id=434 lang=c
 *
 * [434] 字符串中的单词数
 *
 * https://leetcode-cn.com/problems/number-of-segments-in-a-string/description/
 *
 * algorithms
 * Easy (29.13%)
 * Total Accepted:    4.2K
 * Total Submissions: 14.2K
 * Testcase Example:  ‘"Hello, my name is John"‘
 *
 * 统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符。
 *
 * 请注意,你可以假定字符串里不包括任何不可打印的字符。
 *
 * 示例:
 *
 * 输入: "Hello, my name is John"
 * 输出: 5
 *
 *
 */
int countSegments(char* s) {
    if(s==NULL||strlen(s)==0) return 0;
    int i=0,j,count=0;
    while(i<strlen(s)){
        while(i<strlen(s)&&s[i]==‘ ‘) i++;//i每次移到第一个非空格处
        j = i;
        while(j<strlen(s)&&s[j]!=‘ ‘)j++;//j每次移到第一个空格处,i和j之间就是一个单词
        if(i<j){
            i=j;
            count++;
        }
        else
        {
            return count;//当最后一个单词后面有空格时,从此出口结束。
        }
    }
    return count;//当最后一个单词后面没有空格时,从此出口结束。
}

思路是 双指针,i找到第一个非空格字符,j从i的位置开始找到第一个为空格的字符,i<j,那么ij之间的就是一个单词,计数就加一,然后i移动到j的位置进行下一次寻找。

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

python:

#
# @lc app=leetcode.cn id=434 lang=python3
#
# [434] 字符串中的单词数
#
# https://leetcode-cn.com/problems/number-of-segments-in-a-string/description/
#
# algorithms
# Easy (29.13%)
# Total Accepted:    4.2K
# Total Submissions: 14.2K
# Testcase Example:  ‘"Hello, my name is John"‘
#
# 统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符。
#
# 请注意,你可以假定字符串里不包括任何不可打印的字符。
#
# 示例:
#
# 输入: "Hello, my name is John"
# 输出: 5
#
#
#
class Solution:
    def countSegments(self, s: str) -> int:
        return len(s.split())
        

python就很简单了。一个split搞定。

原文地址:https://www.cnblogs.com/lixiaoyao123/p/10564208.html

时间: 2024-07-29 11:21:17

Leecode刷题之旅-C语言/python-434 字符串中的单词数的相关文章

Leecode刷题之旅-C语言/python-26.删除数组中的重复项

/* * @lc app=leetcode.cn id=26 lang=c * * [26] 删除排序数组中的重复项 * * https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/description/ * * algorithms * Easy (42.77%) * Total Accepted: 89.1K * Total Submissions: 208.1K * Testcase Example: '[

Leecode刷题之旅-C语言/python-28.实现strstr()

/* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/implement-strstr/description/ * * algorithms * Easy (37.86%) * Total Accepted: 38.6K * Total Submissions: 102K * Testcase Example: '"hello"\n"ll&

Leecode刷题之旅-C语言/python-26.移除元素

/* * @lc app=leetcode.cn id=27 lang=c * * [27] 移除元素 * * https://leetcode-cn.com/problems/remove-element/description/ * * algorithms * Easy (53.46%) * Total Accepted: 39.5K * Total Submissions: 73.7K * Testcase Example: '[3,2,2,3]\n3' * * 给定一个数组 nums 

Leecode刷题之旅-C语言/python-100相同的树

/* * @lc app=leetcode.cn id=100 lang=c * * [100] 相同的树 * * https://leetcode-cn.com/problems/same-tree/description/ * * algorithms * Easy (51.47%) * Total Accepted: 16K * Total Submissions: 31K * Testcase Example: '[1,2,3]\n[1,2,3]' * * 给定两个二叉树,编写一个函数来

Leecode刷题之旅-C语言/python-121买卖股票的最佳时机

/* * @lc app=leetcode.cn id=121 lang=c * * [121] 买卖股票的最佳时机 * * https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/description/ * * algorithms * Easy (48.50%) * Total Accepted: 32.5K * Total Submissions: 66.9K * Testcase Example: '[7,1,5

Leecode刷题之旅-C语言/python-118杨辉三角

/* * @lc app=leetcode.cn id=118 lang=c * * [118] 杨辉三角 * * https://leetcode-cn.com/problems/pascals-triangle/description/ * * algorithms * Easy (60.22%) * Total Accepted: 17.6K * Total Submissions: 29.2K * Testcase Example: '5' * * 给定一个非负整数 numRows,生成

Leecode刷题之旅-C语言/python-141环形链表

/* * @lc app=leetcode.cn id=141 lang=c * * [141] 环形链表 * * https://leetcode-cn.com/problems/linked-list-cycle/description/ * * algorithms * Easy (35.53%) * Total Accepted: 28.4K * Total Submissions: 79.4K * Testcase Example: '[3,2,0,-4]\n1' * * 给定一个链表

Leecode刷题之旅-C语言/python-349两个数组的交集

/* * @lc app=leetcode.cn id=349 lang=c * * [349] 两个数组的交集 * * https://leetcode-cn.com/problems/intersection-of-two-arrays/description/ * * algorithms * Easy (60.49%) * Total Accepted: 15.1K * Total Submissions: 25K * Testcase Example: '[1,2,2,1]\n[2,2

leecode刷题(26)-- 用栈实现队列

leecode刷题(26)-- 用栈实现队列 用栈实现队列 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列首部的元素. empty() -- 返回队列是否为空. 示例: MyQueue queue = new MyQueue(); queue.push(1); queue.push(2); queue.peek(); // 返回 1 queue.pop(); // 返回 1 queue.empty