当前位置: 首页 > news >正文

Day48 | 657. 机器人能否返回原点、31. 下一个排列、463. 岛屿的周长、1356. 根据数字二进制下 1 的数目排序

657. 机器人能否返回原点

题目链接:657. 机器人能否返回原点 - 力扣(LeetCode)

题目难度:简单

代码:

class Solution {
    public boolean judgeCircle(String moves) {
        int x = 0;
        int y = 0;
        for (char c : moves.toCharArray()) {
            if (c == 'U') y++;
            if (c == 'D') y--;
            if (c == 'L') x++;
            if (c == 'R') x--;
        }
        return x == 0 && y == 0;
    }
}

31. 下一个排列

题目链接:31. 下一个排列 - 力扣(LeetCode)

题目难度:中等

代码:

class Solution {
    public void nextPermutation(int[] nums) {
        for (int i = nums.length - 1; i >= 0; i--) {
            for (int j = nums.length - 1; j > i; j--) {
                if (nums[j] > nums[i]) {
                    // 交换
                    int temp = nums[i];
                    nums[i] = nums[j];
                    nums[j] = temp;
                    // [i + 1, nums.length) 内元素升序排序
                    Arrays.sort(nums, i + 1, nums.length);
                    return;
                }
            }
        }
        Arrays.sort(nums); 
    }
}

463. 岛屿的周长

题目链接:463. 岛屿的周长 - 力扣(LeetCode)

题目难度:简单

代码:

class Solution {
    public int islandPerimeter(int[][] grid) {
        int landSum = 0; // 陆地数量 
        int cover = 0; // 相邻陆地数量
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == 1) {
                    landSum++;
                    // 统计上面和左边的相邻陆地
                    if(i - 1 >= 0 && grid[i-1][j] == 1) cover++;
                    if(j - 1 >= 0 && grid[i][j-1] == 1) cover++;
                }
            }
        }
        return landSum * 4 - cover * 2;
    }
}

1356. 根据数字二进制下 1 的数目排序

题目链接:1356. 根据数字二进制下 1 的数目排序 - 力扣(LeetCode)

题目难度:简单

代码:

class Solution {
    private int cntInt(int val){
        int count = 0;
        while(val > 0) {
            val = val & (val - 1);
            count ++;
        }

        return count;
    }

    public int[] sortByBits(int[] arr) {
      return Arrays.stream(arr).boxed()
            .sorted(new Comparator<Integer>(){
                @Override
                public int compare(Integer o1, Integer o2) {
                    int cnt1 = cntInt(o1);
                    int cnt2 = cntInt(o2);
                    return (cnt1 == cnt2) ? Integer.compare(o1, o2) : Integer.compare(cnt1, cnt2);
                }
            })
            .mapToInt(Integer::intValue)
            .toArray();
    }
}

相关文章:

  • 嵌入式系统简介
  • PH热榜 | 2025-03-31
  • MTU | 检测 / 设置 / 相关问题解析
  • 【JavaEE进阶】MyBatis(4)-完善图书管理系统
  • ros2--xacro
  • uniapp中如何用iconfont来管理图标集成到我们开发的项目中
  • 基于AT89C52单片机的轮胎压力监测系统
  • 学习threejs,使用THREE.ImageUtils.loadTexture加载纹理贴图
  • C++11:包装器(适配器模式)
  • TCP/IP协议的应用层与传输层
  • 【PythonRS】哨兵2号(Sentinel2)轨道条带说明+图幅统计(表格+矢量)
  • 51单片机的五类指令(四)——控制转移类指令
  • ai画图esrgan放大算法。
  • HTB-Code
  • AWS中S3的使用
  • Python Cookbook-4.18 搜集命名的子项
  • SAPIEN 仿真环境下的 pose
  • 告别失联!北斗三号多模对讲机TD70破除无网通信难题
  • go中锁的入门到进阶使用
  • 【算法day27】有效的数独——请你判断一个 9 x 9 的数独是否有效。只需要 根据以下规则 ,验证已经填入的数字是否有效即可。
  • 网站流量平台/如何进行网络营销策划
  • 用ppt做网站方法/厦门seo服务
  • 建设一个企业网站多少钱/企业培训课程视频
  • 哪里做网站需求/想做网络推广的公司
  • 学校网站建设及使用档案/2023重大新闻事件10条
  • 聚美网站开发开题报告/网络营销环境宏观微观分析