Skip to content

Instantly share code, notes, and snippets.

@liyonghelpme
Created December 22, 2019 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liyonghelpme/379862184ee6785f3f58736eaedfaa0b to your computer and use it in GitHub Desktop.
Save liyonghelpme/379862184ee6785f3f58736eaedfaa0b to your computer and use it in GitHub Desktop.
// import { isNumber } from 'util';
// class CurStep {
// wei : number
// curV : number
// }
// function IsNum( c : string) {
// return !isNaN(Number(c))
// }
//特定长度的整数最高位 > 0
class MyInt {
nums : number[] = [] //一半数值 包含中间点
total : number //总长度
realValue : number
buChangeNum : number = 0
// constructor(l : number, totalLen : number){
// this.total = totalLen
// if(totalLen == 1) {
// this.nums.push(0)
// }else {
// for(let i = 0; i < (l-1); i++){
// this.nums.push(0)
// }
// this.nums.push(1)
// }
// }
MakeHalfInt(l : number, totalLen : number){
this.total = totalLen
if(totalLen == 1) {
this.nums.push(0)
}else {
for(let i = 0; i < (l-1); i++){
this.nums.push(0)
}
this.nums.push(1)
}
}
Zero(n : number) {
for(let i = 0; i < n; i++)
{
this.nums.push(0)
}
}
//去除中位数的值 偶数则是所有部分的和
IntValue(){
let res = 0
let mod = this.total % 2
let initV = this.nums.length-1
if(mod == 1) initV = this.nums.length-2
for(let i = initV; i>=0; i--)
{
res += this.nums[i]
if(i > 0) res *= 10
}
// console.log("InvValue", this, res)
return res
}
//差值
//计算差值
Sub(other : MyInt) {
// let res = new MyInt()
// res.Zero(this.nums.length)
return this.IntValue() - other.IntValue()
//origin 原始的奇数偶数特性
// let mod = other.total % 2
// if(mod == 1) {//奇数 中位数 不用做减法 中位数 单独补偿
// }
}
//得到回文数的一半
//123 --》2, 1
static FromFull(n : number[]){
let t = n.length
let l = Math.floor(t/2)
let mod = t % 2
if(mod == 1)l++
let mi = new MyInt()
mi.total = t
// mi.MakeHalfInt(l, t)
for(let i = 0; i < l; i++){
mi.nums.push(n[l-i-1])
}
return mi
}
//2, 1 --> 1, 2
Reverse() {
let mi = new MyInt()
mi.total = this.total
for(let i = 0; i < this.nums.length; i++)
{
mi.nums.push(this.nums[this.nums.length-i-1])
}
return mi
}
//普通的数字 回文数只保留后面部分 用于和回文计算差值
//123 --> 3, 2
static NormalInt(n : number[]){
let mi = new MyInt()
mi.total = n.length
let l = Math.floor(n.length/2)
let mod = n.length % 2
if(mod == 1)l++
for(let i = 0; i < l; i++){
mi.nums.push(n[n.length-i-1])
}
// for(let i = 0; i < n.length; i++){
// mi.nums.push(n[n.length-i-1])
// }
return mi
}
PrintDir(){
return this.nums.reverse()
}
Copy() {
// return this.nums.slice(0)
let nn : number[] = []
for(let i = 0; i < this.total; i++){
if(i < this.nums.length) {
nn.push(this.nums[this.nums.length-i-1])
}else {
let offToEnd = this.total-i-1
let len = this.nums.length-offToEnd-1
nn.push(this.nums[len])
}
}
return nn
}
//最低位1的值
LowOneValue() {
// let mod = this.total % 2
let wei = Math.floor(this.total /2 )
let v = 1
for(let i = 0; i < wei; i++){
v *= 10
}
return v
}
//可以位数变少
MinusOne(lowV : number) {
//正常补偿最低位 退位 10 --> 9
this.buChangeNum = lowV
let findMinus = false
for(let i = 0; i < this.nums.length; i++){
let n = this.nums[i]
if(n > 0) {
this.nums[i]--
findMinus = true
break
} else {
this.nums[i] = 9
}
}
//删除最高位0 一位数 0 可以接受
//11 -> 0 -->9这个数字
if(this.total > 1) {
//最高位0
if(this.nums[this.nums.length-1] == 0){
// this.nums.pop()
// // this.total--
// let mod = this.total % 2
// if(mod == 1) this.total-- //奇数变偶数
// else { //4长度 变 2长度 不能减 差异太大了
//2位偶数
// if(this.total == 2) {
// this.total = 1
// this.nums.push(9)
// }else {
// this.total -= 2
// return false
// }
//前一个是11--》 9 1001-》999
this.total--
this.nums = []
let half = Math.floor(this.total/2)+(this.total%2)
for(let i = 0; i < half; i++) {
this.nums.push(9)
}
this.buChangeNum = this.LowOneValue();
// }
}
}
return findMinus
}
//可以位数变多
AddOne(lowV : number){
this.buChangeNum = lowV
let findAdd = false
for(let i = 0; i < this.nums.length; i++){
let n = this.nums[i]
if(n < 9) {
findAdd = true
this.nums[i] += 1
break
}else {
this.nums[i] = 0
}
}
//扩容
if(!findAdd) {
// this.nums.push(1)
this.total++
this.nums = []
let half = Math.floor(this.total/2)
half += this.total % 2
for(let i = 0; i < (half-1); i++){
this.nums.push(0)
}
this.nums.push(1)
}
return findAdd
}
}
//寻找数值附近的回文数
//暴力 从小到大 寻找所有回文数
//长度控制
class HuiWen {
listNums : number[][]
targetNum : number[] = []
// Find(l : number) {
// this.ListSL(l);
// }
FindMinDiff(s : string){
for(let i = 0; i < s.length; i++){
let c = s[i]
let n = Number(c)
this.targetNum.push(n)
}
// if(this.targetNum.length == 2){
// let t= this.targetNum
// if(t[0] == 1 && t[1] == 0) return "9"
// }
//123--》3, 2
let origin = MyInt.NormalInt(this.targetNum)
//基础回文数123-> 2, 1
let it = MyInt.FromFull(this.targetNum)
it.realValue = 0
//1, 2
let rev = it.Reverse()
let delta1 = Math.abs(rev.Sub(origin))
// console.log("d1", it, rev, origin, delta1)
//回文数-1 +1
let minusOne = MyInt.FromFull(this.targetNum)
minusOne.realValue = -1
let lowV = minusOne.LowOneValue()
//2, 1 --> 1, 1
let fm = minusOne.MinusOne(lowV)
//奇数 偶数 减法不同
//可以-1
let delta2 = 0
if(fm) {
delta2 = minusOne.Reverse().Sub(origin)
delta2 = Math.abs(delta2-minusOne.buChangeNum)
}
//2, 1 --> 3, 1
let addOne = MyInt.FromFull(this.targetNum)
addOne.realValue = 1
addOne.AddOne(lowV)
let delta3 = addOne.Reverse().Sub(origin)
delta3 += addOne.buChangeNum
delta3 = Math.abs(delta3)
// console.log(it.Copy())
// let minV = Math.min(delta1, delta2, delta3)
let arr : [number, MyInt][] = [[delta1, it], [delta2, minusOne], [delta3, addOne]]
// arr.forEach(ele=>{
// console.log(ele)
// })
arr.sort((a, b)=>{
let v = a[0]-b[0]
if(v == 0) return a[1].realValue-b[1].realValue
return v
})
// console.log("Arr")
for(let i = 0; i < arr.length; i++){
if(arr[i][0] > 0){
let cp = arr[i][1].Copy()
// let result = ""
return cp.join("")
}
}
}
// ListSL(sl : number) {
// let mod = sl % 2
// let k = Math.floor(sl/2)
// let ret : number[][] = []
// this.listNums = ret
// // let first : number [] = []
// //一半的长度 + 中间Index的长度
// if(mod == 1) {
// k++
// }
// let first = new MyInt()
// first.MakeHalfInt(k, sl)
// // if(mod == 1) {
// // if(k > 0) {
// // let mid = 0
// // first.push(mid)
// // for(let i = 0; i < (k-1); i++){
// // first.push(0)
// // }
// // first.push(1)
// // }else {
// // let mid = 0
// // first.push(mid)
// // }
// // }else {
// // for(let i = 0; i < (k-1);i++){
// // first.push(0)
// // }
// // first.push(1)
// // }
// // console.log(first)
// // ret.push(first.slice(0))
// // let step = new CurStep()
// // step.wei = 0
// // step.curV = 0
// //向后搜索回文
// //当前回文进度 每个数字进低位 高位变动
// ret.push(first.Copy());
// while(true) {
// let ok = first.AddOne()
// if(!ok) break;
// ret.push(first.Copy())
// }
// }
}
var s = "123"
var s = "123";
var s = "100"
var s = "10099"
var s = "10"
for(let v = 0; v < 200; v++) {
let hw = new HuiWen()
let s = v.toString()
// hw.Find(3)
let ret = hw.FindMinDiff(s)
console.log(s, ret)
}
// console.log(hw.listNums)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment