Skip to content

Commit

Permalink
feat(master): add counting bits
Browse files Browse the repository at this point in the history
  • Loading branch information
thaisonenouvo committed Feb 20, 2024
1 parent 9cbfd0f commit 92e3e49
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
10 changes: 10 additions & 0 deletions go/add-two-numbers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

type Node struct {
Val int
Next *Node
}

func addTwoNumbers(l1 *Node, l2 *Node) *Node {

}
21 changes: 21 additions & 0 deletions go/counting-bits.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import "strconv"

func countBits(n int) []int {
res := []int{}

for i := 0; i <= n; i++ {
binary := strconv.FormatInt(int64(i), 2)

count := 0
for _, v := range binary {
if v == '1' {
count++
}
}
res = append(res, count)
}

return res
}
15 changes: 15 additions & 0 deletions go/reverse-linked-list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

func revertseList(head *ListNode) *ListNode {
current := head

var prev *ListNode = nil

for current != nil {
next := current.Next
current.Next = prev
prev = current
current = next
}
return prev
}
10 changes: 10 additions & 0 deletions typescript/easy/counting-bits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function countBits(n: number): number[] {
let res: number[] = []

for (let i = 0; i <= n; i++) {
const binary = i.toString(2)
res.push(binary.split('').filter((x) => x === '1').length)
}

return res;
};
21 changes: 21 additions & 0 deletions typescript/easy/reverse-linked-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class ListNode {
val: number;
next: ListNode | null;

constructor(val?: number, next?: ListNode | null) {
this.val = val === undefined ? 0 : val;
this.next = next === undefined ? null : next;
}
}

function reverseList(head: ListNode | null): ListNode | null {
let prev: ListNode | null = null;
let current = head;
while (current) {
let next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}

0 comments on commit 92e3e49

Please sign in to comment.