0
点赞
收藏
分享

微信扫一扫

[leetcode] minimum-falling-path-sum

sin信仰 04-15 14:30 阅读 1
算法java

题的内容

解法 1 

思路 1

构建一个Map,Map的key是小写字母,Map的Value有三个值分别为0,1,2.  

0 : 代表只有一个小写字母。

1: 代表所有这个小写字母在大写字母之前。也就是正确的字母

2: 至少一个大写字母在这个小写字母前面。也就是不正确的字母。

写一个for 循环构建这个Map就可以了。

代码 1


class DifferenceLetterCount2 {

    public boolean isLowerCase(String letter){
        if (letter.equals(letter.toLowerCase())){
            return true;
        }else{
            return false;
        }
    }

    public int solution(String letters) {
        // 0 : initial , 1 : true, 2 : false
        Map<String, Integer> letterMap = new HashMap<String, Integer>();
        Integer correctOrder = 0;

        char[] letterArray = letters.toCharArray();
        for (int i =0 ; i< letters.length(); i++ ){
            String letter = Character.toString(letterArray[i]);
            Integer flag = letterMap.get(letter.toLowerCase());
            if (flag== null){
                if (isLowerCase(letter)){
                    letterMap.put(letter, 0);
                }else{
                    letterMap.put(letter.toLowerCase(), 2);
                }
            }else{
                if (flag==0 ){
                    if (!isLowerCase(letter)){
                        correctOrder++;
                        letterMap.put(letter.toLowerCase(), 1);
                    }
                }else if(flag==1){
                    if (!isLowerCase(letter)){
                        letterMap.put(letter.toLowerCase(), 2);
                    }
                }
            }
        }
        return correctOrder;
    }

    public static void main(String[] args) {
        DifferenceLetterCount2 solution = new DifferenceLetterCount2();
        // Test cases
        System.out.println(solution.solution("aaAbcCABBc"));  // Output: 2
        System.out.println(solution.solution("xyzXYZabcABC")); // Output: 6
        System.out.println(solution.solution("ABCabcAefG"));   // Output: 0
    }
}

解法 2 

思路

构建一个长度为26的整数数组。26代表26各字母,数组里面整数有三个值0, 1, -1, 2.

0 : 初始值。代表没有相应的字母。

1:只有一个小写字母。

2: 所有小写在大写之前,代表正确顺序。

-1 : 不正确顺序,至少一个大写字母在小写之前。

写个for 循环构建这个整数数组。

代码


class DifferenceLetterCount {

    public int solution(String letters) {
        int count = 0;
        int[] array = new int[26];
        for (int i = 0; i < letters.length(); i++) {
            if (Character.isLowerCase(letters.charAt(i))) { // lower
                int index = letters.charAt(i) - 'a';
                // -1, 1 do nothing
                if (array[index] == 0)
                    array[index] = 1; // only have lowercase letter
                if (array[index] == 2)
                    array[index] = -1; //incorrect order
            } else { // upper
                int index = Character.toLowerCase(letters.charAt(i)) - 'a';
                // -1, 2 do nothing
                if (array[index] == 0)
                    array[index] = -1; // incorrect order
                else if (array[index] == 1)
                    array[index] = 2; // correct order
            }
        }

        for (int e : array) {
            if (e == 2)
                count++; // count the number of correct order.
        }
        return count;

    }

    public static void main(String[] args) {
        DifferenceLetterCount solution = new DifferenceLetterCount();

        // Test cases
        System.out.println(solution.solution("aaAbcCABBc"));  // Output: 2
        System.out.println(solution.solution("xyzXYZabcABC")); // Output: 6
        System.out.println(solution.solution("ABCabcAefG"));   // Output: 0
    }
}

举报

相关推荐

0 条评论