> 文章列表 > Java每日一练(20230413)

Java每日一练(20230413)

Java每日一练(20230413)

目录

1. 子集 II  🌟🌟

2. 快乐数  ※

3. 整数反转  ※

🌟 每日一练刷题专栏 🌟

Golang每日一练 专栏

Python每日一练 专栏

C/C++每日一练 专栏

Java每日一练 专栏


1. 子集 II

给你一个整数数组 nums ,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集)。

解集 不能 包含重复的子集。返回的解集中,子集可以按 任意顺序 排列。

示例 1:

输入:nums = [1,2,2]
输出:[[],[1],[1,2],[1,2,2],[2],[2,2]]

示例 2:

输入:nums = [0]
输出:[[],[0]]

提示:

  • 1 <= nums.length <= 10
  • -10 <= nums[i] <= 10

出处:

https://edu.csdn.net/practice/25450836

代码:

import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;
public class subsetsWithDup {public static class Solution {public List<List<Integer>> subsetsWithDup(int[] nums) {List<List<Integer>> retList = new ArrayList<>();retList.add(new ArrayList<>());if (nums == null || nums.length == 0)return retList;Arrays.sort(nums);List<Integer> tmp = new ArrayList<>();tmp.add(nums[0]);retList.add(tmp);if (nums.length == 1)return retList;int lastLen = 1;for (int i = 1; i < nums.length; i++) {int size = retList.size();if (nums[i] != nums[i - 1]) {lastLen = size;}for (int j = size - lastLen; j < size; j++) {List<Integer> inner = new ArrayList<>(retList.get(j));inner.add(nums[i]);retList.add(inner);}}return retList;}}public static void main(String[] args) {Solution s = new Solution();int[] nums = {1,2,2};System.out.println(s.subsetsWithDup(nums));int[] nums2 = {0};System.out.println(s.subsetsWithDup(nums2));}
}

输出:

[[], [1], [2], [1, 2], [2, 2], [1, 2, 2]]
[[], [0]]


2. 快乐数

编写一个算法来判断一个数 n 是不是快乐数。

「快乐数」定义为:

  • 对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和。
  • 然后重复这个过程直到这个数变为 1,也可能是 无限循环 但始终变不到 1。
  • 如果 可以变为  1,那么这个数就是快乐数。

如果 n 是快乐数就返回 true ;不是,则返回 false 。

示例 1:

输入:19
输出:true
解释:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1

示例 2:

输入:n = 2
输出:false

提示:

  • 1 <= n <= 2^31 - 1

出处:

https://edu.csdn.net/practice/25450837

代码:

import java.util.List;
import java.util.ArrayList;
public class isHappy {public static class Solution {public boolean isHappy(int n) {List<Integer> list = new ArrayList<Integer>();list.add(n);while (n != 1) {int temp = 0;while (n != 0) {temp += (n % 10) * (n % 10);n = n / 10;}n = temp;if (list.contains(n)) {break;} else {list.add(n);}}return n == 1;}}public static void main(String[] args) {Solution s = new Solution();System.out.println(s.isHappy(19));System.out.println(s.isHappy(2));}
}

输出:

true
false


3. 整数反转

给你一个 32 位的有符号整数 x ,返回将 x 中的数字部分反转后的结果。

如果反转后整数超过 32 位的有符号整数的范围 [−231,  231 − 1] ,就返回 0。

假设环境不允许存储 64 位整数(有符号或无符号)。

示例 1:

输入:x = 123
输出:321

示例 2:

输入:x = -123
输出:-321

示例 3:

输入:x = 120
输出:21

示例 4:

输入:x = 0
输出:0

提示:

  • -2^31 <= x <= 2^31 - 1

出处:

https://edu.csdn.net/practice/25450838

代码:

public class reverse {public static class Solution {public int reverse(int x) {long xx = x;long r;long y = 0;boolean sign = xx < 0;while (xx != 0) {r = xx % 10;y = y * 10 + r;if (sign) {xx = (long) Math.ceil(xx / 10);} else {xx = (long) Math.floor(xx / 10);}}return y > Integer.MAX_VALUE || y < Integer.MIN_VALUE ? 0 : (int) y;}}public static void main(String[] args) {Solution s = new Solution();System.out.println(s.reverse(123));System.out.println(s.reverse(-123));System.out.println(s.reverse(120));System.out.println(s.reverse(0));}
}

输出:

321
-321
21
0


🌟 每日一练刷题专栏 🌟

持续,努力奋斗做强刷题搬运工!

👍 点赞,你的认可是我坚持的动力! 

🌟 收藏,你的青睐是我努力的方向! 

评论,你的意见是我进步的财富!  

 主页:https://hannyang.blog.csdn.net/ 

Golang每日一练 专栏

Python每日一练 专栏

C/C++每日一练 专栏

Java每日一练 专栏