Leetcode first brush _ Length of Last Word

Source: Internet
Author: User

I don't quite understand the intention of the subject. In fact, it is too simple to use java for this question. Use split to process it and get an array of all words, then the length of the last one is enough. I personally think that the most successful function in java is split, which is almost always used in engineering and convenient and powerful.

C ++ is a little more complicated, but this is also the simplest string problem. The function interface determines that the length of the string is unknown. You need to find the character that is not equal to space in a loop, and then find the last word from the tail header, then accumulate until the space ends.

Class Solution {public: int lengthOfLastWord (const char * s) {if (s = NULL) return 0; int len = 0, res = 0; while (s [len]! = '\ 0') len ++; len --; while (s [len] = '') len --; while (len> = 0 & s [len]! = '') {Len --; res ++;} return res ;}};


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.