944. 删列造序-----------LeetCode

╄→гoц情女王★ 提交于 2020-01-29 08:07:28

解题思路:

(1)取出字符串数组s中的每一列组成新的字符串数组

(2)遍历新的字符串数组的每一行,比较当前行的相邻字符的大小,如果出现前一个字符比后一个字符搭的情况,说明当前列要删除,计数器加1

class Solution {
    public int minDeletionSize(String[] A) {
        int rows=A.length;
        int cols=A[0].length();
        String[] arrays=new String[cols];
        for(int i=0;i<cols;++i){
            String temp="";
            for(int j=0;j<rows;++j){
                temp+=A[j].substring(i,i+1);
            }
            arrays[i]=temp;
        }
        int cnt=0;
        for(int i=0;i<cols;++i){
            String temp=arrays[i];
            for(int j=0;j<rows-1;++j){
                if(temp.charAt(j)>temp.charAt(j+1)){
                    cnt++;
                    break;
                }
            }
        }
        return cnt;
    }
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!