Merge branch 'master' of https://github.com/anebz/ctci
This commit is contained in:
commit
706d56f65d
|
|
@ -0,0 +1,57 @@
|
||||||
|
# 5.5. Debugger
|
||||||
|
|
||||||
|
> Explain what the following code does: (( n & (n-1)) == 0)
|
||||||
|
|
||||||
|
```c++
|
||||||
|
00000010 (n)
|
||||||
|
& 00000001 (n - 1)
|
||||||
|
----------
|
||||||
|
00000000
|
||||||
|
```
|
||||||
|
|
||||||
|
n should be odd. And if it's bigger than 2, it'll have some 1s higher up. Which won't be cleared when doing n-1. so n must be 2.
|
||||||
|
|
||||||
|
## Hints
|
||||||
|
|
||||||
|
> Start with a brute force solution. Can you try all possibilities?
|
||||||
|
|
||||||
|
n=2, but there must be something more.
|
||||||
|
|
||||||
|
> What does it mean if A & B == 0?
|
||||||
|
|
||||||
|
That A and B don't have any same bits in any position.
|
||||||
|
|
||||||
|
> If A & B == 0, then it means that A and B never have a 1 at the same spot. Apply this to the equation in the problem.
|
||||||
|
|
||||||
|
A and B are separated by 1 bit in this case, A = B + 1. It could be all 0s and 10 in the end (n=2), or 100000. So that n = pow(2,x). A power of 2.
|
||||||
|
|
||||||
|
```c++
|
||||||
|
10000000 (n)
|
||||||
|
& 01111111 (n - 1)
|
||||||
|
----------
|
||||||
|
00000000
|
||||||
|
```
|
||||||
|
|
||||||
|
> If ( n & ( n-1)) == 0, then this means that n and n - 1 never have a 1 in the same spot. Why would that happen?
|
||||||
|
|
||||||
|
When n is a power of 2.
|
||||||
|
|
||||||
|
> What is the relationship between how n looks and how n - 1 looks? Walk through a binary subtraction.
|
||||||
|
|
||||||
|
Yes.
|
||||||
|
|
||||||
|
> When you do a binary subtraction, you flip the rightmost 0s to a 1, stopping when you get to a 1 (which is also flipped). Everything (all the 1 s and Os) on the left will stay put.
|
||||||
|
|
||||||
|
Same answer.
|
||||||
|
|
||||||
|
> Picture n and n - 1. To subtract 1 from n, you flipped the rightmost 1 to a 0 and all the 0s on its right to 1s. If n & n -1 == 0, then there are no 1 s to the left of the first 1. What does that mean about n?
|
||||||
|
|
||||||
|
I'm getting lost.
|
||||||
|
|
||||||
|
> We know that n must have only one 1 ifn & ( n -1) == 0. What sorts ofnumbers have only one 1?
|
||||||
|
|
||||||
|
Powers of 2! Or the binary base number.
|
||||||
|
|
||||||
|
## Solution
|
||||||
|
|
||||||
|
Correct! n = a power of 2, or n=0.
|
||||||
|
|
@ -0,0 +1,32 @@
|
||||||
|
# 5.6. Conversion
|
||||||
|
|
||||||
|
> Write a function to determine the number of bits you would need to flip to convert integer A to integer B
|
||||||
|
|
||||||
|
Input: 29 (or: 11101), 15 (or: 01111)
|
||||||
|
Output: 2
|
||||||
|
|
||||||
|
First need to convert to array of bits, right? Levenstein distance between them. Or just iterate the bits by doing num >> 1, get rightmost bit of both nums and do XOR. Sum up all numbers.
|
||||||
|
|
||||||
|
## Hints
|
||||||
|
|
||||||
|
> How would you figure out how many bits are different between two numbers?
|
||||||
|
|
||||||
|
Levenstein distance between the strings, but we're working with numbers. Iterate all bits and add up the differences.
|
||||||
|
|
||||||
|
> Think about what an XOR indicates. If you do a XOR b, where does the result have 1s? Where does it have Os?
|
||||||
|
|
||||||
|
XOR has result 0 if bits are different, 1 if bits are same.
|
||||||
|
|
||||||
|
## Solution
|
||||||
|
|
||||||
|
a ^ b makes XOR between a and b, then just iterate a c and increase count if the value at that point is 1. Then shift c to right, keep deleting the right most bit.
|
||||||
|
|
||||||
|
```c++
|
||||||
|
int bitSwapRequired(int a, int b) {
|
||||||
|
int count = 0;
|
||||||
|
for (int c = a ^ b; c != 0; c = c >> 1) {
|
||||||
|
count += c & 1;
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
@ -186,7 +186,7 @@ To enter **Command line mode**, type `:`.
|
||||||
* o / O insert line below / above
|
* o / O insert line below / above
|
||||||
* `A` append to line (in the end of line)
|
* `A` append to line (in the end of line)
|
||||||
* `d{motion}` delete {motion}
|
* `d{motion}` delete {motion}
|
||||||
* e.g. dw is delete word, d$ is delete to end of line, d0 is* delete to beginning of line, d$ delete until end of line
|
* e.g. dw is delete word, d$ is delete to end of line, d0 is delete to beginning of line, d$ delete until end of line
|
||||||
* `dd` delete whole line. 2dd, delete this and next line
|
* `dd` delete whole line. 2dd, delete this and next line
|
||||||
* `rx` to replace the character at the cursor by x. `ra`, deletes current character and writes a.
|
* `rx` to replace the character at the cursor by x. `ra`, deletes current character and writes a.
|
||||||
* c{motion} change {motion}
|
* c{motion} change {motion}
|
||||||
|
|
@ -199,7 +199,7 @@ To enter **Command line mode**, type `:`.
|
||||||
* visual mode + manipulation
|
* visual mode + manipulation
|
||||||
* select text, d to delete it or c to change it
|
* select text, d to delete it or c to change it
|
||||||
* `u` to undo, `U` to undo whole line, `<Ctr> + R` to redo
|
* `u` to undo, `U` to undo whole line, `<Ctr> + R` to redo
|
||||||
* y to copy / “yank” (some other commands like d also copy)
|
* `y` to copy / “yank” (some other commands like d also copy)
|
||||||
* `p` to paste under the cursor
|
* `p` to paste under the cursor
|
||||||
* Lots more to learn: e.g. ~ flips the case of a character
|
* Lots more to learn: e.g. ~ flips the case of a character
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -53,6 +53,9 @@ If you can't afford to buy the book, you can find a free pdf [here](http://ahmed
|
||||||
|
|
||||||
* Insert bit
|
* Insert bit
|
||||||
* Binary to string
|
* Binary to string
|
||||||
|
* Flip bit to create longest sequence of 1s
|
||||||
|
* (( n & (n-1)) == 0). n=?
|
||||||
|
* How many different bits do two numbers have. 11001 vs. 11010 = 2. Levenstein distance
|
||||||
|
|
||||||
## Chapter 7 Object-oriented design
|
## Chapter 7 Object-oriented design
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue