The maximum and __ algorithms for----continuous sub arrays in Java programming

Source: Internet
Author: User

The programming of this blog is about arrays, arrays are important in programming, because arrays in Java are stored directly in the stack, so the operation is very convenient, efficient, arrays are often part of the interview, on the array of programming topics rich and diverse, logic is very strong, I hope you will practice more and improve your ability to solve problems.

Topic Description: In a one-dimensional array, find the maximum and the number of contiguous sub arrays. If the array is full of integers, then the sum of the largest and all elements, then there are negative numbers. For example: {6,-3,-2,7,-15,1,2,2}, the maximum and the number of consecutive sub vectors is 8 (starting from the No. 0, to the 3rd).

Title Requirements: time limit: 1 seconds space limit: 32768K

Ideas for solving problems:

1. First of all, we need to define a variable currentsum, with a for loop to record the previous I and currentsum each time, if the currentsum value is less than 0, we will only reduce the maximum and then, so we need to add array[i+1 The value of the item is currentsum to the new value.

2. We need to define a maximum max, each time we change the value of currentsum, we need to compare Max with Currentsum, and if Currentsum is greater than Max, we assign the Currentsum value to max.

Implementation code:

public class Testarray {public
     static int Findgreatestsumofsubarray (int[] array) {
         if (array.length==0 | | array= =null) {return
             0;
         }
        int currentsum = 0;     The and
        int max = 0 that stores the current continuous n items;            Save the maximum for the contiguous child elements and the for
        (int i = 0; i < Array.Length i++) {
            //core part, well understood.
            if (currentsum<=0) {      //if the current continuous n items are less than or equal to 0, there is no need to add the following elements
                currentsum = array[i];      Currentsum
            }else{
                currentsum + = Array[i];     If the value of Currentsum is greater than 0, it continues to be added to the following elements,
            }
            if (Currentsum>max) {         //each change currentsum value has a comparison with Max
                = Currentsum;       If the value of the currentsum is greater than Max, assign the value of Currentsum to Max
            } return
        max;
     }  

Code test:

public static void Main (string[] args) {
        int[] array = {6,-3,-2,7,-15,1,2,2};
        int result = Findgreatestsumofsubarray (array);
        SYSTEM.OUT.PRINTLN (maximum and for continuous child elements: +result);
    }

the maximum and the number of consecutive child elements is: 8

This programming problem can be a good review of the programmer's ability, if you can write, explain the programming ability is good, programming ability can be improved through practice, hope to make efforts.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.