-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new bit manipulation and linked list example
- Loading branch information
Showing
3 changed files
with
51 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package golang | ||
|
||
/** | ||
* Definition for singly-linked list. | ||
* type ListNode struct { | ||
* Val int | ||
* Next *ListNode | ||
* } | ||
*/ | ||
|
||
type ListNode struct { | ||
Val int | ||
Next *ListNode | ||
} | ||
|
||
func getDecimalValue(head *ListNode) int { | ||
result := 0 | ||
|
||
for head != nil { | ||
// shift bits to right | ||
result = result << 1 | ||
// do a bitwise or operation | ||
// 100 | 001 -> b101 -> 5 | ||
result = result | head.Val | ||
head = head.Next | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package golang | ||
|
||
import "testing" | ||
|
||
func TestFirstGetDecimalValue(t *testing.T) { | ||
last := ListNode{Val: 1} | ||
middle := ListNode{Val: 0, Next: &last} | ||
head := ListNode{Val: 1, Next: &middle} | ||
|
||
result := getDecimalValue(&head) | ||
expected := 5 | ||
|
||
if result != expected { | ||
t.Fatalf(`getDecimalValue(%v) = %v, expected: %v`, head, result, expected) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
| Problem Number | Tag | URL | | ||
| -------------- | ------------- | ------------------------------------------------------------------------------------ | | ||
| 888.go | Hash table | https://leetcode.com/problems/fair-candy-swap | | ||
| 1608.go | Binary Search | https://leetcode.com/problems/special-array-with-x-elements-greater-than-or-equal-x/ | | ||
| 1539.go | Binary Search | https://leetcode.com/problems/kth-missing-positive-number/ | | ||
| Problem Number | Tag | URL | | ||
| -------------- | ------------------------------ | ------------------------------------------------------------------------------------ | | ||
| 888.go | Hash table | https://leetcode.com/problems/fair-candy-swap | | ||
| 1290.go | Linked List / Bit manipulation | https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer | | ||
| 1539.go | Binary Search | https://leetcode.com/problems/kth-missing-positive-number/ | | ||
| 1608.go | Binary Search | https://leetcode.com/problems/special-array-with-x-elements-greater-than-or-equal-x/ | |