zoukankan      html  css  js  c++  java
  • 【LeetCode】第235场

    链接
    1.截断句子
    直接遍历统计空格数量

    class Solution {
    public:
        string truncateSentence(string s, int k) {
            int cnt = 0;
            string res = "";
            for(int i = 0;i < s.size(); i++){
                if(s[i] == ' ') cnt++;
                if(cnt == k) break;
                res += s[i];
            }
            return res;
        }
    };
    

    或用python一行搞定

    class Solution:
        def truncateSentence(self, s: str, k: int) -> str:
            return ' '.join((s.split())[:k])
    

    2.查找用户活跃分钟数

    class Solution {
    public:
        vector<int> findingUsersActiveMinutes(vector<vector<int>>& logs, int k) {
            unordered_map<int,unordered_set<int>> m;
            for(auto &log : logs){
                m[log[0]].insert(log[1]);
            }
            vector<int> res(k);
            for(auto &i : m){
                res[i.second.size()-1]++;
            }
            return res;
        }
    };
    
  • 相关阅读:
    Array
    java 设计模式
    Hashtable
    lettCode-Array
    最短路径 dijkstra
    算法:优先级队列
    7.29 DFS总结
    SZU:D89 The Settlers of Catan
    SZU:B47 Big Integer I
    7.25 RPN转换
  • 原文地址:https://www.cnblogs.com/whisperbb/p/14624758.html
Copyright © 2011-2022 走看看