chore(Go): added Palindrome and Single Number (#454)
Co-authored-by: Ming Tsai <37890026+ming-tsai@users.noreply.github.com>pull/465/head
parent
8b23149ae8
commit
f9a2d4d447
|
@ -5,6 +5,7 @@
|
|||
2. [Two Sum](arrays/two-sum.go)
|
||||
3. [Majority Element](arrays/majority_element.go)
|
||||
4. [Contains Duplicate](arrays/contains_duplicate.go)
|
||||
5. [Single Number](arrays/single-number.go)
|
||||
|
||||
## Scheduling
|
||||
1. [Interval Scheduling](scheduling/interval-scheduling.go)
|
||||
|
@ -20,4 +21,7 @@
|
|||
3. [Quicksort](sorting/quicksort.go)
|
||||
|
||||
## Recursion
|
||||
1. [Fibonacci](recursion/fibonacci.go)
|
||||
1. [Fibonacci](recursion/fibonacci.go)
|
||||
|
||||
## String
|
||||
1. [Palindrome Permutation](strings/palindrome-permutation.go)
|
|
@ -0,0 +1,52 @@
|
|||
package arrays
|
||||
|
||||
import "fmt"
|
||||
/*
|
||||
Given a non-empty array of int, every element appears twice except for 1.
|
||||
Find that element.
|
||||
*/
|
||||
|
||||
/*
|
||||
Hash table approach
|
||||
Time: O(n)
|
||||
Space: O(n)
|
||||
*/
|
||||
|
||||
func singleNumberWithHashTable(nums [] int) int{
|
||||
counts := make(map[int] int)
|
||||
for _, num := range nums{
|
||||
counts[num]++
|
||||
}
|
||||
|
||||
for key, value := range counts{
|
||||
if value == 1{
|
||||
return key
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/*
|
||||
Bit-manipulation approach
|
||||
Time: O(n)
|
||||
Space: O(1)
|
||||
*/
|
||||
func singleNumberWithConstantSpace(nums [] int) int{
|
||||
result := 0
|
||||
for _, num := range nums{
|
||||
result ^= num
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func runSingleNumber(){
|
||||
input1 := []int{2,2,1}
|
||||
input2 := []int{4,1,2,1,2}
|
||||
|
||||
fmt.Printf("The Single Number for input 1 is %d\n",singleNumberWithConstantSpace(input1))
|
||||
fmt.Printf("The Single Number for input 2 is %d\n",singleNumberWithConstantSpace(input2))
|
||||
|
||||
fmt.Printf("The Single Number for input 1 is %d\n",singleNumberWithHashTable(input1))
|
||||
fmt.Printf("The Single Number for input 2 is %d\n",singleNumberWithHashTable(input2))
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package strings
|
||||
|
||||
import(
|
||||
"strings"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
/*
|
||||
Given a string s, return true if we can have a palindrome from the permutation of the input
|
||||
|
||||
Time: O(n)
|
||||
Space: O(n)
|
||||
*/
|
||||
func canPermutePalindrome(s string) bool {
|
||||
a := strings.Split(s,"")
|
||||
dictionary := make(map[string] int)
|
||||
for _, char := range(a){
|
||||
dictionary[char]++
|
||||
}
|
||||
|
||||
odd := 0
|
||||
for _, value := range dictionary{
|
||||
if value % 2 != 0{
|
||||
odd++
|
||||
}
|
||||
}
|
||||
return odd <= 1
|
||||
}
|
||||
|
||||
|
||||
//You are welcome to play around with the test cases
|
||||
func runPermutationCheck(){
|
||||
input1 := "carerac"
|
||||
|
||||
fmt.Printf("%t for input %s \n", canPermutePalindrome(input1), input1) //should print true
|
||||
|
||||
input2 := "code"
|
||||
fmt.Printf("%t for input %s \n", canPermutePalindrome(input2), input2) //should print false
|
||||
|
||||
}
|
Loading…
Reference in New Issue