当前位置:网站首页>HJ9 提取不重复的整数
HJ9 提取不重复的整数
2022-07-25 22:54:00 【敏敏程序媛】
描述
输入一个 int 型整数,按照从右向左的阅读顺序,返回一个不含重复数字的新的整数。
保证输入的整数最后一位不是 0 。
数据范围: 1 \le n \le 10^{8} \1≤n≤108
输入描述:
输入一个int型整数
输出描述:
按照从右向左的阅读顺序,返回一个不含重复数字的新的整数
示例1
输入:9876673
输出:37689
import java.util.Scanner;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
/**
*Author:向敏
*Date:2022/07/22 15:22
*/
public class Main
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext())
{
int input = scanner.nextInt();
System.out.println(getInteger(input+""));
}
scanner.close();
}
//提取不重复的整数
private static String getInteger(String numStr)
{
StringBuilder builder = new StringBuilder();
HashSet<Character> set = new LinkedHashSet<>();
//把整数倒序输入链表集合中,可以去重
for (int i = numStr.length() - 1; i >= 0;i--)
{
set.add(numStr.charAt(i));
}
//把集合中的整数输入到字符串容器中
for (Character c : set)
{
builder.append(c);
}
//如果第一个字母是0
if (builder.charAt(0) == '0')
{
return builder.substring(1,builder.length());
}
return builder.toString();
}
}边栏推荐
- 贴片微型滚珠振动开关的结构原理
- PE format: analyze and implement IATHOOK
- Express framework
- [training Day12] x equation [high precision] [mathematics]
- Oracle query statement summary
- Deploy flash based websites using Google cloud
- invalid syntax
- DOM event binding
- Recommend short videos every week: more and more smart devices need collaboration, posing a greater challenge to the development of the Internet of things?
- Network Security Learning (XV) ARP
猜你喜欢

【论文笔记】基于在线预测和规划的机器人动态跟踪抓取方法

MatrixCube揭秘102——300行实现的完整分布式存储系统MatrixKV

Qt中文编程遇C2001错误,提示“常量中有换行符”

Anaconda~Upload did not complete.

Day006 select structure (if statement exercise)

invalid syntax

Network Security Learning (XIV) IP protocol

Network Security Learning (11) scanning and blasting

Qt的TQTreeWidget控件

【MySQL提权】UDF提权(附带大马)
随机推荐
每周推荐短视频:需要协同的智能设备越来越多,给物联网开发提出更大挑战?
IPFs of Internet Protocol
Today, learn about the use of lists, hyperlinks, image tags, and audio and video
Kibana~后台启动Kibana之后无法找到进程号
What are the differences between FileInputStream and bufferedinputstream?
SSH服务器CBC加密模式漏洞(CVE-2008-5161)
MySQL data type
Opencv compile and call GPU version
Anaconda~Upload did not complete.
【MySQL提权】UDF提权(附带大马)
Array中可以用泛型吗
QVariant的使用
校验码生成
[training Day11] Nescafe [greed]
Session and cookie, token and storage
Naming rules of software test pytest pytest the pre and post confitest of use cases Py customized allure report @pytest.mark.parameter() decorator as data-driven
QT的Tree View Model示例
Summary of common methods of string:
Network Security Learning (XIV) IP protocol
The difference between "= =" and equals