[日常刷题]LeetCode第八天

58. 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 characters only.
Example:

Input: "Hello World"
Output: 5

Solution in C++:

  • **关键点:**只有空串才没有最后一个单词,最后一个单词后面的空格不算没有最后一个单词。
  • **思路:**空串没有最后一个单词,return 0;其他的从字符串最后开始扫描,扫描到第一个不为空格的单词到下一个空格之间的长度就为最后一个单词的长度。
int lengthOfLastWord(string s) {
        size_t size = s.size();
        // 空串
        if (size == 0)
            return 0;
        
        int count = 0;
        int i = size - 1;
        while(s[i] == ' ')
            --i;
        for(; s[i] != ' ' && i >= 0; --i)
            ++count;
        
        return count;
    }

66. Plus One

Given a non-empty array of digits representing a non-negative integer, plus one to the integer.

The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.

You may assume the integer does not contain any leading zero, except the number 0 itself.
Example 1:

Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.

Example 2:

Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.

Solution in C++:

  • 关键点:进位记忆及最高位处理。
  • 思路:只有最低位需要+1,其他位都是根据进位来判断是否需要加,这里没有进行进位为0不需要进行加法操作的优化,图代码简单直接都进行加法处理。每次讲得到的数不用再反转。
    vector<int> plusOne(vector<int>& digits) {
        vector<int> result;
        size_t size = digits.size();
        if ( size == 0){
            result.push_back(1);
            return result;
        }
        size_t carry = 0;
        int tmp;
        for (int i = size - 1; i >= 0; --i){
            if (i == size - 1)
                tmp = digits[i] + 1;
            else
                tmp = digits[i] + carry;
            if(tmp >= 10){
                carry = 1;
                result.insert(result.begin(), tmp % 10);
            } else{
                carry = 0;
                result.insert(result.begin(), tmp);
            }
        }
        if (carry == 1)
            result.insert(result.begin(), 1);
        return result;               
    }

67. Add Binary

Given two binary strings, return their sum (also a binary string).

The input strings are both non-empty and contains only characters 1or0.
Example 1:

Input: a = "11", b = "1"
Output: "100"

Example 2:

Input: a = "1010", b = "1011"
Output: "10101"

Solution in C++:

  • 关键点:两个字符串不同位数的处理&&进位最高位的处理。
  • 思路:和上一题的思路差不多就是具体的加法操作不同,从尾部开始处理存入字符串,然后将字符串反转即可得到最终的字符串。
string addBinary(string a, string b) {
        string s = "";
        string table = "01";
        size_t sizea = a.size(), sizeb = b.size();
        // 相加
        size_t carry = 0;
        int i, j;
        for (i = sizea - 1, j = sizeb - 1; i >= 0 && j >= 0; --i, --j){
            int tmp = a[i] - '0' + b[j] - '0' + carry;
            if ( tmp >= 2){
                carry = 1;
                s += table[tmp % 2];
            } else{
                carry = 0;
                s += table[tmp];
            }
        }
        int tmp = 0;
        if (i >= 0){ // i的位数多于j的位数
            if (carry == 0){
                while(i >= 0){
                    s += a[i];
                    --i;
                }
            } else{
                 while(i >= 0){
                    int tmp = a[i] - '0' + carry;
                    if ( tmp >= 2){
                        carry = 1;
                        s += table[tmp % 2];
                    } else{
                        carry = 0;
                        s += table[tmp];
                    }
                     --i;
                }
            }
        } else if (j >= 0){ // j的位数多于i的位数
            if (carry == 0){
                while(j >= 0){
                    s += b[j];
                    --j;
                }
            } else{
                 while(j >= 0){
                    int tmp = b[j] - '0' + carry;
                    if ( tmp >= 2){
                        carry = 1;
                        s += table[tmp % 2];
                    } else{
                        carry = 0;
                        s += table[tmp];
                    }
                     --j;
                }
            }
        } else{ // i和j位数相同
            tmp = carry;
        }
        if ( carry == 1)
            s += table[1];

        // 反转
        size_t sizes = s.size();
        for (int i = 0; i < sizes / 2; ++i){
            char tmp = s[i];
            s[i] = s[sizes-i-1];
            s[sizes-i-1] = tmp;
        }
        return s;
    }

小结

今天时间比较早,而且题目比较简单,就做了三题,感觉基本没什么难度,可能也是因为自己没有做什么优化。技能上可能没什么大的收获,但是确定了以后刷题得以时间为单位去定,而不是以题目去定了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python 是一种流行的高级编程语言,因其简洁易读的语法和广泛的应用领域而受到开发者喜爱。LeetCode 是一个在线编程平台,专门用于算法和技术面试的准备,提供了大量的编程题目,包括数据结构、算法、系统设计等,常用于提升程序员的编程能力和解决实际问题的能力。 在 Python 中刷 LeetCode 题目通常涉及以下步骤: 1. **注册账户**:首先在 LeetCode 官网 (https://leetcode.com/) 注册一个账号,这样你可以跟踪你的进度和提交的代码。 2. **选择语言**:登录后,在个人主页设置中选择 Python 作为主要编程语言。 3. **学习和理解题目**:阅读题目描述,确保你理解问题的要求和预期输出。题目通常附有输入示例和预期输出,可以帮助你初始化思考。 4. **编写代码**:使用 Python 编写解决方案,LeetCode 提供了一个在线编辑器,支持实时预览和运行结果。 5. **测试和调试**:使用给出的测试用例来测试你的代码,确保它能够正确地处理各种边界条件和特殊情况。 6. **提交答案**:当代码完成后,点击 "Submit" 提交你的解法。LeetCode 会自动运行所有测试用例并显示结果。 7. **学习他人的代码**:如果遇到困难,可以查看社区中的其他优秀解法,学习他人的思路和技术。 8. **反复练习**:刷题不是一次性的事情,通过反复练习和优化,逐渐提高解题速度和代码质量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值