package main
import "fmt"
func CanGetString(s string, t string) bool {
pos := 0
sArr := []byte(s)
sLen := len(sArr)
for _, c:= range t {
for pos < sLen && int32(sArr[pos]) != c {
pos += 1
}
if pos == sLen {
return false
}
pos += 1
}
return true
}
func main() {
/*
问题描述: 输入两个字符串 s 和 t,判断 s 能否在删除一些字符串后得到 t
问题示例: 输入: s = "abc", t = "c" 输出: True
*/
var (
s = "abc"
t = "c"
)
fmt.Println(CanGetString(s, t))
}
package main
import (
"fmt"
"net"
)
func main() {
/*
问题描述: 输入一个 ip 地址,若合法,返回 YES;不合法,返回 NO
问题示例: 输入: 101.138.125.3 输出: YES
*/
ip := "192.172.1"
if address := net.ParseIP(ip); address == nil {
fmt.Println("NO")
} else {
fmt.Println("YES")
}
}
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
)
var fileList []string
func getExtFile(path string, ext string ) ([]string, error) {
rd, err := ioutil.ReadDir(path)
if err !=nil {
fmt.Println("read dir fail: ", err)
return fileList, err
}
for _, file := range rd {
filePath := path + string(os.PathSeparator) + file.Name()
if file.IsDir() {
_, _ = getExtFile(filePath, ext)
} else {
if ok:= strings.HasSuffix(filePath, ext); ok {
fileList = append(fileList, filePath)
}
}
}
return fileList,err
}
func main() {
/*
实现遍历目录与子目录,抓取.txt 文件
*/
f,_ := getExtFile("d://tmp/", ".yaml")
for _,val := range f {
fmt.Println(val)
}
}
package main
import "fmt"
// 链表节点
type ListNode struct {
Val int
Next *ListNode
}
func reverserList(head *ListNode) *ListNode {
var (
cur = head
pre *ListNode = nil
)
for cur != nil {
pre,cur,cur.Next = cur,cur.Next, pre // 链表翻转
}
return pre
}
func main() {
/*
问题:反转一个单链表。
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
*/
head := new(ListNode)
head.Val = 5
for i := 4; i > 0; i-- {
tmp := &ListNode{
Val: i,
Next: nil,
}
tmp.Next = head //头插
head = tmp
}
pre := reverserList(head)
for pre != nil {
fmt.Println(pre.Val)
pre = pre.Next
}
}
package main
import (
"fmt"
"time"
)
// 装饰器函数,在特定的函数执行前后,添加执行时间
func execTime(wrapper func()) {
startNow := time.Now()
wrapper()
fmt.Printf("程序运行时间: %s\n", time.Since(startNow))
}
// 定义函数
func hi() {
fmt.Println("hi!")
time.Sleep(time.Duration(1)*time.Second)
}
func main() {
/*
定义一个装饰器
*/
execTime(hi)
}
package main
import (
"fmt"
"time"
)
// 装饰器函数,在特定的函数执行前后,添加执行时间
func log(wrapper func(string), arg string) {
fmt.Printf("StartTime: %v\n",time.Now())
wrapper(arg)
fmt.Printf("EndtTime: %v\n",time.Now())
}
// 定义函数
func my(name string) {
fmt.Printf("hi! %s\n", name)
}
func main() {
/*
定义一个带参数的装饰器
*/
log(my, "张三")
}
// This example demonstrates a priority queue built using the heap interface.
package main
import (
"container/heap"
"fmt"
)
// An Item is something we manage in a priority queue.
type Item struct {
value string // The value of the item; arbitrary.
priority int // The priority of the item in the queue.
// The index is needed by update and is maintained by the heap.Interface methods.
index int // The index of the item in the heap.
}
// A PriorityQueue implements heap.Interface and holds Items.
type PriorityQueue []*Item
func (pq PriorityQueue) Len() int { return len(pq) }
func (pq PriorityQueue) Less(i, j int) bool {
// We want Pop to give us the highest, not lowest, priority so we use greater than here.
return pq[i].priority > pq[j].priority
}
func (pq PriorityQueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
pq[i].index = i
pq[j].index = j
}
func (pq *PriorityQueue) Push(x interface{}) {
n := len(*pq)
item := x.(*Item)
item.index = n
*pq = append(*pq, item)
}
func (pq *PriorityQueue) Pop() interface{} {
old := *pq
n := len(old)
item := old[n-1]
old[n-1] = nil // avoid memory leak
item.index = -1 // for safety
*pq = old[0 : n-1]
return item
}
// update modifies the priority and value of an Item in the queue.
func (pq *PriorityQueue) update(item *Item, value string, priority int) {
item.value = value
item.priority = priority
heap.Fix(pq, item.index)
}
// This example creates a PriorityQueue with some items, adds and manipulates an item,
// and then removes the items in priority order.
func main() {
// Some items and their priorities.
items := map[string]int{
"banana": 3, "apple": 2, "pear": 4,
}
// Create a priority queue, put the items in it, and
// establish the priority queue (heap) invariants.
pq := make(PriorityQueue, len(items))
i := 0
for value, priority := range items {
pq[i] = &Item{
value: value,
priority: priority,
index: i,
}
i++
}
heap.Init(&pq)
// Insert a new item and then modify its priority.
item := &Item{
value: "orange",
priority: 1,
}
heap.Push(&pq, item)
pq.update(item, item.value, 5)
// Take the items out; they arrive in decreasing priority order.
for pq.Len() > 0 {
item := heap.Pop(&pq).(*Item)
fmt.Printf("%.2d:%s ", item.priority, item.value)
}
}
package main
import (
"fmt"
)
func fact(j int) int {
sum := 0
if j == 0 {
sum = 1
} else {
sum = j * fact(j -1)
}
return sum
}
func main() {
/*
利用递归方法求 5!
阶乘 F(n)=F(n-1)*n
*/
j := 5
fmt.Println(fact(j))
}
package main
import (
"fmt"
)
//数组平分
func splitArray(arr []int, num int) [][]int {
arrLen := len(arr)
if arrLen < num {
return [][]int{arr} // 数组大小 小于 等分时,直接返回原数组
}
// 获取数组分割为多少份
var quantity int
if arrLen % num == 0 {
quantity = arrLen / num
} else {
quantity = (arrLen / num) + 1
}
// 声明分割好的二维数组
segments := make([][]int,0)
// 声明分割数组的截止
var s,end int
for i := 1; i <= num; i++ {
end = i * quantity
if i != num {
segments = append(segments,arr[i-1+s:end])
}else {
segments = append(segments,arr[i-1+s:])
}
s = end-i
}
return segments
}
func main() {
/*
问题描述: 将一个数组 n 等分。
问题示例: 输入: [1, 2, 3, 4, 5] 输出: 三等分:[[1, 2], [3, 4], [5]] 二等分: [[1, 2, 3], [4, 5]]
*/
var (
a = []int{1, 2, 3, 4, 5}
b = 3
)
fmt.Println(splitArray(a,b))
}
package main
import "fmt"
func main() {
/*
问题描述:编写一个程序,该程序以 X,Y 2 位为输入,并生成一个二维数组。 数组的第 i 行和第 j 列中的元素值应为 i * j
问题示例: 输入:3,5 输出:[[0,0,0,0,0],[0,1,2,3,4],[ 0,2,4,6,8]]
*/
var (
x = 3
y = 5
)
z := make([][]int, x)
for i := 0; i < len(z); i++ {
z[i]= make([]int, y)
}
for i := 0; i < x; i++ {
for j := 0; j < y; j++ {
z[i][j] = i * j
}
}
fmt.Println(z)
}
package main
import "fmt"
// 定义一个结构体
type student struct {
Name string
score float64
}
// student 因为student结构体首字母是小写,因此是只能在model使用
// 我们通过工厂模式来解决
func NewStudent(n string, s float64) *student {
return &student{
Name: n,
score: s,
}
}
// 如果score字段首字母小写,则,在其它包不可以直接方法,我们可以提供一个方法
func (s *student) GetScore() float64{
return s.score
}
func main() {
/*
实现一个工厂模式
简单工厂模式是属于创建型模式,简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。
简单工厂模式是 GoLang 最简单实用的模式,可以理解为是不同工厂模式的一个特殊实现。
*/
//定student结构体是首字母小写,我们可以通过工厂模式来解决
var stu = NewStudent("tom~", 98.8)
fmt.Println(*stu) //&{....}
fmt.Println("name=", stu.Name, " score=", stu.GetScore())
}
package main
import "fmt"
func compareString(a,b string) bool {
if len(b) == 0 {
return true
}
if len(a) == 0 {
return true
}
aList := []byte(a)
bList := []byte(b)
cList := make([]int,26) // 生成26个字母的对应list
for _, val := range aList {
cList[val-65]++ // 标记 a 中的字符
}
for _, val := range bList {
if cList[val-65] == 0 {
return false // b 中的字符不在 a 里,直接返回 false
}
}
return true
}
func main() {
/*
问题描述: 比较字符串 A 和 B,A 和 B 中的字符串都是大写字母,确定 A 中是否包含 B 中所有字符串
问题示例: A = "ABCD" B = "ACD" 返回 True
*/
var (
a = "ABCD"
b = "ACD"
)
fmt.Println(compareString(a,b))
}
package main
import "fmt"
func quickSort(arr []int, low, height int){
if low >= height {
return // 当 low = height 时跳出
}
left, right := low, height
pivot := arr[(low+height)/2] // 取中间数做对比
for left <= right {
// 从左边开始迭代
// 左边的数如果比 pivot 小,那么就应该将他放在左边,继续向右滑动,遇到一个比他大的为止
for arr[left] < pivot {
left++
}
// 右边的数如果比 pivot 大,那么就应该将他放在右边,继续向左滑动,遇到一个比他小的为止
for arr[right] > pivot {
right--
}
// 这里进行一次交换,将上面碰到的大数和小数交换一次
// left 继续右走,right 继续左走 注意这里还不一定相遇,去继续执行上面的逻辑
if left <= right {
arr[left], arr[right] = arr[right], arr[left]
left++
right--
}
}
quickSort(arr, low, right)
quickSort(arr, left, height)
}
func main() {
/*
实现快速排序
快速排序:分治法+递归实现
随意取一个值A,将比A大的放在A的右边,比A小的放在A的左边;然后在左边的值AA中再取一个值B,将AA中比B小的值放在B的左边,将比B大的值放在B的右边。以此类推
*/
testArr := []int{2, 5, 3, 7, 4, 5, 8, 1, 4, 0}
quickSort(testArr, 0, len(testArr)-1)
fmt.Println(testArr)
}
package main
import (
"fmt"
"sync"
"time"
)
// 定义结构体代替类
type singleton struct {
}
// 定义全局变量
var (
instance *singleton
lock sync.Mutex
)
func GetSingleton() *singleton{
// 懒汉加锁方式:指全局的单例实例在第一次被使用时构建。
if instance == nil { // 单例没被实例化,才会加锁
lock.Lock()
defer lock.Unlock() // return 之后释放锁
if instance == nil { // 单例没被实例化才会创建
instance = &singleton{}
}
}
return instance
}
func main() {
/*
实现单例模式
定义:单例对象的类必须保证只有一个实例存在,全局有唯一接口访问。
https://github.com/silsuer/golang-design-patterns/blob/master/singleton/README.md
*/
fmt.Printf("%p\n", GetSingleton())
fmt.Printf("%p\n", GetSingleton())
fmt.Printf("%p\n", GetSingleton())
time.Sleep(time.Second)
fmt.Println("done")
}
package main
import "fmt"
func sortPort(a []int64) []int64{
aRange := len(a) - 1
for i :=0; i < aRange; i++ {
jRange := aRange - i
for j :=0; j < jRange; j++ {
if a[j] > a[j+1] {
a[j],a[j+1] = a[j+1],a[j]
}
}
}
return a
}
func main() {
/*
实现冒泡排序。
*/
var (
a = []int64{1,5,32,76,2,1,92,3,6,20}
)
fmt.Println(sortPort(a))
}
package main
import "fmt"
func binarySearch(data []int64, item int64) bool{
n := len(data)
if n > 0 {
mid := n / 2
if data[mid] == item {
return true
} else if data[mid] > item {
return binarySearch(data[:mid], item)
} else {
return binarySearch(data[mid+1:], item)
}
}
return false
}
func main() {
/*
实现二分查找
*/
var (
data = []int64{1,2,3,4,5,6,7,8}
item = 7
)
fmt.Println(binarySearch(data, int64(item)))
}
package main
import "fmt"
func max(x, y int64) int64{
if x > y {
return x
}
return y
}
func min(x, y int64) int64{
if x < y {
return x
}
return y
}
func main() {
/*
问题描述:在一个数组里找到第 2 大的数。
问题示例: 输入: [1, 3, 2, 4] 输出:3
输入: [1, 2] 输出: 1
*/
var (
a = []int64{1,3,2,4}
)
maxValue := max(a[0], a[1])
secValue := min(a[0], a[1])
for i := 2; i < len(a); i++ {
if a[i] > maxValue {
secValue = maxValue
maxValue = a[i]
} else if a[i] > secValue {
secValue = a[i]
}
}
fmt.Println(secValue)
}
package main
import "fmt"
func main() {
/*
问题描述: 输入三个整数 x,y,z,请把这三个数由小到大输出
问题示例: 输入:3 4 5 输出: [3, 4, 5]
*/
var (
a = []int{3,5,4}
)
for i :=0; i <len(a); i++ {
for j :=i; j <len(a); j++ {
if a[i] > a[j] {
a[i],a[j]=a[j],a[i]
}
}
}
fmt.Println(a)
}
package main
import "fmt"
func fib(n int) int {
// 递归实现
if n < 2 {
return n
} else {
return fib(n-1) + fib(n-2)
}
}
func main() {
/*
问题描述:斐波那契数列指的是这样一个数列 0, 1, 1, 2, 3, 5, 8, 13,第 0 项是 0,第 1 项是第一个 1。从第三项开始,每一项都等于前两项之和。
*/
var (
n = 10
a []int
)
for i := 0; i < n; i++ {
a = append(a, fib(i))
}
fmt.Println(a)
}
问题已解决。
go
package main
import (
"fmt"
)
func main() {
/*
问题描述: 给定一个数组 A,其中除了 2 个数,其他均出现 2 次,请找到不重复的 2 个数并返回。
问题示例: 给定: A = [1, 2, 5, 5, 6, 6] 返回: [1, 2]
给定: A = [3, 2, 7, 5, 5, 7] 返回: [2, 3]
*/
var (
num = 0
a = []int{3, 2, 7, 5, 5, 7}
b []int
)
for i := 0; i < len(a); i++ {
num = 0
for ii := 0; ii < len(a); ii++ {
if a[i] == a[ii] {
num++
}
if num >= 2 {
continue
}
}
if num < 2 {
b = append([]int{a[i]}, b...)
}
}
fmt.Println(b)
}
go 语言实现
package main
import "fmt"
func sortArry(a, b []int) []int {
var (
aLen = len(a)
bLen = len(b)
cLen = aLen + bLen
arrC = make([]int, cLen)
aIndex = 0
bIndex = 0
cIndex = 0
)
for aIndex < aLen && bIndex < bLen {
// 循环对比数组的大小
if a[aIndex] < b[bIndex] {
arrC[cIndex] = a[aIndex]
cIndex++
aIndex++
} else {
arrC[cIndex] = b[bIndex]
cIndex++
bIndex++
}
}
// 取出数组中剩余的值
for aIndex < aLen {
arrC[cIndex] = a[aIndex]
cIndex++
aIndex++
}
for bIndex < bLen {
arrC[cIndex] = b[bIndex]
cIndex++
bIndex++
}
return arrC
}
func main() {
/*
问题描述: 合并两个升序的整数数组,形成一个新数组,并保持有序。
问题实例: 输入: A = [1] B = [1] 输出: [1, 1]
输入: A = [1, 2, 3, 4] B = [2, 4, 5, 6] 输出: [1, 2, 2, 3, 4, 4, 5, 6]
*/
var (
a = []int {1, 2, 3, 4}
b = []int {2, 4, 5, 6}
)
fmt.Println(sortArry(a, b))
}
答: 内核层面 kill 掉了 pause 进程,当 pause 被进程被杀,为了给 pod 设置网络,那么分配 IP 的 sandbox 也会被一起杀掉,pod 重启后 sandbox 会重建,那么 pod 的 ip 就改变了。但是 pod 的 name 在 delete 操作时才会变,但是此时 pod 并没有删除,所以 name 不变。
因为 1.22 把 v1beta 去掉了,ingress crontrol 都有问题了,现在这个问题解决了。可以重新安装下 ingress
python3 解法
每个元素的值是 i*j
def gen_array(x, y):
arr = []
for i in range(x):
fi = [] # fi列表再次进行初始化
for j in range(y):
fi.append( i * j )
arr.append(fi)
print(arr)
gen_array(3, 5)
[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]