1.动态规划的思想解决

  /**
     * 在时间复杂度为O(N)内找出数组中最大的子序列的累加和
     */
    public static int sumNum(int[] array) {
        int n = array.length;
        int all = array[n - 1], start = array[n - 1];
        int count = 0;
        for (int i = n - 2; i >= 0; i--) {
            if ((start + array[i]) > array[i]) {
                start = start + array[i];
            } else {
                start = array[i];
            }
            if (all < start) {
                all = start;
            }
            count++;
        }
        System.out.println("数组长度:" + array.length + ",  时间复杂度:" + count);
        System.out.println("最大值" + all);
        return all;
    }

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-03
  • 2022-02-28
  • 2021-07-26
  • 2022-12-23
  • 2022-01-31
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-09-14
  • 2021-06-02
  • 2022-01-29
  • 2022-12-23
  • 2021-12-21
相关资源
相似解决方案