(Easy) Valid Boomerang - LeetCode

Description:

A boomerang is a set of 3 points that are all distinct and not in a straight line.

Given a list of three points in the plane, return whether these points are a boomerang.

 

Example 1:

Input: [[1,1],[2,3],[3,2]]
Output: true

Example 2:

Input: [[1,1],[2,2],[3,3]]
Output: false

 

Note:

  1. points.length == 3
  2. points[i].length == 2
  3. 0 <= points[i][j] <= 100
 
Accepted
9,468
Submissions
25,229

 

 

Solution:

class Solution {
   public boolean isBoomerang(int[][] points) {
        
       if(points ==null||points.length==0 ||points[0].length==0)
        return false;
        
       boolean line = true;
       boolean vert = true;
       for(int i =0; i<points.length-1; i++){
           if(points[i][1]!=points[i+1][1]){
               line = false;
                
           }
           
           if(points[i][0]!=points[i+1][0]){
               vert = false;
           }
       }
       
       
        List<Float> list = new ArrayList<Float>();
        float y=  0;
        float x = 0;
        float k = 0;
        List<String> dup_list = new ArrayList<String>();
       
        for(int i =0; i<points.length; i++){
            boolean zero_flag = false;
            boolean yuan_dian_check= false;
            String tmp ="";
            for(int j = 0; j<points[i].length; j++){
                 tmp = tmp+Integer.toString(points[i][j]);
                if(j==0){
                   
                    if(points[i][j]==0){
                        
                        zero_flag =true;
                         
                    }
                    else{
                        
                        x= points[i][j]-0;
                    }
                }
                
                else{
                  
                    if(zero_flag&&points[i][j]==0){
                        
                       yuan_dian_check = true;
                    }
                    y = points[i][j] - 0;
                    
                    
                }
                
                if(zero_flag){
                k = Integer.MAX_VALUE;
            }
            else{
                k = y/x; 
            }
            
            }
            
            if(dup_list.contains(tmp)){
                return false;
            }
            else{
               
                dup_list.add(tmp);
            }
            if(!yuan_dian_check){
                if(!list.contains(k)){
                   
                    list.add(k);
                }
               
            }
        } 
       
       if(list.size()<=1||vert||line){
           return false;
       }
       return true;
    }
    
}

 

Guess you like

Origin www.cnblogs.com/codingyangmao/p/11429087.html