LeetCode Length of Last Word 最后一个字的长度

 1 class Solution {
 2 public:
 3     int lengthOfLastWord(const char *s) {
 4         if(s=="")    return 0;
 5         string snew=s;
 6         int n=0,len=strlen(s);
 7         char *p=&snew[len-1];
 8         while(*p==‘ ‘&&len!=0){
 9             *p--;
10             len--;
11         }
12         while(*p!=‘ ‘&&len!=0){
13             n++;
14             p--;
15             len--;
16         }
17         return n;
18     }
19 };

题意:给一个数组,以空格来判断是否字的结束与开始。一个词的前后都是空格,中间无空格。返回最后一个字的长度。

思路:给的是一个不可修改的字符串,为了方便,创建另外的指针。若最后是空格,先将后面的空格过滤,从后开始寻找第一个不是空格的字符,开始计数,继续往前数,直到有空格或者已经扫完字符串为止。

吐槽:感觉snew可以省略的,直接利用s就行。但是语法忘了~

时间: 2024-10-11 21:50:55

LeetCode Length of Last Word 最后一个字的长度的相关文章

LeetCode——Length of Last Word

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space cha

LeetCode: Length of Last Word [057]

昨天同事在做主从时,从库报如下错误: Got fatal error 1236 from master when reading data from binary log: 'Misconfigured master - server id was not set' 粗粗看好像是master的server-id没有设置,但同事做如下查询: 备库采集: [email protected] Fri May 23 14:18:59 2014 14:18:59 [(none)]> show variab

LeetCode Length of Last Word

1. 题目 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-spa

[LeetCode] Length of Last Word 字符串查找

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space cha

leetcode : Length of Last Word [基本功]

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "Helvetica Neue"; color: #333333 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "Helvetica Neue"; color: #333333; background-color: #ffffff } p.p3 { margin: 0.0px 0.0px 0.0px

[Leetcode] Length of last word 最后一个单词的长度

Given a string s consists of upper/lower-case alphabets and empty space characters' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space char

Leetcode: Length of Last Word in python

Length of Last Word Total Accepted: 47690 Total Submissions: 168587 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note:

leetCode 58.Length of Last Word (最后单词的长度) 解题思路和方法

Length of Last Word Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consi

leetcode Length of Last Word(easy) /java

坑主要在处理空格.因为字符串之间不止有一个空格. import java.io.*; import java.util.*; public class Solution { public static int lengthOfLastWord(String s) { int r=0; int len=s.length(); if(len==0) return 0; int i,j; char[] c=s.toCharArray(); int flag=1; for(i=0;i<len;i++)