剑指offer打卡 week5

59. 把数字翻译成字符串

https://www.acwing.com/problem/content/55/
使用动态规划:
f(n)为前i个数字翻译数:
首先可以有 f(n)=f(n-1)
如果前两个数字可以组成一个新翻译 就还有f(n)=f(n-1)+f(n-2)

class Solution {
public:
    int getTranslationCount(string s) {
        int n = s.size();
        vector<int> f(n+1);
        f[0]=1;
        for(int i=1;i<=n;i++){
            f[i] = f[i-1];
            if(i>1){
                int t = s[i-1] - '0' + (s[i-2] - '0') * 10;
                if(t >= 10 && t <= 25){
                    f[i] += f[i-2];
                }
            }
        }
        return f[n];
    }
};

60. 礼物的最大价值

https://www.acwing.com/problem/content/56/
经典DP问题

class Solution {
public:
    int getMaxValue(vector<vector<int>>& grid) {
        int n = grid.size();
        int m = grid[0].size();
        vector<vector<int>> f(n + 1, vector<int>(m+1));
        for(int i = 1; i <= n; i ++){
            for(int j = 1; j <= m; j ++){
                f[i][j] = max(f[i-1][j],f[i][j-1]) + grid[i-1][j-1];
            }
        }
        return f[n][m];
    }
};

61. 最长不含重复字符的子字符串

https://www.acwing.com/problem/content/57/

class Solution {
public:
    unordered_map<char,int>m;
    int longestSubstringWithoutDuplication(string s) {
        int n = s.size();
        int ans = 0;
        for(int i = 0,j = 0; j < n; j++){
            if(++m[s[j]] > 1){
                while(m[s[j]] > 1){
                    m[s[i++]]--;
                }
            }
            ans = max(ans,j - i + 1);
        }
        return ans;
    }
};

62. 丑数

https://www.acwing.com/problem/content/58/
从1开始 不断乘2 3 5 生成数
并通过优先队列得到当前生成的最小数

class Solution {
public:
    int getUglyNumber(int n) {
        priority_queue<int,vector<int>,greater<int> >q;
        long long res = 1;
        for(int i = 1; i < n; i++){
            q.push(res * 2);
            q.push(res * 3);
            q.push(res * 5);
            res = q.top();
            q.pop();
            while(!q.empty() && q.top() == res){
                q.pop();
            }
        }
        return res;
    }
};

63. 字符串中第一个只出现一次的字符

https://www.acwing.com/problem/content/59/

class Solution {
public:
    char firstNotRepeatingChar(string s) {
        unordered_map<char,int> m;
        for(int i = 0; i < s.size(); i++){
            m[s[i]] ++;
        }
        char ans = '#';
        for(int i = 0; i < s.size(); i++){
            if(m[s[i]] == 1){
                ans = s[i];
                return ans;
            }
        }
        return ans;
    }
};

66. 两个链表的第一个公共结点

https://www.acwing.com/problem/content/62/
思路:

class Solution {
public:
    ListNode *findFirstCommonNode(ListNode *headA, ListNode *headB) {
        auto p = headA, q = headB;
        while(p != q){
            if(p) p = p->next;
            else p = headB;
            if(q) q = q->next;
            else q = headA;
        }
        return q;
    }
};

67. 数字在排序数组中出现的次数

https://www.acwing.com/problem/content/63/
简单的二分使用

class Solution {
public:
    int getNumberOfK(vector<int>& nums , int k) {
        int l = 0,r = nums.size();
        if(r == 0){
            return 0;
        }
        int ans = 0;
        //二分查找第一个大等于k的位置
        //分为 (l,mid) (mid+1,r)
        while(l < r){
            int mid = (l + r) >> 1;
            if(nums[mid] < k){
                l = mid + 1;
            }
            else {
                r = mid;
            }
        }
        if(nums[l] != k){
            return 0;
        }
        else {
            for(int i = l;i < nums.size();i++){
                if(nums[i] == k){
                    ans++;
                }
                else {
                    break;
                }
            }
        }
        return ans;
    }
};
posted @ 2021-10-10 20:03  offlineboy  阅读(29)  评论(0)    收藏  举报