数组中出现次数超过一半的数字&&矩阵的最小路径和&&表达式求值

127 阅读1分钟

NC73 数组中出现次数超过一半的数字

题目链接

1、解题思路

摩尔投票法

2、代码
public class Solution {
    public int MoreThanHalfNum_Solution(int [] array) {
        int ans = array[0];
        int cnt = 1;
        for (int i = 1; i < array.length; i++) {
            if (array[i] == ans) {
                cnt++;
            } else {
                if (cnt == 0) {
                    cnt++;
                    ans = array[i];
                } else {
                    cnt--;
                }
            }
        }
        return ans;
    }
}

NC59 矩阵的最小路径和

题目链接

1、解题思路

简单dp

2、代码
import java.util.*;


public class Solution {
    /**
     * 
     * @param matrix int整型二维数组 the matrix
     * @return int整型
     */
    public int minPathSum (int[][] matrix) {
        // write code here
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return 0;
        }
        int r = matrix.length;
        int c = matrix[0].length;
        int[][] dp = new int[r][c];
        dp[0][0] = matrix[0][0];
        for (int i = 1; i < c; i++) {
            dp[0][i] = matrix[0][i] + dp[0][i - 1];
        }
        for (int i = 1; i < r; i++) {
            dp[i][0] = dp[i - 1][0] + matrix[i][0];
        }
        for (int i = 1; i < r; i++) {
            for (int j = 1; j < c; j++) {
                int minV = dp[i][j - 1] < dp[i - 1][j] ? dp[i][j - 1] : dp[i - 1][j];
                dp[i][j] = minV + matrix[i][j];
            }
        }
        return dp[r - 1][c - 1];
    }
}

NC137 表达式求值

题目链接

1、解题思路

递归的思想可以解除有括号的困扰,然后剩余的就常规的解就好了。

2、代码
import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * 返回表达式的值
     * @param s string字符串 待计算的表达式
     * @return int整型
     */
    public int solve (String s) {
        // write code here
        int number = 0;
        int sum = 0;
        char sign = '+';
        int index = 0;
        Stack<Integer> stack = new Stack<>();
        int len = s.length();
        while (index < len) {
            char element = s.charAt(index);
            if (Character.isDigit(element)) {
                number = number * 10 + element - 48;
                index++;
            } else if (element == '(') {
                int j = index + 1;
                int cnt = 1;
                while (cnt != 0) {
                    if (s.charAt(j) == '(') {
                        cnt++;
                    } else if (s.charAt(j) == ')') {
                        cnt--;
                    }
                    j++;
                }
                number = solve(s.substring(index + 1, j - 1 ));
                index = j;
            } else {
                index++;
            }
            if ((!Character.isDigit(element) && element != '(') || index == len) {
                if (sign == '+') {
                    stack.push(number);
                } else if (sign == '*') {
                    stack.push(stack.pop() * number);
                } else {
                    stack.push(-1 * number);
                }
                sign = element;
                number = 0;
            }
        }
        while (!stack.isEmpty()) {
            sum = sum + stack.pop();
        }
        return sum;
    }
}