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 ...
However, a critical aspect of successfully applying the weighted K-NN algorithm lies in choosing the optimal number of nearest neighbors \( k \), the weight vector \( \alpha \), and the distance ...
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 ...
(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 ...
两种重要的查询类型是最近邻(Nearest Neighbor, NN)查询和反向最近邻(Reverse Nearest Neighbor, RNN)查询。NN查询旨在找到距离给定查询点最近的数据点,而RNN查询则是找出所有将给定查询点视为其最近邻的数据点...
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 ...
### 关键知识点解析 #### 一、概览与背景 - **高维空间中的最近邻搜索**:在计算机视觉领域,最耗时的部分通常涉及在高维空间中进行最近邻匹配。 - **精确算法的局限性**:对于解决高维问题,目前尚未找到比线性...
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 ...
• The main challenges of Machine Learning, in particular underfitting and overfitting (the bias/variance tradeoff). • Reducing the dimensionality of the training data to fight the curse of ...