0
点赞
收藏
分享

微信扫一扫

LeetCode 刷题集锦


目录

1.  1108. IP 地址无效化

2.  1093. 大样本统计

3.  888. 公平的糖果交换

4.  445. 两数相加 II 

1.  1108. IP 地址无效化

题目链接

1108. IP 地址无效化

题目描述

LeetCode 刷题集锦_IP

解题思路

替换. 为 [.] ,  如果使用 Java 最好使用 stringBuilder, 而且可以使用 string 封装好的 replace api 。

时间复杂度:O(n)。

运行结果

LeetCode 刷题集锦_时间复杂度_02

参考代码

代码复制到IDE 或 LeetCode 的编辑框中格式是没问题的。 

class Solution {
public:
    string defangIPaddr(string address) {
        string result;
        int length = address.size();
        for(int i=0; i<length; i++) {
            if(address[i] == '.') {
                result += "[.]";
            } else {
                result += address[i];
            }
        }
        return result;
    }
};

 

2.  1093. 大样本统计

题目链接

1093. 大样本统计

题目描述

LeetCode 刷题集锦_时间复杂度_03

LeetCode 刷题集锦_IP_04

解题思路

有注释。

时间复杂度:O(n)。

运行结果

LeetCode 刷题集锦_时间复杂度_05

参考代码

class Solution {
public:
    vector<double> sampleStats(vector<int>& count) {
        vector<int> countTemp = count;
        int totalNumber = 0; // 样本的数量
        double minn = 255; // 最小值
        double maxx = 0; // 最大值
        double sum = 0; // 总大小
        double midnum = 0; // 中位数
        double zhongshu = 0; // 众数
        double zhongshuNumber = 0; // 众数的数量
        bool flagMin = false; // false 未找到最小值
        
        // 找到最大值,众数,并是count[i]变为前缀和,用来求中位数
        for(int i=0; i<256; i++) {
            sum += countTemp[i]*i;
            totalNumber += countTemp[i];
            if(0 != countTemp[i]) {
                maxx = i;
                if(countTemp[i] > zhongshuNumber) {
                    zhongshu = i;
                    zhongshuNumber = countTemp[i];
                }
                if(false == flagMin) {
                    minn = i;
                    flagMin = true;
                }
            }
            if(0 != i) {
                countTemp[i] += countTemp[i-1];
            }
        }
        
        bool flag = false; // false 为偶数
        int findNumber = totalNumber/2;
        if(1 == totalNumber%2) {
            flag = true;
            findNumber++;
        }
        // 偶数需要找两次
        int times = 2;
        for(int i=0; i<256;i++) {
            if(countTemp[i] >= findNumber) {
                // 处理奇数
                if(true == flag) {
                    midnum = i;
                    break;
                } else { // 处理偶数
                    times --;
                    findNumber ++;
                    midnum += i;
                    if(0 == times) {
                        midnum /= 2;
                        break;
                    }
                    i--;
                }
            } 
        }
        vector<double> result;
        result.push_back(minn);
        result.push_back(maxx);
        result.push_back(sum/totalNumber);
        result.push_back(midnum);
        result.push_back(zhongshu);
        return result;
    }
};

 

3.  888. 公平的糖果交换

题目链接

888. 公平的糖果交换

题目描述

 

 

LeetCode 刷题集锦_i++_06

LeetCode 刷题集锦_时间复杂度_07

解题思路

vis[B] 数组标记 B中出现的数,然后根据遍历数组A中的数,B中存在合适的交换的数,则交换。

时间复杂度:O(n)。

运行结果

LeetCode 刷题集锦_IP_08

参考代码

class Solution {
public:
    vector<int> fairCandySwap(vector<int>& A, vector<int>& B) {
        vector<int> result;

        int MAX_N = 100010;
        bool visB[MAX_N];
        // 初始化
        for(int i=0; i<MAX_N; i++) {
            visB[i] = false;
        }
        
        // MaxTotal 10000*100000
        int totalA = 0, totalB = 0;
        int lengthA = A.size();
        int lengthB = B.size();
        for(int i=0; i<lengthA; i++) {
            totalA += A[i];
        }
        
        for(int i=0; i<lengthB; i++) {
            totalB += B[i];
            visB[B[i]] = true;
        }
        
        int distace = (totalB-totalA)/2;
        for(int i=0; i<lengthA; i++) {
            int temp = A[i]+distace;
            if(temp > 0 && temp < MAX_N) {
                if(visB[temp] == true) {
                    result.push_back(A[i]);
                    result.push_back(temp);
                    break;
                }
            }
        }
        
        return result;
    }
};

 

4.  445. 两数相加 II 

题目链接

445. 两数相加 II

题目描述

LeetCode 刷题集锦_IP_09

 

解题思路

把链表存到动态数组vector里面,然后使用两个vector模拟加法。

时间复杂度:O(n)。

运行结果

LeetCode 刷题集锦_IP_10

参考代码

 

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* result = NULL;
        vector<int> v1, v2;
        v1.clear();
        v2.clear();
        v1.push_back(0);
        v2.push_back(0);
        while(l1 != NULL) {
            v1.push_back(l1->val);
            l1 = l1->next;
        }
        while(l2 != NULL) {
            v2.push_back(l2->val);
            l2 = l2->next;
        }
        
        int len1 = v1.size(), len2 = v2.size();
        int length = min(len1-1, len2-1);
        
        if(len1 >= len2) {
            for(int i=1; i<=length; i++) {
                v1[len1-i] = v1[len1-i]+v2[len2-i];  
                if(v1[len1-i] >= 10) {
                    v1[len1-i] -= 10;
                    v1[len1-i-1] ++;
                }
            }
            for(int i=len1-length-1; i>=1; i--) {
                if(v1[i] >= 10) {
                    v1[i] -= 10;
                    v1[i-1] ++;
                }
            }
            ListNode* temp;
            if(v1[0] != 0) {
                temp = new ListNode(v1[0]);
                result = temp;
            }
            for(int i=1; i<len1; i++) {
                if(NULL == result) {
                    temp = new ListNode(v1[i]);
                    result = temp; 
                } else {
                    temp->next = new ListNode(v1[i]);
                    temp = temp->next;
                }
            }
        } else{
            for(int i=1; i<=length; i++) {
                v2[len2-i] = v1[len1-i]+v2[len2-i];
                if(v2[len2-i] >= 10) {
                    v2[len2-i] -= 10;
                    v2[len2-i-1] ++;
                }
            }
            for(int i=len2-length-1; i>=1; i--) {
                if(v2[i] >= 10) {
                    v2[i] -= 10;
                    v2[i-1] ++;
                }
            }
            ListNode* temp;
            if(v2[0] != 0) {
                temp = new ListNode(v2[0]);
                result = temp;
            }
            for(int i=1; i<len2; i++) {
                if(NULL == result) {
                    temp = new ListNode(v2[i]);
                    result = temp; 
                } else {
                    temp->next = new ListNode(v2[i]);
                    temp = temp->next;
                }
            }
        }
        return result;
    }
};

 

 

举报

相关推荐

C++刷题错误集锦

Leetcode刷题

leetcode刷题

leetcode刷题三

LeetCode刷题-9

Leetcode刷题笔记

0 条评论