extra

package
v0.0.0-...-632ac27 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 15, 2023 License: GPL-3.0 Imports: 0 Imported by: 0

README

Test

$ go test -v
=== RUN   TestXorProduct
--- PASS: TestXorProduct (0.00s)
PASS
ok  	github.com/ZhengjunHUO/leetcode/extra	0.092s

Benchmark. compare 2 methods

$ go test -bench . -benchmem
goos: darwin
goarch: arm64
pkg: github.com/ZhengjunHUO/leetcode/extra
BenchmarkXorproduct-8     	1000000000	         0.3149 ns/op	       0 B/op	       0 allocs/op
BenchmarkXorProductBF-8   	   38329	     31300 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/ZhengjunHUO/leetcode/extra	1.968s

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func XorProduct

func XorProduct(M int, N int) int
   because:
	wanted = M ^ (M+1) ^ ... ^ (N-1) ^ N
	prodFromZeroToX(N) = (0 ^ 1 ^ ... ^ M-1) ^ (M ^ M+1 ^ ... ^ N-1 ^ N)
			   = prodFromZeroToX(M-1) ^ wanted
   then XOR prodFromZeroToX(M-1) in both side we have:
	wanted = prodFromZeroToX(N) ^ prodFromZeroToX(M-1)

func XorProductBF

func XorProductBF(M int, N int) int

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL