微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java如何判断存在重复元素

小编给大家分享一下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如何判断存在重复元素”有了一定的了解,如果想了解更多相关知识,欢迎关注编程之家行业资讯频道,感谢各位的阅读!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐