博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Length of Last Word --最后单词长度---如何优雅的写代码(重重)
阅读量:4107 次
发布时间:2019-05-25

本文共 575 字,大约阅读时间需要 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-space characters only.

For example, 
Given s = "Hello World",
return 5.

解答:这道题主要是想记录下来 如何优雅的写代码!!!!!!!!!!!如何处理好边界值!!!!!!

参考别人的代码:

写代码之前一定要想好思路

代码:

class Solution {public:    int lengthOfLastWord(const char *s) {	int n = strlen(s) - 1;	while(n>=0 && s[n] == ' ')		n--;	int  i = 0;	for(; n>=0 && s[n] != ' '; n--, i++);	return i;	}};

转载地址:http://qktsi.baihongyu.com/

你可能感兴趣的文章
STM32的两个.bin文件如何合并?
查看>>
合并BIN文件的两种方法
查看>>
网线中哪几根真正有用?
查看>>
STM32的SPI时钟
查看>>
UltraEdit 操作小技巧--一次性修改多列
查看>>
IAR的有用的快捷键
查看>>
FTP工具取消被动模式的办法,cuteftp/flashfxp 被动模式!
查看>>
关于pasv模式中,数据端口由谁指定
查看>>
norflash芯片内执行(XIP)
查看>>
STM32 中断向量表的位置 、重定向
查看>>
STM32 启动解析,启动代码,__main main
查看>>
STM32 keil mdk启动代码发分析
查看>>
stm32 库文件_line 函数
查看>>
深入理解SP、LR和PC
查看>>
stm32应用部分发生中断,PC跳转到IAP中断向量表处后,如何跳转到应用部分中断函数入口的
查看>>
选择ARM7还是cortex-M3?
查看>>
EMC与EMI的区别
查看>>
STM32的串口采用DMA方式接收数据测试
查看>>
STM32的串口采用DMA方式发送数据测试
查看>>
msps
查看>>