温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java如何判断存在重复元素

发布时间:2022-01-17 13:46:00 来源:亿速云 阅读:102 作者:小新 栏目:大数据

小编给大家分享一下java如何判断存在重复元素,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

给定一个整数数组,判断是否存在重复元素。

如果任何值在数组中出现至少两次,函数返回 true。如果数组中每个元素都不相同,则返回 false。

示例 1:

输入: [1,2,3,1]
输出: true

示例 2:

输入: [1,2,3,4]
输出: false

示例 3:

输入: [1,1,1,3,3,4,3,2,4,2]
输出: true

上期的问题是:157,反转链表

1public ListNode reverseList(ListNode head) {
2    if (head == null || head.next == null)
3        return head;
4    ListNode tempList = reverseList(head.next);
5    head.next.next = head;
6    head.next = null;
7    return tempList;
8}

解析:

链表反转,这是个老生常谈的问题了,其实方法非常多,下面再来看两个

 1public ListNode reverseList(ListNode head) {
2    ListNode pre = null;
3    while (head != null) {
4        ListNode next = head.next;
5        head.next = pre;
6        pre = head;
7        head = next;
8    }
9    return pre;
10}
11
12
13public ListNode reverseList(ListNode head) {
14    return reverseListInt(head, null);
15}
16
17private ListNode reverseListInt(ListNode head, ListNode newHead) {
18    if (head == null)
19        return newHead;
20    ListNode next = head.next;
21    head.next = newHead;
22    return reverseListInt(next, head);
23}

看完了这篇文章,相信你对“java如何判断存在重复元素”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI