Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2257 - MinusOne #30082

Open
YE840926098 opened this issue Sep 15, 2023 · 0 comments
Open

2257 - MinusOne #30082

YE840926098 opened this issue Sep 15, 2023 · 0 comments
Labels
2257 answer Share answers/solutions to a question zh-CN 简体中文

Comments

@YE840926098
Copy link

// 你的答案
type NumberToString<T extends number> = `${T}`
type StringToNumber<T extends string> = T extends `${infer F extends number}` ? F : never
type StringToArray<T extends string> = T extends `${infer F}${infer Rest}` ? [F, ...StringToArray<Rest>] : []
type MinusNum = {
  '0': '9'
  '1': '0'
  '2': '1'
  '3': '2'
  '4': '3'
  '5': '4'
  '6': '5'
  '7': '6'
  '8': '7'
  '9': '8'
}
type AddNum = {
  '0': '1'
  '1': '2'
  '2': '3'
  '3': '4'
  '4': '5'
  '5': '6'
  '6': '7'
  '7': '8'
  '8': '9'
  '9': '0'
}
type PosAndNeg<T extends string[]> = T extends [infer F, ...any] ? F extends '-' ? ArrayAdd<T> : ArraySub<T> : never

type ArraySub<T extends string[], U extends string[] = []> =
  T extends [...infer Rest extends string[], infer L] ?
  L extends keyof MinusNum ?
  L extends '0' ? ArraySub<Rest, [MinusNum[L], ...U]> : [...Rest, MinusNum[L], ...U]
  : never
  : never

type ArrayAdd<T extends string[], U extends string[] = []> =
  T extends [...infer Rest extends string[], infer L] ?
  L extends keyof AddNum ?
  L extends '9' ? ArrayAdd<Rest, [AddNum[L], ...U]> : [...Rest, AddNum[L], ...U]
  : U[0] extends '0' ? [L, '1', ...U] : [L, ...U]
  : 1

type ArrayToString<T extends string[]> =
  T extends [infer F extends string, ...infer Rest extends string[]] ? `${F}${ArrayToString<Rest>}` : ''
type Remove0<T extends string> = T extends `0${infer Rest}` ? Rest extends '' ? T : Remove0<Rest> : T
type MinusOne<T extends number> = T extends 0 ? -1 : StringToNumber<Remove0<ArrayToString<PosAndNeg<StringToArray<NumberToString<T>>>>>>
@YE840926098 YE840926098 added answer Share answers/solutions to a question zh-CN 简体中文 labels Sep 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2257 answer Share answers/solutions to a question zh-CN 简体中文
Projects
None yet
Development

No branches or pull requests

1 participant