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
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"math/rand" | |
"os" | |
"strings" | |
"time" |
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
func TestCreatingAStack(t *testing.T) { | |
s := &stack{ | |
data: []int{1, 2, 3, 4, 5, 6}, | |
} | |
testPop := s.pop() | |
testPop2 := s.pop() | |
if len(s.data) != 4 { | |
t.Errorf("Expected pop to return elements from the top of the stack: %v, popped: %v", s.data, testPop) | |
} |
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
type stackWithMin struct { | |
data []int | |
minStack stack //utilizes the stack implementation we defined above! | |
} | |
func (s *stackWithMin) pop() int { | |
popped := s.data[len(s.data)-1] | |
s.data = s.data[:len(s.data)-1] | |
if popped == s.min() { //if you've popped the min, move it off the minStack! | |
s.minStack.pop() |
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
func TestCreatingAMinStack(t *testing.T) { | |
s := &stackWithMin{} | |
for i := 1; i < 7; i++ { | |
s.push(i) | |
} | |
testPop := s.pop() | |
testPop2 := s.pop() | |
if len(s.data) != 4 { | |
t.Errorf("Expected pop to return elements from the top of the stack: %v, popped: %v", s.data, testPop) |
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
//the key to this implementation is using a different data structure in the stack. | |
//each val in the stack contains the current minimum at the time of insertion. | |
type stackVal struct { | |
val int | |
min int | |
} | |
type stackWithMin struct { | |
data []stackVal | |
} |
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
type stack struct { //use a struct to define the stack | |
data []int | |
} | |
func (s *stack) pop() int { | |
popped := s.data[len(s.data)-1] | |
s.data = s.data[:len(s.data)-1] //resize the internal slice | |
return popped | |
} |
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
func optimizedAddTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode { | |
carry := 0 | |
sum := 0 | |
//this is the starting node, we need to return this | |
head := ListNode{ | |
Val: 0, | |
Next: nil, | |
} | |
//create a current node to save to |
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
//struct we will use for Linked list | |
type ListNode struct { | |
Val int | |
Next *ListNode | |
} | |
//the function that puts it all together | |
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode { | |
n1 := createNumber(l1) | |
n2 := createNumber(l2) |
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
/** | |
* @param {number} x | |
* @return {number} | |
*/ | |
var reverse = function(x) { | |
let str = x.toString().split(""); | |
let reverse = 0; | |
if (str[0] === "-"){ | |
reverse = parseInt( "-" + str.slice(1).reverse().join("")); | |
} else { |
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
func reverse(x int) int { | |
neg := 1 | |
ans := 0 | |
if x < 0 { | |
//make sure to only work with positive numbers | |
x *= -1 | |
neg = -1 | |
} | |
//the business loop! |