Problem:
Given an integer, print the next smallest and next largest number that have the same number of 1 bits in their binary representation
Solution from CareerCup book:
public static boolean GetBit(int n, int index) {
return ((n & (1 << index)) > 0);
}
public static int SetBit(int n, int index, boolean b) {
if (b) {
return n | (1 << index);
} else {
int mask = ~(1 << index);
return n & mask;
}
}
public static int GetNext_NP(int n) {
if (n <= 0) return -1;
int index = 0;
int countOnes = 0;
// Find first one.
while (!GetBit(n, index)) index++;
// Turn on next zero.
while (GetBit(n, index)) {
index++;
countOnes++;
}
n = SetBit(n, index, true);
// Turn off previous one
index--;
n = SetBit(n, index, false);
countOnes--;
// Set zeros
for (int i = index - 1; i >= countOnes; i--) {
n = SetBit(n, i, false);
}
// Set ones
for (int i = countOnes - 1; i >= 0; i--) {
n = SetBit(n, i, true);
}
return n;
}
public static int GetPrevious_NP(int n) {
if (n <= 0) return -1; // Error
int index = 0;
int countZeros = 0;
// Find first zero.
while (GetBit(n, index)) index++;
// Turn off next 1.
while (!GetBit(n, index)) {
index++;
countZeros++;
}
n = SetBit(n, index, false);
// Turn on previous zero
index--;
n = SetBit(n, index, true);
countZeros--;
// Set ones
for (int i = index - 1; i >= countZeros; i--) {
n = SetBit(n, i, true);
}
// Set zeros
for (int i = countZeros - 1; i >= 0; i--) {
n = SetBit(n, i, false);
}
return n;
}
分享到:
相关推荐
where y_i is the i'th bit, and the context is the previous i - 1 bits of uncompressed data. 2. PAQ6 MODEL The PAQ6 model consists of a weighted mix of independent submodels which make predictions ...
For many computer vision problems, the most time consuming component consists of nearest neighbor matching in high-dimensional spaces. There are no known exact algorithms for solving these high-...
pattern on the basis of its nearest neighbors in a recorded data set is addressed from the point of view of Dempster-Shafer theory. Each neighbor of a sample to be classified is considered as an item ...
Finally, it uses an eigenvector-based optimization technique to find the low-dimensional embedding of points, such that each point is still described with the same linear combination of its neighbors...
The nearest neighbour (NN) algorithm (a greedy algorithm) lets the salesperson choose the nearest unvisited city as his next move. This algorithm quickly yields an effectively short route. For N ...
Computational Statistics Handbook with MATLAB®, Third Edition covers today’s most commonly used techniques in computational statistics while maintaining the same philosophy and writing style of the ...
During the past decade there has been an explosion in computation and information technology. With it has come vast amounts of data in a variety of fields such as medicine, biology, finance, and ...
(b) Use the same process in (a) with image transformed to YIQ color model and show the reconstructed image in RGB space. [3 images] Compute the PSNR values of the three reconstructed images and ...
neurocomputing and evolutionary optimization brings forward a number of unique features that might be instrumental to the development of concepts and algorithms to deal with big data. This carefully...
improve the speed of approximate nearest neighbor search on the dataset of 1 billion SIFT vectors compared to the best previously published systems, while achieving better recall and incurring only ...
本文主要介绍了最近邻查询和反向最近邻查询,主要介绍了反向最近邻的算法
<br>Dim tSA As SAFEARRAY2D Dim bDib() As Byte <br> ' Get the bits in the from DIB section: With tSA .cbElements = 1 .cDims = 2 .Bounds(0).lLbound = 0 ' Height of ...
<br>Dim tSA As SAFEARRAY2D Dim bDib() As Byte <br> ' Get the bits in the from DIB section: With tSA .cbElements = 1 .cDims = 2 .Bounds(0).lLbound = 0 ' Height of ...
Predict the values of continuous variables using linear regression and K Nearest Neighbors Classify documents and images using logistic regression and support vector machines Create ensembles of ...
Machine learning is currently a vast area of research with applications in a broad range of fields such as computer vision, bioinformatics, information retrieval, natural language processing, audio ...
Predict the values of continuous variables using linear regression and K Nearest Neighbors Classify documents and images using logistic regression and support vector machines Create ensembles of ...
OPTICS and Shared Nearest Neighbour (SNN) algorithms have the capabilities of clustering variable density datasets but they have their own limitations. Both fail to detect overlapping clusters. Also,...
and the Graduate School of the University of Oregon in partial fulfillment of the requirements for the degree of Master of Science March 2009 11 "Effectiveness of Extracting Water Surface Slopes from ...
If you have an aptitude for mathematics and some programming skills, author Joel Grus will help you get comfortable with the math and statistics at the core of data science, and with hacking skills ...