LeetCode刷算法题-简单难度(九)题号801-900

804.唯一摩斯密码词

class Solution {
     
public:
    int uniqueMorseRepresentations(vector<string>& words) {
     
        vector<string> table{
     ".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
        set<string> tmp;
        for(auto s : words){
     
            string trans;
            for(auto c : s){
     
                trans += table[c-'a'];
            }
            tmp.insert(trans);
        }
        return tmp.size();
    }
};

806.写字符串需要的行数

class Solution {
     
public:
    vector<int> numberOfLines(vector<int>& widths, string S) {
     
        int lines = 1, w = 0;
        for(auto c : S){
     
            w += widths[c-'a'];
            if(w > 100){
     
                lines++;
                w = widths[c-'a'];
            }
        }
        vector<int> res{
     lines,w};
        return res;
    }
};

811.子域名访问计数

class Solution {
     
public:
    vector<string> subdomainVisits(vector<string>& cpdomains) {
     
        vector<string> res;
        int n = cpdomains.size();
        map<string, int> tmp;
        int idx0,idx1,idx2;
        for(int i = 0; i < n; ++i){
     
            int m = cpdomains[i].size();
            int count = 0;
            for(int j = 0; j < m; ++j){
     
                if(' ' == cpdomains[i][j]){
     
                    idx0 = j;
                }
                if(0==count){
     
                    if('.'==cpdomains[i][j]){
     
                        count++;
                        idx1 = j;
                    }
                }else if(1==count){
     
                    if('.'==cpdomains[i][j]){
     
                        count++;
                        idx2 = j;
                    }
                }else break;
            }
            int times = stoi(cpdomains[i].substr(0,idx0));
            tmp[cpdomains[i].substr(idx1+1,m-idx1-1)] += times;
            tmp[cpdomains[i].substr(idx0+1,m-idx0-1)] += times;
            if(2==count){
     
                tmp[cpdomains[i].substr(idx2+1,m-idx2-1)] += times;
            }
        }
        for(auto iter = tmp.begin(); iter != tmp.end(); ++iter){
     
            string str = to_string(iter->second) + " " + iter->first;
            res.push_back(str);
        }
        return res;
    }
};

812.最大三角形面积

class Solution {
     
public:
    double largestTriangleArea(vector<vector<int>>& points) {
     
        double s = 0;
        for(int i =0;i<points.size();i++){
     
            for(int j=0;j<points.size();j++){
     
                for(int k=0;k<points.size();k++){
     
                    s = max(s,0.5*(points[k][0]*points[j][1]+points[j][0]*points[i][1]+points[i][0]*points[k][1]-points[j][1]*points[i][0]-points[k][1]*points[j][0]-points[k][0]*points[i][1]));
                }
            }
        }
        return s;
    }
};

819.最常见的词

class Solution {
     
public:
    string mostCommonWord(string paragraph, vector<string>& banned) {
     
        unordered_map<string,int> count;
        for(auto& c : paragraph){
     
            c = isalpha(c) ? tolower(c) : ' ';
        }
        istringstream iss(paragraph);
        string w;
        pair<string,int> res{
     "",0};
        while(iss >> w){
     
            if(find(banned.begin(), banned.end(),w)==banned.end() && ++count[w] > res.second){
     
                res = make_pair(w, count[w]);
            }
        }
        return res.first;
    }
};

821.字符的最短距离

class Solution {
     
public:
    vector<int> shortestToChar(string S, char C) {
     
        int n = S.length();
        vector<int> res(n);
        int idx = -1;
        for(int i = 0; i < n; ++i){
     
            if(S[i] == C){
     
                for(int k = idx + 1; k < i; ++k){
     
                    res[k] = idx == -1 ? (i - k) : min(k-idx, i - k);
                }
                res[i] = 0;
                idx = i;
            }
        }
        if(idx < n - 1){
     
            for(int k = idx + 1; k < n; ++k){
     
                res[k] = k - idx;
            }
        }
        return res;
    }
};

824.山羊拉丁文

class Solution {
     
public:
    string toGoatLatin(string S) {
     
        string vo = "aeiouAEIOU";
        istringstream iss(S);
        string w;
        string res;
        int count = 0;
        while(iss>>w){
     
            count++;
            if(vo.find(w[0]) == string::npos){
     
                w.push_back(w[0]);
                w.erase(w.begin());
            }
            res += w + "ma" + string(count,'a') + " ";
        }
        res.erase(res.find_last_of(' '));
        return res;
    }
};

830.较大分组的位置

class Solution {
     
public:
    vector<vector<int>> largeGroupPositions(string S) {
     
        char last = S[0];
        int beg = 0, end = 0;
        vector<vector<int>> res;
        for(int i = 1; i < S.size(); ++i){
     
            if(S[i] == last) end++;
            else{
     
                if(end-beg>=2) {
     
                    res.push_back(vector<int>{
     beg,end});
                }
                beg = end = i;
                last = S[i];
            }
        }
        if(end-beg>=2) res.push_back(vector<int>{
     beg,end});
        return res;
    }
};

832.翻转图像

class Solution {
     
public:
    vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) {
     
        int n = A.size(), m = A[0].size();
        for(int i = 0; i < n; ++i)
            for(int j = 0; j < (m+1)/2; ++j){
     
                int tmp = A[i][j];
                A[i][j] = A[i][m-1-j] ^ 1;
                A[i][m-1-j] = tmp ^ 1;
            }
        return A;    
    }
};

836.矩形重叠

class Solution {
     
public:
    bool isRectangleOverlap(vector<int>& rec1, vector<int>& rec2) {
     
        int bx = max(rec1[0],rec2[0]);
        int tx = min(rec1[2],rec2[2]);
        int by = max(rec1[1],rec2[1]);
        int ty = min(rec1[3],rec2[3]);
        return (bx < tx) && (by < ty);
    }
};

840.矩阵中的幻方

class Solution {
     
public:
    int numMagicSquaresInside(vector<vector<int>>& grid) {
     
        if(grid.size() < 3 || grid[0].size() < 3) return 0;
        int res = 0;
        int n = grid.size(), m = grid[0].size();
        for(int i = 0; i < n - 2; ++i)
            for(int j = 0; j < m - 2; ++j){
     
                set<int> sgrid;
                int k = 0;
                for(; k < 3; ++k){
     
                    for(int l = 0; l < 3; ++l){
     
                    sgrid.insert(grid[i+k][j+l]);
                    }
                if(grid[i+k][j] + grid[i+k][j+1] + grid[i+k][j+2] != 15) break;
                }
                if(k!=3) continue;
                if(sgrid.size() < 9 || *(--sgrid.end()) > 9) continue;
                if(grid[i][j] + grid[i+1][j+1] + grid[i+2][j+2] != 15) continue;
                if(grid[i][j] + grid[i+1][j] + grid[i+2][j] != 15) continue;
                res++;
            }
        return res;
    }
};

844.比较含退格的字符串

class Solution {
     
public:
    bool backspaceCompare(string S, string T) {
     
        stack<char> ss,st;
        for(auto c : S){
     
            if('#'==c){
     
                if(ss.empty()) continue;
                ss.pop();
            }else ss.push(c);
        }
        for(auto c : T){
     
            if('#'==c){
     
                if(st.empty()) continue;
                st.pop();
            }else st.push(c);
        }
        return ss == st;
    }
};

849.到最近的人的最大距离

class Solution {
     
public:
    int maxDistToClosest(vector<int>& seats) {
     
        int n = seats.size();
        int maxLen = 0;
        int i = 0;
        while(i<n){
     
            if(seats[i]==0){
     
                int k = 0;
                int l = i - 1;
                while(l>=0&&seats[l]==0) l--;
                int r = i + 1;
                while(r<n&&seats[r]==0) r++;
                if(l < 0) k = r - i;
                else if(r==n) k = i - l;
                else k = min(r-i,i-l);
                maxLen = max(maxLen,k);
            }
            ++i;
        }
        return maxLen;
    }
};

852.山峰数组的峰顶索引

class Solution {
     
public:
    int peakIndexInMountainArray(vector<int>& A) {
     
        int left = 0, right = A.size()-1, mid;
        while(left < right){
     
            mid = (left + right) / 2;
            if(A[mid-1] < A[mid] && A[mid] < A[mid+1]) left = mid;
            else if(A[mid-1] > A[mid] && A[mid] > A[mid+1]) right = mid;
            else break;
        }
        return mid;
    }
};

859.亲密字符串

class Solution {
     
public:
    bool buddyStrings(string A, string B) {
     
        if(A.size() != B.size())
            return false;

        vector<int> temp;
        for(int i=0;i<A.length();i++){
     
                if(A[i] !=B[i]){
     
                    temp.push_back(i);
                    if(temp.size()>2){
     
                        return false;
                    }
                }
        }
        if(temp.size() == 0){
     
            map<char,int> m;
            for(int i=0;i<A.length();i++){
     
                m[A[i]]++;
                if(m[A[i]]>=2){
     
                    return true;
                }
            }
            return false;
        }


        if(temp.size() == 1)
            return false;
        else{
     
            if(A[temp[0]] == B[temp[1]] && A[temp[1]]== B[temp[0]]){
     
                return true;
            }
            return false;
        }
    }
};

860.柠檬水找零

class Solution {
     
public:
    bool lemonadeChange(vector<int>& bills) {
     
        int fives = 0, tens = 0;
        for(auto n : bills){
     
            switch(n){
     
                case 5:
                    fives++;
                    break;
                case 10:
                    fives--;
                    tens++;
                    break;
                case 20:
                    tens > 0 ? tens--,fives-- : fives -= 3;
                    break;
            }
            if(fives < 0) return false;
        }
        return true;
    }
};

867.转置矩阵

class Solution {
     
public:
    vector<vector<int>> transpose(vector<vector<int>>& A) {
     
        int n = A.size(), m = A[0].size();
        vector<vector<int>> res(m, vector<int>(n));
        for(int i = 0; i < m; ++i){
     
            for(int j = 0; j < n; ++j){
     
                res[i][j] = A[j][i];
            }
        }
        return res;
    }
};

868.二进制间距

class Solution {
     
public:
    int binaryGap(int N) {
     
        int idx = -1, count = 0;
        int res = 0;
        while(N){
     
            if(N&1){
     
                if(idx != -1) res = max(res, count - idx);
                idx = count;
            }
            N >>= 1;
            count++;
        }
        return res;
    }
};

872.叶子相似的树

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
     
public:
    bool leafSimilar(TreeNode* root1, TreeNode* root2) {
     
        vector<int> lv1, lv2;
        lvSeq(lv1,root1);
        lvSeq(lv2,root2);
        return lv1==lv2;
    }
    void lvSeq(vector<int>& lv, TreeNode* root){
     
        if(root==NULL) return;
        stack<TreeNode*> s;
        s.push(root);
        while(!s.empty()){
     
            TreeNode* p = s.top();
            s.pop();
            if(p->left == NULL && p->right == NULL) lv.push_back(p->val);
            if(p->right!=NULL) s.push(p->right);
            if(p->left!=NULL) s.push(p->left);
        }
        return;
    }
};

874.模拟行走机器人

/*
*在set中查找的速度要远比vector快
*/
class Solution {
     
public:
    int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {
     
        int res = 0;
        vector<pair<int,int>> dir{
     {
     0,1},{
     1,0},{
     0,-1},{
     -1,0}};
        int i = 0, x = 0, y = 0;
        set<pair<int,int>> ob;
        for(auto o : obstacles){
     
            ob.insert(make_pair(o[0],o[1]));
        }
        for(auto cd : commands){
     
            if(-2==cd) i = (i+3)%4;
            else if(-1==cd) i = (++i)%4;
            else {
     
                while(cd){
     
                    int dx = x + dir[i].first;
                    int dy = y + dir[i].second;
                    if(ob.find(make_pair(dx,dy))==ob.end()){
     
                        x = dx;
                        y = dy;
                        res = max(res, x*x+y*y);
                    }else break;
                    cd--;
                }
            }
        }
        return res;
    }
};

876.链表的中间节点

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
     
public:
    ListNode* middleNode(ListNode* head) {
     
        ListNode * fast = head;
        while(!(fast==NULL || fast->next==NULL)){
     
            head = head->next;
            fast = fast->next->next;
        }
        return head;
    }
};

883.三维形体投影面积

class Solution {
     
public:
    int projectionArea(vector<vector<int>>& grid) {
     
        int sum = 0;
        for(int i = 0; i < grid.size(); ++i){
     
            int temp1 = 0, temp2 = 0;
            for(int j = 0; j < grid[0].size(); ++j){
     
                if(grid[i][j]>0) sum++;
                temp1 = max(temp1, grid[i][j]);
                temp2 = max(temp2, grid[j][i]);
            }
            sum += temp1 + temp2;
        }
        return sum;
    }
};

884.两句话中的不常见单词

class Solution {
     
public:
    vector<string> uncommonFromSentences(string A, string B) {
     
        string t = A + " " + B;
        istringstream iss(t);
        string w;
        map<string, int> mmap;
        vector<string> res;
        while(iss >> w){
     
            mmap[w]++;
        }
        for(auto iter = mmap.begin(); iter != mmap.end(); ++iter){
     
            if(1==iter->second)
                res.push_back(iter->first);
        }
        return res;
    }
};

888.公平的糖果交换

class Solution {
     
public:
    vector<int> fairCandySwap(vector<int>& A, vector<int>& B) {
     
        int m = A.size(), n = B.size();
        vector<int> res(2);
        int sum1 = 0, sum2 = 0;
        int l = max(m, n);
        for(int i = 0; i < l; ++i){
     
            if(i < m) sum1 += A[i];
            if(i < n) sum2 += B[i];
        }
        unordered_map<int,int> amap;
        for(auto a : A){
     
            amap[(sum2 - sum1)/2 + a] = 1;
        }
        for(auto b : B){
     
            if(amap.find(b)!=amap.end()){
     
                res[0] = (sum1 - sum2)/2 + b;
                res[1] = b;
                return res;
            }
        }
        return res;
    }
};

892.三维形体的表面积

class Solution {
     
public:
    int surfaceArea(vector<vector<int>>& grid) {
     
        int n = grid.size(), m = grid[0].size();
        int area = 0;
        for(int i = 0; i < n; ++i){
     
            for(int j = 0; j < m; ++j){
     
                if(grid[i][j] > 0) area += 4*grid[i][j]+2;
                if(i < n - 1) area -= min(grid[i][j], grid[i+1][j])*2;
                if(j < m - 1) area -= min(grid[i][j], grid[i][j+1])*2;
            }
        }
        return area;
    }
};

893.特殊等价字符串

class Solution {
     
public:
    int numSpecialEquivGroups(vector<string>& A) {
     
        set<string> m;
        for(int i = 0; i < A.size(); ++i){
     
            string odd = "";
            string even = "";
            for(int j = 0; j < A[i].length(); ++j){
     
                if(j%2==0) odd += A[i][j];
                else even += A[i][j];
            }
            sort(odd.begin(), odd.end());
            sort(even.begin(), even.end());
            m.insert(odd+even);
        }
        return m.size();
    }
};

896.单调数列

class Solution {
     
public:
    bool isMonotonic(vector<int>& A) {
     
        bool inc = true, dec = true;
        int n = A.size();
        for(int i = 1; i < n; ++i){
     
            if(inc || dec){
     
                if(A[i] > A[i-1]) dec = false;
                if(A[i] < A[i-1]) inc = false;
            } else break;
        }
        return inc || dec;
    }
};

897.递增顺序查找树

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
     
public:
    TreeNode* increasingBST(TreeNode* root) {
     
        queue<TreeNode*> qt;
        inOrder(root, qt);
        TreeNode* head = qt.front();
        qt.pop();
        TreeNode* p = head;
        while(!qt.empty()){
     
             p->left = NULL;
             p->right = qt.front();
             qt.pop();
             p = p->right;
        }
        p->left = p->right = NULL;
        return head;
    }
    void inOrder(TreeNode* root, queue<TreeNode*>& qt){
     
        if(root != NULL){
     
            inOrder(root->left, qt);
            qt.push(root);
            inOrder(root->right,qt);
        }
    }
};

你可能感兴趣的:(LeetCode算法简单难度,算法,leetcode,二叉树)