share my java solution~~~~~~~~~


  • 0
    T
    public class Solution {
        public boolean isConvex(List<List<Integer>> points) {
            if(points==null || points.size()<3){
                return false;
            }
            
            if(points.size()==3){
                return true;
            }
            
            boolean check=true;
            int len=points.size();
            int p=compute_cross_product(points.get(len-1),points.get(0),points.get(1));
            
            for(int i=1;i<len;i++){
                int cross1=compute_cross_product(points.get((i-1+len)%len),points.get(i),points.get((i+1)%len));
                
                if((long)cross1*(long)p<0){
                    return false;
                }
            }
            return true;
        }
        
        public int compute_cross_product(List<Integer> a,List<Integer> b,List<Integer> c){
               int[] array1=new int[]{a.get(0)-b.get(0),a.get(1)-b.get(1)};
               int[] array2=new int[]{c.get(0)-b.get(0),c.get(1)-b.get(1)};
               
               int cross_product=array1[0]*array2[1]-array1[1]*array2[0];
               return cross_product;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.