Find the elements in the set that appears only once
2011-05-16 10:42
351 查看
given a set and each element in the set will appear twice. so how to find the elements that only appear once.
1. Use a bitmap and traverse the set. exclusive xor the value of bitmap at index with value of element in the set by 1. then if the element appears twice then the value will be zero and if the element appears only once then the value will be 1. trasverse
the bitmap then if the value is 1 at some index then the value represented by the index only appears once. pros. each to implement. cons. need a huge amount of space to store the bitmap.
2. Still use exclusive xor operation. we traverse the set and each time we meet a element we exclusive xor it to the final result. if the element appears twice then the element will have no influence on the final result because the result of that two same
element xor each other is zero. and the element that appears only once will have influence on the final result.
pros. only need one storage unit to store the final result. cons. only can find one element that appears once. if there are more than one element in the set that appears only once we cannot find them together.
3. In terms of invariant. the set is predefined then the sum of the set is fixed that we call it a invariant. if a element is missing in the set then the difference from the invariant is the missing element. the method still can only find the element in
the set where only one element appears once. if there are more than one element in the set that appears only once we cannot find them. if we need find them then we need more information. we can use multiplication of the element in the set as another invariant.
and quotient of dividing invariant by muplication of the element in the set is the multiplication value of two missing element in the set. now we have two equation and two variants: x+y=a,xy=b then we can acquire a and b.
Summary
The problem can be abstracted as the problem: given a set and element in the set is within the predefined range. so now we know there is one or two elements missing in the set then how to find them.
1. xor is very useful operation. because if two elements with same value xor each other the result is zero. and if an element xor zero the result is itself. it is very useful to test whether two element are equal.
2. invariant is also very useful. a predefined set is a invariant. the missing element is the difference from the invariant, through which we can find the missing elements.
Above methods are extracted from chapter 1.5 in "beauty of programming"
1. Use a bitmap and traverse the set. exclusive xor the value of bitmap at index with value of element in the set by 1. then if the element appears twice then the value will be zero and if the element appears only once then the value will be 1. trasverse
the bitmap then if the value is 1 at some index then the value represented by the index only appears once. pros. each to implement. cons. need a huge amount of space to store the bitmap.
2. Still use exclusive xor operation. we traverse the set and each time we meet a element we exclusive xor it to the final result. if the element appears twice then the element will have no influence on the final result because the result of that two same
element xor each other is zero. and the element that appears only once will have influence on the final result.
pros. only need one storage unit to store the final result. cons. only can find one element that appears once. if there are more than one element in the set that appears only once we cannot find them together.
3. In terms of invariant. the set is predefined then the sum of the set is fixed that we call it a invariant. if a element is missing in the set then the difference from the invariant is the missing element. the method still can only find the element in
the set where only one element appears once. if there are more than one element in the set that appears only once we cannot find them. if we need find them then we need more information. we can use multiplication of the element in the set as another invariant.
and quotient of dividing invariant by muplication of the element in the set is the multiplication value of two missing element in the set. now we have two equation and two variants: x+y=a,xy=b then we can acquire a and b.
Summary
The problem can be abstracted as the problem: given a set and element in the set is within the predefined range. so now we know there is one or two elements missing in the set then how to find them.
1. xor is very useful operation. because if two elements with same value xor each other the result is zero. and if an element xor zero the result is itself. it is very useful to test whether two element are equal.
2. invariant is also very useful. a predefined set is a invariant. the missing element is the difference from the invariant, through which we can find the missing elements.
Above methods are extracted from chapter 1.5 in "beauty of programming"
相关文章推荐
- Find the element that appears once
- Find the element that appears once(查找出现一次的元素)
- appears more than once in the keychain The codesign tool requires there only be one
- Find the element that appears once
- Find the element that appears once
- Find out all the elements in A and B such that the A[i]-B[j]=C[k]
- 代码面试题:Find the first element in a given string A that also appears in another given string B
- Find the Smallest K Elements in an Array
- CareerCup Find the biggest interval that has all its members in list in O(n)
- find all pairs of elements in a balanced BST that sum to a certain number
- How-to find the SQL that using lots of temp tablespace in Oracle
- Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive
- vs2005下出现“Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive”的解决方法
- How Many Elements Are in the Power Set?
- 1. 找出数组中的单身狗OddOccurrencesInArray Find value that occurs in odd number of elements.
- CareerCup Finds all the elements that appear more than n/3 times in the list
- CodeSign error: Certificate identity 'iPhone Developer:xxx appears more than once in the keychain.
- Stored procedure 'zhanghouying' may be run only in unchained transaction mode. The 'SET CHAINED OFF' command will cause the curr
- Find the two non-repeating elements in an array of repeating elements
- Here's an example that shows how to enumerate through the features of a selection set, returning the value of each field in the attribute table except for the geometry colum.