当前位置:网站首页>Leetcode advanced road - 136 A number that appears only once
Leetcode advanced road - 136 A number that appears only once
2022-06-10 21:22:00 【Li_ XiaoJin】
Given an array of non-empty integers , Except that an element only appears once , Each of the other elements occurs twice . Find the element that only appears once .
explain :
Your algorithm should have linear time complexity . Can you do this without using extra space ?
Example 1:
Input : [2,2,1]
Output : 1
Example 2:
Input : [4,1,2,1,2]
Output : 4
My mind only thinks of violent solutions , After reading the solution, I found that bit operation can be used , That's clever .
/**
* 136. A number that appears only once
* @Author: lixj
* @Date: 2020/9/21 10:19
*/
public class SingleNumber {
/**
* Violence law
* @param nums
* @return
*/
public int singleNumber(int[] nums) {
if (nums.length ==1) return nums[0];
Arrays.sort(nums);
if (nums[0] != nums[1]) return nums[0];
if (nums[nums.length-1] != nums[nums.length-2]) return nums[nums.length-1];
for (int i = 1; i < nums.length-1; i++) {
if (nums[i-1] != nums[i] && nums[i] != nums[i+1]) {
return nums[i];
}
}
return 0;
}
/**
* An operation
* @param nums
* @return
*/
public int singleNumber1(int[] nums) {
int single = 0;
for (int num : nums){
single ^= num;
}
return single;
}
public static void main(String[] args) {
int[] nums = new int[]{2,4,3,2,3};
// int[] nums = new int[]{2,2,1};
// int[] nums = new int[]{1};
SingleNumber singleNumber = new SingleNumber();
System.out.println(singleNumber.singleNumber1(nums));
}
}
Copyright: use Creative Commons signature 4.0 International license agreement to license Links:https://lixj.fun/archives/leetcode Advanced road -136 A number that appears only once
边栏推荐
- Self attention and multi head attention
- 视频监控系统存储控件,带宽计算方法
- Leetcode advanced path - the first unique character in a string
- 自注意力(self-attention)和多头注意力(multi-head attention)
- Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading
- Redis cache penetration
- MySQL service startup failed
- Finally, someone explained the difference among cookies, sessions and tokens. Detailed explanation, interview questions.
- 蛮力法/任务分配
- pdf. Js----- JS parse PDF file to realize preview, and obtain the contents in PDF file (in array form)
猜你喜欢

六级考试-商务英语-考前最后一背

20192407 2021-2022-2 experimental report on Experiment 8 of network and system attack and Defense Technology

CET-6 - Business English - the last recitation before the test

35岁被裁员,还能拥有美妙人生吗?

一、Vulkan开发理论基础知识

^30h5 web worker multithreading

异步、线程池(CompletableFuture)

数据库系统概论 ---- 第一章 -- 绪论(重要知识点)

Nanny tutorial: how to become a contributor to Apache linkis documents

标配双安全气囊,价格屠夫长安Lumin 4.89万起售
随机推荐
Zero trust architecture
标配双安全气囊,价格屠夫长安Lumin 4.89万起售
leetcode 划分数组使最大差为 K
Talk about server performance optimization ~ (recommended Collection)
编程式导航路由跳转到当前路由(参数不变), 多次执行会抛出NavigationDuplicated的警告错误?
LeetCode 进阶之路 - 搜索插入位置
Niuke.com: numbers that appear more than half of the times in the array
Kcon 2022 topic public selection is hot! Don't miss the topic of "favorite"
^30H5 Web Worker多线程
你的公司会选择开发数据中台吗?
牛客网:数组中出现次数超过一半的数字
Uncover secrets: how can wechat red envelopes in the Spring Festival Gala resist 10billion requests?
LeetCode:497. Random points in non overlapping rectangles -- medium
LeetCode 进阶之路 - 反转字符串
蛮力法/1~n的全排列 v3 递归
app測試用例
Brute force method / task assignment
Redis缓存雪崩
Leetcode advanced path - the first unique character in a string
Use DAP link to download the executable file separately to the mm32f5 microcontroller