当前位置: 首页 > 面试经验 >

美团0812后端-Golang

优质
小牛编辑
90浏览
2023-08-12

美团0812后端-Golang

第一题:100%

第二题:100%

第三题:100%

第四题:100%

package main
import "fmt"
func main() {
	n := 0
	fmt.Scan(&n)
	nums := make([]int, n)
	dict := make(map[int]int)
	for i := 0; i < n; i++ {
		n, _ := fmt.Scan(&nums[i])
		if n == 0 {
			break
		}
		dict[nums[i]] = i
	}
	var x, y int
	_, _ = fmt.Scan(&x, &y)
	v1, ok1 := dict[x]
	v2, ok2 := dict[y]
	if ok1 == false || ok2 == false {
		fmt.Println("No")
		return
	} else {
		if v1+1 == v2 || v2+1 == v1 {
			fmt.Println("Yes")
			return
		}
		fmt.Println("No")
		return
	}
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	nums := make([]int, n)
    res := 0 

	for i := 0; i < n; i++ {
		fmt.Scan(&nums[i])
        res += nums[i]
	}
	var x, y int
	fmt.Scan(&x, &y)
    l:= 0
    for i:=x;i<y;i++{
        l += nums[i-1]
    }
    fmt.Println(min(res-l,l))
}
func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}

package main

import "fmt"

const MAX_N = 1e3 + 5
const INF = 100000000

var nums [MAX_N][MAX_N]int
var dp [MAX_N][MAX_N]int

func main() {
	n, m := 0, 0
	fmt.Scan(&n, &m)
	for i := 1; i <= n; i++ {
		for j := 1; j <= m; j++ {
			fmt.Scan(&nums[i][j])
			dp[i][j] = dp[i-1][j] + dp[i][j-1] + nums[i][j] - dp[i-1][j-1]
		}
	}
	a, b := INF, INF
	for i := 1; i <= n; i++ {
		a = min(a, abs(dp[i][m]<<1-dp[n][m]))
	}
	for j := 1; j <= m; j++ {
		b = min(b, abs(dp[n][j]<<1-dp[n][m]))
	}
	fmt.Println(min(a, b))
}
func abs(n int) int {
	if n > 0 {
		return n
	}
	return -n
}
func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}

package main

import "fmt"

func main() {
	var length int
	var s string
	fmt.Scan(&length)
	fmt.Scan(&s)
	list := make([][]int, 0)
	for i := 1; i < length; i++ {
		if length%i == 0 {
			list = append(list, []int{i, length / i})
		}
	}
	res := length
	for _, v := range list {
		temp := 0
		m, n := v[0], v[1]
		nums := make([][]uint8, m)
		visited := make([][]int, m)
		for i := 0; i < m; i++ {
			nums[i] = make([]uint8, n)
			visited[i] = make([]int, n)
		}
		for i := 0; i < m; i++ {
			for j := 0; j < n; j++ {
				nums[i][j] = s[i*n+j]
			}
		}
		var dfs func(i, j int)
		dfs = func(x, y int) {
			if x < 0 || x >= m || y < 0 || y >= n || visited[x][y] == 1 {
				return
			}
			visited[x][y] = 1
			for _, v := range [][]int{{0, 1}, {0, -1}, {1, 0}, {-1, 0}} {
				dx, dy := v[0], v[1]
				nx, ny := x+dx, y+dy
				if nx < 0 || nx >= m || ny < 0 || ny >= n {
					continue
				}
				if nums[nx][ny] == nums[x][y] {
					dfs(nx, ny)
				}
			}
		}
		for i := 0; i < m; i++ {
			for j := 0; j < n; j++ {
				if visited[i][j] == 0 {
					temp++
					dfs(i, j)
				}
			}
		}
		res = min(res, temp)
	}
	fmt.Println(res)
}
func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}

 类似资料: