LeetCode C++刷题 65-68题题解

65、有效数字

题目:

验证给定的字符串是否可以解释为十进制数字。

例如:

"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true
" -90e3   " => true
" 1e" => false
"e3" => false
" 6e-1" => true
" 99e2.5 " => false
"53.5e93" => true
" --6 " => false
"-+3" => false
"95a54e53" => false

说明: 我们有意将问题陈述地比较模糊。在实现代码之前,你应当事先思考所有可能的情况。这里给出一份可能存在于有效十进制数字中的字符列表:

数字 0-9
指数 - "e"
正/负号 - "+"/"-"
小数点 - "."
当然,在输入中,这些字符的上下文也很重要。

更新于 2015-02-10:
C++函数的形式已经更新了。如果你仍然看见你的函数接收 const char * 类型的参数,请点击重载按钮重置你的代码。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/valid-number
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题解:

先清除前后空格,再判断剩下的字符串中第一位是否是符号位,再根据要求判断剩下的字符串

完整代码:

class Solution {
public:
    bool isNumber(string s) {
        int n = s.size();
        //判断s是否为空字符串
        if(n == 0)
        {
            return false;
        }

        //消除空格
        int start = 0;
        int end = n - 1;
        while(start < n && s[start] == ' ')
        {
            start ++;
        }
        while(end >= 0 && s[end] == ' ')
        {
            end --;
        }

        //如果全是空格,返回false
        if(start > end)
        {
            return false;
        }

        //如果第一个字符为+号或者-号,直接++
        if(s[start] == '+' || s[start] == '-'){
            start ++;
        }

        //处理每一个字符
        string temp = "";
        bool has_point = false;
        bool has_e = false;
        while(start <= end)
        {
            //判断数字
            if(s[start] >= '0' && s[start] <= '9')
            {
                temp += s[start];
            }
            //判断.
            else if(s[start] == '.')
            {
                //当已经有小数点或者已经有或者字符串只有一个小数点时,返回false
                if(has_point || has_e || (temp == "" && start == end))
                {
                    return false;
                }
                has_point = true;
            }
            //判断e
             else if(s[start] == 'e')
            {
                if(has_e || temp == "" || start == end){
                    return false;
                }
                has_e = true;
            }
            //判断正负号
            else if(s[start] == '+' || s[start] == '-')
            {
                //若前面没有e或者正负号后没有数值,则返回
                if(start - 1 < 0 || s[start - 1] != 'e' || start == end){
                    return false;
                }
            }
            //存在其他字符即返回
            else{
                return false;
            }

            start ++;
        }

        return true;
    }
};

66、加一

题目:

给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。

最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。

你可以假设除了整数 0 之外,这个整数不会以零开头。

示例 1:

输入: [1,2,3]
输出: [1,2,4]
解释: 输入数组表示数字 123。
示例 2:

输入: [4,3,2,1]
输出: [4,3,2,2]
解释: 输入数组表示数字 4321。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/plus-one
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题解:

判断是否有进位

完整代码:

class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        vector<int> result;
        int n = digits.size();
        int temp =  (digits[n - 1] + 1 - 10) >= 0 ? (digits[n - 1] + 1 - 10) : (digits[n - 1] + 1);
        int num = (digits[n - 1] + 1 - 10) >= 0 ? 1: 0;
        digits[n - 1] = temp;
        for(int i =  n - 2; i >= 0; i --)
        {
            int temp =  (digits[i] + num - 10) >= 0 ? (digits[i] + num - 10) : (digits[i] + num );
            num = (digits[i] + num - 10) >= 0 ? 1: 0;
            digits[i] = temp;
        }
       
        if(num > 0)
        {
            result.push_back(num);
            result.insert(result.end(), digits.begin(), digits.end());
        }
        else{
            result.insert(result.begin(), digits.begin(), digits.end());
        }

        
        return result;
    }
};

67、二进制求和

题目:

给你两个二进制字符串,返回它们的和(用二进制表示)。

输入为 非空 字符串且只包含数字 1 和 0。

 

示例 1:

输入: a = "11", b = "1"
输出: "100"
示例 2:

输入: a = "1010", b = "1011"
输出: "10101"
 

提示:

每个字符串仅由字符 '0' 或 '1' 组成。
1 <= a.length, b.length <= 10^4
字符串如果不是 "0" ,就都不含前导零。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/add-binary
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题解:

根据二进制相加的规则书写代码

完整代码:

class Solution {
public:
    string addBinary(string a, string b) {
        int i = a.size() - 1;
        int j = b.size() - 1;
        bool is_add_one = false;
        string result = "";
        while(i >= 0 && j >= 0)
        {
            if(is_add_one){
                int temp = (a[i] - '0') + (b[j] - '0') + 1;
                if(temp == 3){
                    result = '1' + result;
                    is_add_one = true;
                }else if(temp == 2){
                    is_add_one = true;
                    result = '0' + result;
                }else if(temp == 1){
                    result = '1' + result;
                    is_add_one = false;
                }else{
                    result = '0' + result;
                    is_add_one = false;
                }
            }else{
                int temp = (a[i] - '0') + (b[j] - '0');
                if(temp == 2){
                    result = '0' + result;
                    is_add_one = true;
                }else if(temp == 1){
                    result = '1' + result;
                    is_add_one = false;
                }else{
                    result = '0' + result;
                    is_add_one = false;
                }
            }
            i --;
            j --;
        }

        while(i >= 0){
            if(is_add_one){
                int temp = (a[i] - '0') + 1;
                if(temp == 2){
                    is_add_one = true;
                    result = '0' + result;
                }else if(temp == 1){
                    result = '1' + result;
                    is_add_one = false;
                }else{
                    result = '0' + result;
                    is_add_one = false;
                }
            }else{
                result = a[i] + result;
                is_add_one = false;
            }
            i --;
        }

        while(j >= 0){
            if(is_add_one){
                int temp =  (b[j] - '0') + 1;
                if(temp == 2){
                    is_add_one = true;
                    result = '0' + result;
                }else if(temp == 1){
                    result = '1' + result;
                    is_add_one = false;
                }else{
                    result = '0' + result;
                    is_add_one = false;
                }
            }else{
                result =  b[j] + result;
            }
            j --;
        }
        if(is_add_one){
            result = '1' + result;
        }
        return result;
    }
};

68、文本左右对齐

题目:

给定一个单词数组和一个长度 maxWidth,重新排版单词,使其成为每行恰好有 maxWidth 个字符,且左右两端对齐的文本。

你应该使用“贪心算法”来放置给定的单词;也就是说,尽可能多地往每行中放置单词。必要时可用空格 ' ' 填充,使得每行恰好有 maxWidth 个字符。

要求尽可能均匀分配单词间的空格数量。如果某一行单词间的空格不能均匀分配,则左侧放置的空格数要多于右侧的空格数。

文本的最后一行应为左对齐,且单词之间不插入额外的空格。

说明:

单词是指由非空格字符组成的字符序列。
每个单词的长度大于 0,小于等于 maxWidth。
输入单词数组 words 至少包含一个单词。
示例:

输入:
words = ["This", "is", "an", "example", "of", "text", "justification."]
maxWidth = 16
输出:
[
   "This    is    an",
   "example  of text",
   "justification.  "
]
示例 2:

输入:
words = ["What","must","be","acknowledgment","shall","be"]
maxWidth = 16
输出:
[
  "What   must   be",
  "acknowledgment  ",
  "shall be        "
]
解释: 注意最后一行的格式应为 "shall be    " 而不是 "shall     be",
     因为最后一行应为左对齐,而不是左右两端对齐。       
     第二行同样为左对齐,这是因为这行只包含一个单词。
示例 3:

输入:
words = ["Science","is","what","we","understand","well","enough","to","explain",
         "to","a","computer.","Art","is","everything","else","we","do"]
maxWidth = 20
输出:
[
  "Science  is  what we",
  "understand      well",
  "enough to explain to",
  "a  computer.  Art is",
  "everything  else  we",
  "do                  "
]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/text-justification
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题解:

难度上倒是还好,主要很烦

完整代码:

class Solution {
public:
    vector<string> fullJustify(vector<string>& words, int maxWidth) 
    {
        vector<string> result;
        int width = 0;
        int word_count = 0;
        string temp = "";
        for(int i = 0; i < words.size(); i++) {
            if(width == 0) {
                width = words[i].size();
                temp = words[i];
                word_count = 1;
            } else {
                if(words[i].size() + 1 + width <= maxWidth) {
                    temp += " " + words[i];
                    width += 1 + words[i].size();
                    word_count += 1;
                } else {
                    // 处理字符串
                    process(temp, width, word_count, maxWidth);
                    result.push_back(temp);
                    width = words[i].size();
                    temp = words[i];
                    word_count = 1;
                }
            }
        }
        // 在最后一个字符串后填充0
        if(width <= maxWidth) {
            temp.insert(width, maxWidth - width, ' ');
            result.push_back(temp);
        }
        return result;
    }

    void process(string &temp, int width, int word_count, int maxWidth)
    {
        if(word_count == 1) {
            temp.insert(width, maxWidth - width, ' ');
            return;
        }

        int total = maxWidth - width;
        int pos = word_count - 1;
        int n = total / pos;
        for(int i = temp.size() - 1; i >= 0; i--) {
            if(temp[i] == ' ') {
                temp.insert(i, n, ' ');
                pos -= 1;
                total -= n;
                if(pos > 0 && total % pos == 0) {
                    n = total / pos;
                }                
            }
        }
    }
};

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cchangcs

谢谢你的支持~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值