删除给定整数的重复数字
我的结果基于以下方法,但我不满意。
我想要其他一些方式和有效的方式来获得预期的输出。
输入:
112341
输出:
1234
不应在输出中显示重复,答案必须为1234
。
我的解决方案:
public class PrintUnique {
public static void main(String[] args) {
int result = printUniquNums(112341);
System.out.println(result);
}
private static int printUniquNums(int num) {
String nums = Integer.toString(num);
char [] ch = nums.toCharArray();
Set<Character> store = new LinkedHashSet<>();
String res = "";
for (int i = 0; i < ch.length; i++){
if (store.add(ch[i])){
res += ch[i];
}
}
return Integer.parseInt(res);
}
}
I have got my result based on below approach, but I'm not satisfied.
I want some other way and efficient manner to get the expected output.
Input:
112341
Output:
1234
No duplicates should be displayed in output and answer must be 1234
.
My solution :
public class PrintUnique {
public static void main(String[] args) {
int result = printUniquNums(112341);
System.out.println(result);
}
private static int printUniquNums(int num) {
String nums = Integer.toString(num);
char [] ch = nums.toCharArray();
Set<Character> store = new LinkedHashSet<>();
String res = "";
for (int i = 0; i < ch.length; i++){
if (store.add(ch[i])){
res += ch[i];
}
}
return Integer.parseInt(res);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
与Java Lambdas和Streams进行了一些调整。您的代码也很好。
A little tweaks with Java lambdas and streams. Your code is also good.
不使用字符串的另一种方法意味着将数字除以10,以供电数字减去一个数字,并在
set set
linkedhashset中从左到右收集每个数字维护插入顺序。然后,使用流来重建不重复数字的数字:输出
是测试代码的链接:
https://www.jdoodle.com/iembed/v0/rsi
Another approach, which doesn't use strings, implies to keep dividing the number by 10 to power the number's digits minus one and collect each digit from left to right in a
Set
, specifically aLinkedHashSet
to maintain the insertion order. Then, using streams to rebuild the number without duplicate digits:Output
Here is a link to test the code:
https://www.jdoodle.com/iembed/v0/rSi
在性能方面,这并不是更有效的。但是,它的代码线较少,更容易阅读的行为更重要。
This is not more effecient in term of performance. But its fewer lines of code and easier to read which in most cases is more important.
您的方法可以使用 stream api 重新进化。
最简单的方法之一是创建一个代码点的流。
然后,我们需要应用
dinters()
确保流元素的唯一性。此操作利用linkedhashset
在引擎盖下拒绝重复并保留初始订单。最后,应用终端操作 cocal> collect> collect() 代码> StringBuilder 作为A 容器用于执行可变减少的。
实现此目的的另一种方法是 map 流元素到
string
类型中,并使用内置 collectorjoning()< /code>将照顾AS的流串联:
output (对于两个解决方案):
链接到在线演示
Your method can be reimplemented using Stream API.
One of the simplest ways to do it is to create a stream of code points.
Then we need to apply
distinct()
to insure uniqueness of the stream elements. This operation utilizes aLinkedHashSet
under the hood to reject duplicates and preserve the initial order.And finally, apply a terminal operation
collect()
by making useStringBuilder
as a container for performing mutable reduction.Another way of achieving this is to map stream elements into
String
type and make use of the built-in collectorjoning()
that will take care of the stream concatenation for as:Output (for both solutions):
A link to Online Demo