02.19

文章展示了多个编程问题的解决方案,包括利用排序和哈希表找出变位词组,判断外星语言字符顺序,找到最小时间差,求解后缀表达式的值,以及模拟小行星碰撞的过程,并计算每日温度变化所需的等待天数。这些问题涉及字符串操作、算法设计和数据结构的应用。
摘要由CSDN通过智能技术生成

剑指 Offer II 033. 变位词组

class Solution {
public:
//由于互为字母异位词的两个字符串包含的字母相同,因此对两个字符串分别进行排序之后得到的字符串一定是相同的,故可以将排序之后的字符串作为哈希表的键。
    vector<vector<string>> groupAnagrams(vector<string>& strs) {
        unordered_map<string,vector<string>> mp;
        for(string &str:strs)
        {
            string key=str;
            sort(key.begin(),key.end());
            mp[key].emplace_back(str);
        }
        vector<vector<string>>ans;
        for(auto it=mp.begin();it!=mp.end();it++)
        {
            ans.emplace_back(it->second);
        }
        return ans;

    }
};

 剑指 Offer II 034. 外星语言是否排序

class Solution {
public:
    bool isAlienSorted(vector<string>& words, string order) {
        //哈希表储存字符
        vector<int>index(26);
        for(int i=0;i<26;i++)
        {
            index[order[i]-'a']=i;
        }
        for(int i=1;i<words.size();i++)
        {
            bool vail=false;
            for(int j=0;j<words[i-1].size()&&j<words[i].size();j++)
            {
                int pre=index[words[i-1][j]-'a'];
                int curr=index[words[i][j]-'a'];
                if(pre<curr)
                {
                    vail=true;break;
                }
                if(pre>curr)
                {
                    return false;
                }
            }
            if(!vail)
            {
                 /* 比较两个字符串的长度 */
                if(words[i-1].size()>words[i].size())
                {
                    return false;
                }
            }
        }
        return true;

    }
};

 剑指 Offer II 035. 最小时间差

class Solution {
    int getMinutes(string &t) {
        return (int(t[0] - '0') * 10 + int(t[1] - '0')) * 60 + int(t[3] - '0') * 10 + int(t[4] - '0');
    }

public:
    int findMinDifference(vector<string> &timePoints) {
        int n = timePoints.size();
        //鸽巢原理可知24×60=1440  大于1440一定存在相同的时间
        if (n > 1440) {
            return 0;
        }
        sort(timePoints.begin(), timePoints.end());
        int ans = INT_MAX;
        int t0Minutes = getMinutes(timePoints[0]);
        int preMinutes = t0Minutes;
        for (int i = 1; i < n; ++i) {
            int minutes = getMinutes(timePoints[i]);
            ans = min(ans, minutes - preMinutes); // 相邻时间的时间差
            preMinutes = minutes;
        }
        ans = min(ans, t0Minutes + 1440 - preMinutes); // 首尾时间的时间差
        return ans;
    }
};

 剑指 Offer II 036. 后缀表达式

class Solution {
public:
    int evalRPN(vector<string>& tokens) {
        //创建一个栈
        stack<int> stk;
        //字符长度
        int n = tokens.size();
        //遍历
        for (int i = 0; i < n; i++) {
            string& token = tokens[i];
            //确认为阿拉伯数字
            if (isNumber(token)) {
                //atoi(s)函数用于把一个字符串转换成一个整型数据
                //c_str() 函数可以将 const string* 类型 转化为 cons char* 类型
                stk.push(atoi(token.c_str()));
            } else {
                int num2 = stk.top();
                stk.pop();
                int num1 = stk.top();
                stk.pop();
                switch (token[0]) {
                    case '+':
                        stk.push(num1 + num2);
                        break;
                    case '-':
                        stk.push(num1 - num2);
                        break;
                    case '*':
                        stk.push(num1 * num2);
                        break;
                    case '/':
                        stk.push(num1 / num2);
                        break;
                }
            }
        }
        return stk.top();
    }

    bool isNumber(string& token) {
        return !(token == "+" || token == "-" || token == "*" || token == "/");
    }
};

 剑指 Offer II 037. 小行星碰撞

class Solution {
public:
    vector<int> asteroidCollision(vector<int>& asteroids) {
        vector<int> st;
        for(auto aster:asteroids)
        {
            bool alive=true;
            //向左运行的行星是否碰撞
            //st.back()栈顶元素
            while(alive&&aster<0&&!st.empty()&&st.back()>0)
            {
                alive=st.back()<-aster;
                if(st.back()<=-aster)
                {
                    st.pop_back();
                }
            }
            if(alive)
            {
                st.push_back(aster);
            }
        }
        return st;

    }
};

 

 剑指 Offer II 038. 每日温度

class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& temperatures) {
        int n = temperatures.size();
        //输出
        vector<int> ans(n);
        //存放下标第几天
        stack<int> s;
        //遍历
        for (int i = 0; i < n; ++i) {
            //非空&&该天温度大于栈顶温度
            while (!s.empty() && temperatures[i] > temperatures[s.top()]) {
                int previousIndex = s.top();
                ans[previousIndex] = i - previousIndex;
                s.pop();
            }
            //小于栈顶温度 则压入
            s.push(i);
        }
        return ans;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值