Leetcode - Shortest Word Distance
Leetcode - Shortest Word Distance II

Leetcode - Single Number

violet posted @ Apr 02, 2020 02:52:36 AM in 算法 with tags Algorithm Golang BitManipulation array , 164 阅读

https://leetcode.com/problems/single-number/

Given a non-empty array of integers, every element appears twice except for one. Find that single one.

Note:

Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

Example 1:

Input: [2,2,1]
Output: 1

 

Given a number, use any number to do xor for it twice and it still remains.

So in this array, there are many elements appearing twice and only one single. Running xor for every number, the left one is the single one.

func singleNumber(nums []int) int {
    xor := nums[0]
    for i := 1; i < len(nums); i++ {
        xor ^= nums[i]
    }
    return xor
}
WBBSE 8th Class Syl 说:
Jul 13, 2023 04:03:18 PM

WBBSE 8th Class Syllabus 2024 Pdf File Format helps to get an idea About the Concepts and Topics Taught in class for a Subject During this Academic year 2024, Students will be able to Access the Clickable Download Links From where they WBBSE 8th Class Syllabus 2024 can Download the Syllabus of Bengali, English Medium All Subjects.WBBSE Recently Upload West Bengal Class Syllabus 2024, WBBSE will Organise the 8th Class Public Examinations in April 2024, West Bengal Every Year A Huge Number of Students Appeared for Class Should Check the new Syllabus Details here.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter