Files
2022-03-05 15:31:22 +08:00

127 lines
1.9 KiB
Go

package utils
import (
"encoding/json"
"fmt"
"sort"
"testing"
)
var rank = []string{
"Gold Medal", "Silver Medal", "Bronze Medal",
}
type Score struct {
Score int
Index int
}
func findRelativeRanks(score []int) []string {
length := len(score)
scores := make([]*Score, length)
for k, v := range score {
scores[k] = &Score{
Score: v,
Index: k,
}
}
sort.Slice(scores, func(i, j int) bool {
return scores[i].Score > scores[j].Score
})
out := make([]string, length)
for k, v := range scores {
if k < 3 {
out[v.Index] = rank[k]
continue
}
out[v.Index] = fmt.Sprintf("%d", k+1)
}
return out
}
func twoSum(nums []int, target int) []int {
length := len(nums)
for i := 0; i < length; i++ {
for j := i + 1; j < length; j++ {
if nums[i]+nums[j] == target {
return []int{i, j}
}
}
}
return []int{}
}
type ListNode struct {
Val int
Next *ListNode
}
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
out := new(ListNode)
if l1 != nil || l2 != nil {
}
return out
}
func lengthOfLongestSubstring(s string) int {
arrs := make([]map[rune]int, 0)
_runes := []rune(s)
_map := make(map[rune]int, 0)
length := len(_runes)
for k, v := range _runes {
if _, has := _map[v]; !has {
_map[v] = 1
if k >= length-1 {
goto CONTINUE
}
continue
}
CONTINUE:
arrs = append(arrs, _map)
fmt.Println(_map)
_map = map[rune]int{v: 1}
}
out := 0
for _, v := range arrs {
_length := len(v)
if out != _length && _length > out {
out = _length
}
}
return out
}
func TestCode(t *testing.T) {
//src := []int{5, 4, 3, 2, 1}
//t.Log(findRelativeRanks(src))
//t.Log(twoSum([]int{3, 2, 4}, 6))
t.Log(lengthOfLongestSubstring("dvdf"))
}
type ABC struct {
Key string `json:"key"`
}
func publish(src []*ABC) {
src[1].Key = "测试"
}
func TestNew(t *testing.T) {
src := []*ABC{&ABC{}, &ABC{}}
publish(src)
_bytes, _ := json.Marshal(src)
t.Log(string(_bytes))
}