Zheaoli / do-something-right

MIT License
37 stars 3 forks source link

2022-05-15 #238

Open Zheaoli opened 2 years ago

Zheaoli commented 2 years ago

2022-05-15

SaraadKun commented 2 years ago

812. 最大三角形面积

image

class Solution {
    public double largestTriangleArea(int[][] points) {
        int n = points.length;
        double ans = 0.0d;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                for (int k = j + 1; k < n; k++) {
                    ans = Math.max(ans, halfCross(points[i], points[j], points[k]));
                }
            }
        }
        return ans;
    }

    //a点视为原点,计算向量ab和向量ac的叉乘
    private double halfCross(int[] a, int[] b, int[] c) {
        return 0.5d * Math.abs((b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]));
    }
}

WeChat:Saraad

SaraadKun commented 2 years ago

5234. 移除字母异位词后的结果数组

image

class Solution {
    public List<String> removeAnagrams(String[] words) {
        List<String> ans = new ArrayList<>();
        char[] pre = null;
        for(String word: words) {
            char[] cur = word.toCharArray();
            Arrays.sort(cur);
            if (!Arrays.equals(pre, cur)) {
                ans.add(word);
            }
            pre = cur;
        }
        return ans;
    }
}

WeChat:Saraad

SaraadKun commented 2 years ago

6064. 不含特殊楼层的最大连续楼层数

image

class Solution {
    public int maxConsecutive(int bottom, int top, int[] special) {
        Arrays.sort(special);
        int ans = 0;
        int lo = bottom - 1, hi = 0;
        for (int s : special) {
            hi = s;
            ans = Math.max(ans, hi - lo - 1);
            lo = hi;
        }
        return Math.max(ans, top - lo);
    }
}

WeChat:Saraad

SaraadKun commented 2 years ago

6065. 按位与结果大于零的最长组合

image

class Solution {
    public int largestCombination(int[] candidates) {
        int ans = 0;
        for (int i = 0; i <= 24; i++) {
            int n = 0;
            for (int candidate : candidates) {
                n += (candidate & 1 << i) > 0 ? 1 : 0;
            }
            ans = Math.max(ans, n);
        }
        return ans;
    }
}

WeChat:Saraad

dreamhunter2333 commented 2 years ago
package main

import (
    "fmt"
    "math"
)

/*
 * @lc app=leetcode.cn id=812 lang=golang
 *
 * [812] 最大三角形面积
 */

// @lc code=start
func largestTriangleArea(points [][]int) float64 {
    res := 0.0
    n := len(points)
    for i := 0; i < n-2; i++ {
        for j := 1; j < n-1; j++ {
            for k := 2; k < n; k++ {
                x1, y1 := points[i][0], points[i][1]
                x2, y2 := points[j][0], points[j][1]
                x3, y3 := points[k][0], points[k][1]
                cur := math.Abs(float64(x1*y2+x2*y3+x3*y1-x1*y3-x2*y1-x3*y2)) / 2
                res = math.Max(res, cur)
            }
        }
    }
    return res
}

// @lc code=end
func main() {
    fmt.Println(largestTriangleArea([][]int{{0, 0}, {0, 1}, {1, 0}, {0, 2}, {2, 0}}))
}

微信id: 而我撑伞 来自 vscode 插件