当前位置:网站首页>2021-11-18: given a length len, it indicates how many bits there are in total. All characters

2021-11-18: given a length len, it indicates how many bits there are in total. All characters

2022-06-24 01:19:00 Fuda scaffold constructor's daily question

2021-11-18: Given a length len, How many people are there . All characters are lowercase (a~z), Can generate a length of 1, The length is 2, The length is 3... The length is len All strings of . If you sort all strings in dictionary order , Each string has a location . Given a string str, Given len, Please return str Is the number of in the total sequence . such as len = 4, The first few strings in the dictionary order are :a aa aaa aaaa aaab ... aaaz ... azzz b ba baa baaa ... bzzz c ....a Is the... In this sequence 1 individual ,bzzz Is the... In this sequence 36558 individual .

answer 2021-11-18:

cdb, Total length is 7, Excuse me, cdb It's the number one ?

first place c :

With a start , The remaining length is (0~6) There are several possibilities

+

With b start , The remaining length is (0~6) There are several possibilities

+

With c start , The remaining length is (0) There are several possibilities

Second d :

+

With ca In the beginning , The remaining length is (0~5) There are several possibilities

+

With cb In the beginning , The remaining length is (0~5) There are several possibilities

+

With cc In the beginning , The remaining length is (0~5) There are several possibilities

+

With cd In the beginning , The remaining length is (0) There are several possibilities

Third b

+

With cda In the beginning , The remaining length is (0~4) There are several possibilities

+

With cdb In the beginning , The remaining length is (0) There are several possibilities .

The code to use golang To write . The code is as follows :

package main

import "fmt"

func main() {
    s := "c"
    len0 := 2
    ret := kth(s, len0)
    fmt.Println(ret)
}
func kth(s string, len0 int) int {
    if len(s) == 0 || len(s) > len0 {
        return -1
    }
    num := []byte(s)
    ans := 0
    for i, rest := 0, len0-1; i < len(num); i, rest = i+1, rest-1 {
        ans += int(num[i]-'a')*f(rest) + 1
    }
    return ans
}

//  Whatever it starts with , The remaining length is (0~len) There are several possibilities 
func f(len0 int) int {
    ans := 1
    for i, base := 1, 26; i <= len0; i, base = i+1, base*26 {
        ans += base
    }
    return ans
}

The results are as follows :

picture

Zuo Shen java Code

原网站

版权声明
本文为[Fuda scaffold constructor's daily question]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/11/20211119083252265A.html