题解 | #字符串最后一个单词的长度#

字符串最后一个单词的长度

https://www.nowcoder.com/practice/8c949ea5f36f422594b306a2300315da

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void async function () {
    // Write your code here
    while(line = await readline()){
        console.log(line.length - 1 - line.lastIndexOf(" "))//空格最后一次出现的index
    }
}()

我的方法

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
 
void (async function () {
    // Write your code here
    while ((line = await readline())) {
        var strArr = line.trim().split(' ')//.trim()去掉两边的" "
        console.log(strArr[strArr.length-1].length)
    }
})();

ycljy——的方法

const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
 
void async function () {
    // Write your code here
    while(line = await readline()){
        let tokens = line.split(' ');
        let lastNum=tokens.pop().length//pop() 方法用于删除数组的最后一个元素并返回删除的元素。
        console.log(lastNum) 
    }
}()

linecl——的方法

练练练练练 文章被收录于专栏

练练练练练

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务