(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;
    }

}

原文地址:https://www.cnblogs.com/codingyangmao/p/11429087.html

时间: 2024-10-09 20:45:59

(Easy) Valid Boomerang - LeetCode的相关文章

(Easy) Valid Palindrome -LeetCode

Description: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. Note: For the purpose of this problem, we define empty string as valid palindrome. Example 1: Input: "A man, a plan, a canal: P

Valid Number leetcode java

题目: Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true Note: It is intended for the problem statement to be ambigu

Valid Palindrome leetcode java

题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome. Note: Have you consider

Valid Sudoku leetcode java

题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note: A valid Sudoku board (parti

Longest Valid Parentheses leetcode java

题目: Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example

Valid Parentheses leetcode java

题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]&

Valid Parentheses [LeetCode 20]

1- 问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "(

Valid Parentheses -- leetcode

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]"

Valid Sudoku leetcode

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. 判断九宫格的合理性(并不一定有解),只需要依次判断行.列.9个子九宫格是否