java编程练习题六

题目链接:https://leetcode-cn.com/problems/delete-columns-to-make-sorted/

题目解析:

1)外层循环遍历每个字符串的字符;
2)内存循环遍历字符串数组A的每个字符串元素;
3)当出现((A[j].charAt(i)-'a')>(A[j+1].charAt(i)-'a'))时,即列中是降序时,停止内存循环遍历,输出结果out++;

 

代码:

class Solution {
    public int minDeletionSize(String[] A) {
        int out = 0;
    	for(int i =0;i< A[0].length();i++) {
    		int j =0;
    		while(j<A.length-1) {
    			if((A[j].charAt(i)-'a')<=(A[j+1].charAt(i)-'a')) {
    				j++;
    			}else {
    				out++;
    				break;
    			}
    		}
    	}
        return out;
    }
}

 

版权声明:本文为qq_44923097原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_44923097/article/details/104816094