Skip to content

Instantly share code, notes, and snippets.

View Longest Univalue Path.go
func max(v0 int, v1 int) int {
if v0 < v1 {
return v1
}
return v0
}
func longestUnivaluePathInternal(root *TreeNode, value int) int {
if nil == root || root.Val != value{
return 0
View Isomorphic Strings.go
func isIsomorphic(s string, t string) bool {
if len(s) != len(t) {
return false
}
var pairs = make(map[uint8]uint8)
var reversePairs = make(map[uint8]uint8)
for idx := range s {
if v, ok := pairs[s[idx]]; ok {
if v != t[idx] {
View 3sum.go
import (
"sort"
"fmt"
)
func threeSum(nums []int) [][]int {
// count elements
var counts = make(map[int]int)
for _, n := range nums {
View Container With Most Water.go
type Height struct {
idx int
value int
}
func maxArea(height []int) int {
var left []Height
for i := 0; i < len(height); i += 1 {
View Reverse Integer.go
func reverse(x int) int {
isMinus := false
var remain = x
if 0 > x {
isMinus = true
remain = -x
}
var acc = 0
for 0 < remain {
View Next Permutation.go
import (
"strings"
"strconv"
)
func nextPermutation(nums []int) {
var startPoint = -1
var endPoint = -1
View Minimum Time Difference.go
func stringAsMinutes(timeString string) int {
hour := int(timeString[0] - '0') * 10 + int(timeString[1] - '0')
minute := int(timeString[3] - '0') * 10 + int(timeString[4] - '0')
return hour * 60 + minute
}
func findMinDifference(timePoints []string) int {
sort.Strings(timePoints)
lenOfTimes := len(timePoints)
View Toeplitz Matrix.go
func isToeplitzMatrix(matrix [][]int) bool {
for xBase := -len(matrix) + 1; xBase < len(matrix[0]); xBase++ {
yBase := -xBase
if yBase < 0 {
yBase = 0
}
lastValue := matrix[yBase][xBase + yBase]
for y := yBase + 1; y < len(matrix) && xBase + y < len(matrix[0]); y++ {
View Purely Functional Data Structure ch03.scala
object QueueTest {
object Queue {
def empty[T]: Queue[T] = Queue[T]()
def queue[T](f: List[T], r: List[T]): Queue[T] = {
f match {
case Nil =>
Queue(r.reverse, Nil)
View TidyNumbers.scala
package Main
import java.io.{FileInputStream, FileOutputStream}
import scala.io.StdIn
object Main2 extends App {
Console.setIn(new FileInputStream("example2.in"))