• day006


    Nim游戏

    292. Nim 游戏

    你和你的朋友,两个人一起玩 Nim 游戏

    • 桌子上有一堆石头。
    • 你们轮流进行自己的回合, 你作为先手 
    • 每一回合,轮到的人拿掉 1 - 3 块石头。
    • 拿掉最后一块石头的人就是获胜者。

    假设你们每一步都是最优解。请编写一个函数,来判断你是否可以在给定石头数量为 n 的情况下赢得游戏。如果可以赢,返回 true;否则,返回 false 。

    1. class Solution {
    2. public boolean canWinNim(int n) {//博弈论,找规律
    3. return n%4!=0;
    4. }
    5. }

     

    文物朝代判断

    1. class Solution {
    2. public boolean checkDynasty(int[] places) {
    3. int unkown=0;
    4. Arrays.sort(places);
    5. for(int i=0;i<4;i++){
    6. if(places[i]==0)
    7. unkown++;
    8. else if(places[i]==places[i+1])
    9. return false;
    10. }
    11. return places[4]-places[unkown] <5;//精髓
    12. }
    13. }

    最大三角形面积

    812. 最大三角形面积

    给你一个由 X-Y 平面上的点组成的数组 points ,其中 points[i] = [xi, yi] 。从其中取任意三个不同的点组成三角形,返回能组成的最大三角形的面积。与真实值误差在 10-5 内的答案将会视为正确答案

    1. class Solution {
    2. public double getLength(int[] a, int[] b) {
    3. return Math.sqrt(Math.pow(a[0] - b[0], 2) + Math.pow(a[1] - b[1], 2));
    4. }
    5. public double get_area(int[] point1, int[] point2, int[] point3) {
    6. double a = getLength(point1, point2);
    7. double b = getLength(point1, point3);
    8. double c = getLength(point2, point3);
    9. double p = (a + b + c) / 2;
    10. return Math.sqrt(p * (p - a) * (p - b) * (p - c));
    11. }
    12. public double largestTriangleArea(int[][] points) {
    13. //无脑海伦公式,直接秒解问题
    14. double max = 0;
    15. for (int i = 0; i < points.length; i++) {
    16. for (int j = 0; j < points.length; j++) {
    17. for (int k = 0; k < points.length; k++) {
    18. if (i == j || i == k || j == k) continue;
    19. double p = get_area(points[i], points[j], points[k]);
    20. if (p > max)
    21. max = p;
    22. }
    23. }
    24. }
    25. return max;
    26. }
    27. }

  • 相关阅读:
    【无标题】
    在Python中求解定积分scipy.integrate.quad()方法
    028.Python面向对象_类&补充_元类
    基于yolov7与arduino的眼睛跟随模块
    尚硅谷设计模式(十九)迭代器模式
    macOS系统查找被占用的端口号的操作
    用户行为分析-阿里日志服务
    2311鸿蒙文档中文目录版
    基于 Glibc 版本升级的 DolphinDB 数据查询性能优化实践
    Actor 生命周期
  • 原文地址:https://blog.csdn.net/qq_62074445/article/details/133749626