zoukankan      html  css  js  c++  java
  • 结队开发-最大子数组

    要求:

      对于任意一个整数数组,找出最大和的字数组。要求时间复杂度为O(n)。

    设计思路:

      数字类型 负数,零,正数,那么最大子数组

      1.所有正数/(正数+零)

      2.最小的负数

      主代码:

    package 课堂测试N;
    //编译调试技巧  用一句System是否执行到该句
    //方法变量命名    类首字母大写 方法首字母小写其余大写 变量所有小写 常量大写
    //tab间做间隔   4个空格
    //多条件判断加括号
    //每个大括号担起一行
    //对重点加注释,不用在语法上进行注释
    
    public class KTACE {
    
        static int l=0;
        static List list = new List();
        public static void main(String[] args){
            boolean flag0=false;
            
            int A[] ={-5,2,0,-9,-1};
            
            for(int i=0;i<A.length;i++)
            {
                if(A[i]>=0)
                    {
                    l++;
                    flag0=true;//只要有正数就调用非负数数方法
                    }
            }
            if(flag0==true)
                {
                int b[]=maxszz(A,l);
                System.out.print("最大子数组");
                for(int len=0;len<b.length;len++)
                    System.out.print(b[len]+" ");
                }
            else 
                maxszf(A);
        }
    
        //找最大
        public static int Largest(int List[],int length)
        {    
            boolean flag=true;
            
            if(length==0)
            {
            System.out.print("数组长度为 ");
            flag=false;
            return 0;//return的位置可以是后面不用进行
            }
            
            else
            {
            int  i,max=List[0];//max不能设置为0,若是负数就不可一返回该数字
            int num=0;
            
            
            for(int m=0;m<length;m++)
            {
                if(List[m]==0)
                    num++;
            }
            
            if(length!=0&&num==length)
            {
                System.out.print("数组未赋值");
                flag=false;
            }
            if(flag==true)
            {
            for(i=0;i<length;i++)
            {
                if(List[i]>=max)
                {max=List[i];}
            }
            }
            return max;
            }
            }
        //有非负数的最大数组
        public static int[] maxszz(int a[],int l)
        {
            int b[] =new int[l];
            for(int i=0;i<a.length;i++)
            {
                if(a[i]>=0)
                    list.insert(a[i]);
            }
            for(int j=0;j<l;j++)
            {
                b[j]=Integer.parseInt(String.valueOf(list.head.next.data));
                list.head=list.head.next;
            }
            /*System.out.print("最大数组元素");
            for(int mm=0;mm<b.length;mm++)
            {
                System.out.print(b[mm]+" ");
            }
            System.out.println(" ");*/
            return b;
        }
        //全为负数的最大数组
        public static int[] maxszf(int a[])
        {
            int b[] =new int[1];
            int k=a[0];
            for(int i=0;i<a.length;i++)
            {
                if(a[i]>k)
                    {
                        k=a[i];
                        break;
                    }
            }
            System.out.println("最大数组元素"+k);
            return b;
        }
        
        }

      链表 Node:

      

    package 课堂测试N;
    
    import java.util.Random;
    
    public class Node {
        int data;
        Node next;
        public Node(int data2, Node node) {
            this.data = data2;
            this.next = node;
        }
    
    }

      链表 List:

    package 课堂测试N;
    public class List {
        Node head;
        int size;
    
        public List() {
            head = new Node(0, null);
            size = 0;
        }
    
        public Node getLast() {
            Node node = head;
            while(node.next != null) {
                node = node.next;
            }
            return node;
        }
    
        public int insert(int  data) {
            Node last = getLast();
            Node node = new Node(data, null);
            last.next = node;
            size ++;
            return 1;
        }
    
        public void display() {
            System.out.println("size = " + size + "");
            Node temp = head.next;
            while(temp != null) {
                System.out.print(temp.data + " ");
                temp = temp.next;
            }
            System.out.println();
        }
    }

    运算截图:

      

  • 相关阅读:
    GUID
    ORA-04044: 此处不允许过程, 函数, 程序包或类型和
    去掉word文档两边的空白
    Mysql数据库服务启动
    计算两个日期之间的天数
    SpringMVC——接收请求参数和页面传参
    ajax中get和post区别
    如何实现两个页面之间进行传值
    面试题
    MySQL数据库优化
  • 原文地址:https://www.cnblogs.com/Amyheartxy/p/6592634.html
Copyright © 2011-2022 走看看