HJ56、HJ58、JZ4、JZ6、JZ15、JZ17几道题


补上这几天的每日一题

HJ56 完全数计算

题源 👉 完全数计算_牛客题霸_牛客网 (nowcoder.com)

题目描述:

image-20230123224900660

具体实现:

方法一:

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int cnt = 0;
        while (n > 0){
            int sum = 0;
            for (int i = 1; i < n; i++){
                if (n % i == 0) sum += i;
                if (sum > n) break;
            }
            if (sum == n){
                cnt++;
            }
            n--;
        }
        System.out.println(cnt);
    }
}

HJ58 输入n个整数,输出其中最小的k个

题源 👉 输入n个整数,输出其中最小的k个_牛客题霸_牛客网 (nowcoder.com)

题目描述:

image-20230125215550045

具体实现:

方法一:

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] t = new int[n];
        for(int i = 0; i < n; i++)
            t[i] = sc.nextInt();
        Arrays.sort(t);	// 快排
        for(int i = 0; i < k; i++)
            System.out.print(t[i] + " ");
    }
}

时间复杂度:O(nlog 2 n)

空间复杂度:O(n)

JZ4 二维数组中的查找

题源 👉 二维数组中的查找_牛客题霸_牛客网 (nowcoder.com)

题目描述:

image-20230125223251053

image-20230125223312426

具体实现:

方法一:暴力法

public class Solution {
    public boolean Find(int target, int [][] array) {     
        int n = array.length;   // 行数
        int m = array[0].length;    // 列数        
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
                if(array[i][j] == target)   
                    return true;
        return false;
    }
}

方法二:从左下开始找

从左下开始,若当前元素<目标元素,右移;若>则上移。

若跃出边界仍未找到,返回false。

image-20230126220206068
图源官方解析
public class Solution {
    public boolean Find(int target, int [][] array) {     
        int n = array.length;   // 行数
        int m = array[0].length;    // 列数        
        if(n == 0) return false;
        if(m == 0) return false;

        for(int i = n - 1, j = 0; i >= 0 && j < m; ){
            if(array[i][j] == target) return true;
            else if(array[i][j] < target) j++;
            else i--;
        }

        return false;
    }
}

JZ6 从尾到头打印链表

题源 👉 从尾到头打印链表_牛客题霸_牛客网 (nowcoder.com)

题目描述:

image-20230126224311328

具体实现:

方法一:

  • 题目要求遍历单链表并从尾到头输出,即 “先进后出”(栈)。

  • ArrayList 中有 add(index, value) 方法,即将value放在index位置。 可用来实现先进后出。

/**
*    public class ListNode {
*        int val;
*        ListNode next = null;
*
*        ListNode(int val) {
*            this.val = val;
*        }
*    }
*
*/
import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> list = new ArrayList<>();
        ListNode p = listNode;
        while(p != null){
            list.add(0, p.val);
            p = p.next;
        }
        return list;
    }
}

JZ15 二进制中1的个数

题源 👉 二进制中1的个数_牛客题霸_牛客网 (nowcoder.com)

题目描述:

image-20230126225500816

具体实现:

方法一:位数检查法

与 1 的二进制相与,与完后右移一位继续和 1 与,直至检查完32位。

public class Solution {
    public int NumberOf1(int n) {
        int cnt = 0;
        for(int i = 0; i < 32; i++)
            cnt += ((n >> i) & 1);
        return cnt;
    }
}

// 改进
public class Solution {
    public int NumberOf1(int n) {
        int cnt = 0;
        while(n != 0){
            if((n & 1) == 1) cnt++;	 // 写成 cnt += (n & 1)也行
            n >>>= 1;
        }
        return cnt;
    }
}

方法二:lowbit

方法三:分组统计

该方法来自 👉 【宫水三叶の剑指精选】一题四解:「位数检查」&「右移统计」&「lowbit」&「分组统计」_牛客博客 (nowcoder.net)

image-20230126233553828

image-20230126233712561

JZ17 打印从1到最大的n位数

题源 👉 打印从1到最大的n位数_牛客题霸_牛客网 (nowcoder.com)

题目描述:

image-20230126233858523

具体实现:

方法一:暴力法捏

public int[] printNumbers (int n) {
    int m = 1;
    while(n-- > 0) m *= 10;
    int[] ans = new int[m - 1];
    for(int i = 1; i < m; i++) ans[i - 1] = i;
    return ans; 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值