There are several pairs of opposite numbers in the computed array

Source: Internet
Author: User

Package Interview;import Java.util.arrays;import Java.util.collections;import java.util.comparator;import  java.util.list;/** * * @author * calculation in the array there are several pairs of opposite numbers * */public class Listsort {public static void main (string[] args) {Listsort        Test = new Listsort ();        list<integer> list = Arrays.aslist (9,1,3,8,-10,7,-9,-2,-8,2); context<integer> contextmin= New Context<> (Test.new mincomparatorlist ());//context<integer> cont                extmax= New Context<> (Test.new maxcomparatorlist ());         list<integer> listmin = contextmin.sortlist (list);//List<integer> Listmax = contextmax.sortlist (list);              integer[] Arrays = (integer[]) Listmin.toarray (); System.out.println (GetCount (Arrays,getmidindex (arrays));} public static int GetCount (integer[] arrays,int mid) {int count=0;for (int. i=0;i<mid;i++) {for (int j=mid;j< arrays.length;j++) {if (arrays[i]+arrays[j]==0) {count++;}}} return count;} public static int Getmidindex (InteGer[] arrays) {int start = 0;        int end =arrays.length-1;        int mid = 0;        while (start<=end) {mid = (End-start)/2+start;        if ((mid>0 && arrays[mid]>0&& arrays[mid-1]<0)) {return mid;        }else if (mid>0 &&arrays[mid]>0 && arrays[mid-1]>0) {end = Mid-1;        }else if (arrays[mid]<0 && arrays[mid+1]<0) {start=mid+1; }} return mid;}  /* * Policy mode */class Mincomparatorlist implements comparator<integer>{@Overridepublic int compare (integer O1, integer O2) {return o1>o2?1:-1;}} Class Maxcomparatorlist implements comparator<integer>{@Overridepublic int compare (integer o1, integer o2) { return o1<o2?1:-1;}} public static class Context<t>{private comparator<t> comparator;public Context (comparator<t> Comparator) {this.comparator = comparator;} Public list<t> sortlist (list<t> List) {collections.sort (LISt,comparator); return list;}}} 

There are several pairs of opposite numbers in the computed array

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.