Find the Duplicated Number Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.
Example 1:
1 2
Input: [1,3,4,2,2] Output: 2
Example 2:
1 2
Input: [3,1,3,4,2] Output: 3
Note:
You must not modify the array (assume the array is read only). You must use only constant, O(1) extra space. Your runtime complexity should be less than O(n2). There is only one duplicate number in the array, but it could be repeated more than once.
Single Number II Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Values in the traversals pre and post are distinct positive integers.
Example 1:
1 2
Input: pre = [1,2,4,5,3,6,7], post = [4,5,2,6,7,3,1] Output: [1,2,3,4,5,6,7]
Note:
1 2 3
1 <= pre.length == post.length <= 30 pre[] and post[] are both permutations of 1, 2, ..., pre.length. It is guaranteed an answer exists. If there exists multiple answers, you can return any of them.
/* The purpose of this function is to convert an unsigned binary number to reflected binary Gray code.
The operator >> is shift right. The operator ^ is exclusive or. */ unsignedintbinaryToGray(unsignedint num) { return (num >> 1) ^ num; }
/* The purpose of this function is to convert a reflected binary Gray code number to a binary number. */ unsignedintgrayToBinary(unsignedint num) { unsignedint mask; for (mask = num >> 1; mask != 0; mask = mask >> 1) { num = num ^ mask; } return num; }