[LeetCode]t944_删列造序

题目链接:LeetCode.994

题目描述

给你由n个小写字母字符串组成的数组strs,其中每个字符串长度相等。
这些字符串可以每个一行,排成一个网格。例如,strs = ["abc", "bce", "cae"] 可以排列为:

abc
bce
cae

你需要找出并删除 不是按字典序升序排列的 列。在上面的例子(下标从 0 开始)中,列 0('a', 'b', 'c')和列 2('c', 'e', 'e')都是按升序排列的,而列 1('b', 'c', 'a')不是,所以要删除列 1 。
返回你需要删除的列数。

思路

纯水题,遍历即可,不必做过多解释。

实现代码

#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Solution
{
public:
    int minDeletionSize(vector<string> &strs)
    {
        int ret = 0;
        int sz = strs.size();
        int n = strs[0].size();
        char tmp;
        for (int i = 0; i < n; i++)
        {
            tmp = 0;
            for (int j = 0; j < sz; j++)
            {
                if (strs[j][i] < tmp)
                {
                    ret += 1;
                    break;
                }
                else
                {
                    tmp = strs[j][i];
                }
            }
        }
        return ret;
    }
};
int main()
{
    vector<string> strs;
    strs.push_back("cba");
    strs.push_back("daf");
    strs.push_back("ghi");
    Solution s;
    cout << s.minDeletionSize(strs) << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值