leetcode-15-basic-string

58. Length of Last Word

解题思路:

从结尾向前搜索,空格之前的就是最后一个词了。写的时候我考虑了尾部有空格的情况。需要注意的是,测试用例中有" "的情况,此时应返回0。

int lengthOfLastWord(string s) {
        if (s.length() == 0 || s.length() == 1 && s[0] == ‘ ‘)
            return 0;
        if (s.length() == 1 && s[0] != ‘ ‘)
            return 1;
        int i;
        int count = 0;
        bool flag = false;
        for (i = s.length() - 1; i >= 0; i--) {
            if (s[i] == ‘ ‘) {
                if (flag == false)
                    continue;
                else
                    break;
            }
            if (s[i] != ‘ ‘) {
                if (flag == false)
                    flag = true;
                count++;
            }
        }
        return count;
    }  


leetcode-15-basic-string

时间: 2024-08-19 10:41:18

leetcode-15-basic-string的相关文章

[LeetCode]15. Pascal's Triangle杨辉三角

Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 杨辉三角主要有下列五条性质: 杨辉三角以正整数构成,数字左右对称,每行由1开始逐渐变大,然后变小,回到1. 第行的数字个数为个. 第行的第个数字为组合数. 第行数字和为. 除每行最左侧与最右侧的数字以

【leetcode系列】String to Integer (atoi)

这个我就直接上代码了,最开始把"abc123"也算作合法的了,后来查了一下atoi的定义,把这种去掉了. public class Solution { public static int atoi(String inStr) { long result = 0L; /* * 网上查了一下,atoi函数的定义是如果第一个非空格字符存在,是数字或者正负号则开始做类型转换, * 之后检测到非数字(包括结束符\0)字符时停止转换,返回整型数.否则,返回零.可能的输入情况有: 1.空字符串 *

[LeetCode][JavaScript]Basic Calculator

Basic Calculator Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces . You may assume that the giv

[LeetCode] NO. 8 String to Integer (atoi)

[题目] Implement atoi to convert a string to an integer. [题目解析] 该题目比较常见,从LeetCode上看代码通过率却只有13.7%,于是编码提交,反复修改了三四次才完全通过.该题目主要需要考虑各种测试用例的情况,比如"+5"."   67"."   +0078"."  12a56--".int的最大值最小值溢出等情况,通过这个题目,联系到实际项目中,我们一定要多考虑边界

LeetCode Design Compressed String Iterator

原题链接在这里:https://leetcode.com/problems/design-compressed-string-iterator/description/ 题目: Design and implement a data structure for a compressed string iterator. It should support the following operations: next and hasNext. The given compressed string

[LeetCode]224. Basic Calculator(模拟,栈)

题目链接:https://leetcode.com/problems/basic-calculator/#/description 题意:计算一个只含有括号.加减.非负数的表达式. 用一个栈记数,一个栈记符号. 1 class Solution { 2 public: 3 int calculate(string s) { 4 int n = s.length(); 5 stack<int> st; 6 stack<char> op; 7 int ret = 0; 8 op.pus

【Leetcode】Basic Calculator II

题目链接:https://leetcode.com/problems/basic-calculator-ii/ 题目: Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division s

【Leetcode】Basic Calculator

题目链接:https://leetcode.com/problems/basic-calculator/ 题目: Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and em

【LeetCode】字符串 string(共112题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman

Leetcode 772. Basic Calculator III

Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces . The expression string contains only non-nega