Skip to content

Commit

Permalink
添加文件
Browse files Browse the repository at this point in the history
  • Loading branch information
CSep27 authored Mar 23, 2020
1 parent 2bd7e8d commit a42f2f3
Show file tree
Hide file tree
Showing 24 changed files with 1,094 additions and 0 deletions.
62 changes: 62 additions & 0 deletions 两个大整数相加-优化-未完成.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>

</body>
<script>
function add (a, b) {
var length = a.length > b.length ? (a.length + 1) : (b.length + 1)
var arrA = []
var arrB = []
var arrR = []
for (var i = 0; i < length; i++) {
arrA[i] = a[a.length - 1 - i] == undefined ? 0 : a[a.length - 1 - i]
arrB[i] = b[b.length - 1 - i] == undefined ? 0 : b[b.length - 1 - i]
arrR[i] = 0
}
console.log(arrA)
for (var i = 0; i < length; i++) {
var sum = Number(arrA[i]) + Number(arrB[i]) + arrR[i]
if (sum > 9) {
arrR[i] = sum - 10
arrR[i + 1] = 1
} else {
arrR[i] = sum
}
}
console.log(arrR)
return parseInt(arrR.reverse().join(''))
}
function addBigNumber (a, b) {
var num = 3
var length = Math.ceil((a.length > b.length ? (a.length + 1) : (b.length + 1)) / num)
var arrA = []
var arrB = []
var arrR = []
for (var i = 0; i < length; i++) {
arrA[i] = a.slice(i* num, (i+1)*num) == undefined ? 0 : a.slice(i* num, (i+1)*num)
arrB[i] = b[b.length - 1 - i] == undefined ? 0 : b[b.length - 1 - i]
arrR[i] = 0
}
console.log(arrA)
arrA = arrA.reverse()
for (var i = 0; i < length; i++) {
var sum = Number(arrA[i]) + Number(arrB[i]) + arrR[i]
if (sum > 9) {
arrR[i] = sum - 10
arrR[i + 1] = 1
} else {
arrR[i] = sum
}
}
console.log(arrR)
return parseInt(arrR.reverse().join(''))
}
console.log(add('426709752318', '95481253129'))
addBigNumber('426709752318', '95481253129')
</script>
</html>
36 changes: 36 additions & 0 deletions 两个大整数相加.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>

</body>
<script>
function add (a, b) {
var length = a.length > b.length ? (a.length + 1) : (b.length + 1)
var arrA = []
var arrB = []
var arrR = []
for (var i = 0; i < length; i++) {
arrA[i] = a[a.length - 1 - i] == undefined ? 0 : a[a.length - 1 - i]
arrB[i] = b[b.length - 1 - i] == undefined ? 0 : b[b.length - 1 - i]
arrR[i] = 0
}
console.log(arrA)
for (var i = 0; i < length; i++) {
var sum = Number(arrA[i]) + Number(arrB[i]) + arrR[i]
if (sum > 9) {
arrR[i] = sum - 10
arrR[i + 1] = 1
} else {
arrR[i] = sum
}
}
console.log(arrR)
return parseInt(arrR.reverse().join(''))
}
console.log(add('426709752318', '95481253129'))
</script>
</html>
41 changes: 41 additions & 0 deletions 删去k个数字后的最小值-优化.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>

</body>
<script>
function removeKDigits (num, k) {
// 新整数的长度 = 原整数长度 - k
var num = num.toString()
var newLength = num.length - k
// 创建一个栈,用于接收所有数字
var stack = new Array(num.length)
var top = 0
for (var i = 0; i < num.length; ++i) {
// 遍历当前数字
var c = num.charAt(i)
// 当前一个元素大于当前遍历到的元素时,栈顶元素出栈
while (top > 0 && stack[top - 1] > c && k > 0){
top -= 1
k -= 1
}
// 遍历到的当前元素入栈
stack[top++] = c
}
// 找到栈中第一个非0数字的位置,以此构建新的整数字符串
var offset = 0
while (offset < newLength && stack[offset] == 0) {
offset++
}
return offset == newLength ? "0" : stack.splice(offset, newLength - offset).join('')
}
console.log(removeKDigits(541270936, 3))
console.log(removeKDigits(100123, 1))
console.log(removeKDigits(12345, 1))
console.log(removeKDigits(12345, 5))
</script>
</html>
55 changes: 55 additions & 0 deletions 删去k个数字后的最小值.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>

</body>
<script>
function removeKDigits (num, k) {
var strArr = num.toString().split('')
function removeOneDigit (strArr) {
for (var i = 0; i < strArr.length; i++) {
if (Number(strArr[i]) > Number(strArr[i + 1])){
strArr.splice(i, 1)
hasCut = true
break
}
}
}
var hasCut = false
while (k > 0) {
k--
hasCut = false
removeOneDigit(strArr)
}
// 如果没有找到要删除的数字,则删除最后一个数字
if (!hasCut) {
strArr.splice(strArr.length - 1, 1)
}
// 清除左侧为0的数字 用parseInt方法不需要这一步
/*function removeZero (strArr) {
if (strArr[0] != 0) {
return strArr
}
for (var i = 1; i < strArr.length; i++) {
if (strArr[i] != 0){
return strArr.slice(i)
}
}
}
strArr = removeZero(strArr)
console.log(strArr)*/
// 如果整数的所有数字都被删除了,直接返回0
if (strArr.length === 0) {
return 0
}
return parseInt(strArr.join(''))
}
console.log(removeKDigits(541270936, 3))
console.log(removeKDigits(100123, 1))
console.log(removeKDigits(12345, 1))
</script>
</html>
38 changes: 38 additions & 0 deletions 判断数字是否为2的整数幂.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>

</body>
<script>
// 普通解法
/*function isPowerOf2 (num) {
var a = 1
while (num !== a && a < num) {
a = a<<2
}
return num === a
}*/
/*function isPowerOf2 (num) {
var a = 1
while (a <= num) {
if(a === num){
return true
}
a = a<<2
}
return false
}*/
// 最优解法
function isPowerOf2 (num) {
return (num & num - 1) === 0
}
console.log(isPowerOf2(4))
console.log(isPowerOf2(1))
console.log(isPowerOf2(5))
console.log(isPowerOf2(6))
</script>
</html>
55 changes: 55 additions & 0 deletions 堆排序.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>

</body>
<script>
// 待调整的堆 要下沉的父节点 堆的有效大小
function downAdjust (array, parentIndex, length) {
// temp 保存父节点的值 用于最后的赋值
var temp = array[parentIndex]
var childIndex = 2 * parentIndex + 1 // 左孩子下标
while (childIndex < length) {
// 如果有右孩子,且右孩子大于左孩子的值 则定位到右孩子
if(childIndex + 1 < length && array[childIndex + 1] > array[childIndex]) {
childIndex++
}
// 如果父节点大于任何一个孩子的值,则直接跳出
if (temp >= array[childIndex]) {
break
}
// 无需真正交换 单向赋值即可 因为比较还没有结束
// 父节点下面的子节点还会可能有子节点 需要接着比较 所以不能直接将父子节点的值直接交换
array[parentIndex] = array[childIndex] // 子节点中值大的上浮
parentIndex = childIndex // 子节点的索引赋给父节点索引
childIndex = 2 * childIndex + 1 // 计算子节点有没有子节点 没有值会超出length 本次循环结束
}
array[parentIndex] = temp // 在这里将原来父节点的值替换到子节点中 parentIndex是原来值大子节点的索引
}
function heapSort (array) {
// 把无序数组构建成最大堆
// 从最后一个非叶子节点开始 依次比较
// Math.ceil((array.length-2)/2)最后一个非叶子节点的下标
for (var i = Math.ceil((array.length-2)/2); i >= 0; i--) {
downAdjust(array, i, array.length)
}
// 循环调整堆顶元素 移到集合尾部 调整堆产生新的堆顶
// 最大堆堆顶是堆中最大的元素,第一次直接将该元素和第一个元素调换位置,将除最后一个元素外的剩下元素再构建成最大堆,这样第二轮中第二大的元素就会成为堆顶,然后将第二大元素与倒数第二个元素交换位置,以此类推。数组最后就会成为升序数组
for (var i = array.length-1; i > 0; i--) {
// 最后一个元素和第一个元素进行交换
var temp = array[i]
array[i] = array[0]
array[0] = temp
// 除去已经调整的元素,将剩下的元素重新调整为最大堆
downAdjust(array, 0, i)
}
return array
}
var arr = [1,3,2,6,5,7,8,9,10,0]
console.log(heapSort(arr))
</script>
</html>
57 changes: 57 additions & 0 deletions 寻找全排列的下一个数.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>

</body>
<script>
function findNearestNumber (arr) {
// 找到逆序临界点位置
function findTransferPoint (arr) {
for (var i = arr.length - 1; i >= 0; i--) {
if (arr[i] > arr[i-1]) {
return i
}
}
return 0
}
// 将逆序临界点前一位数head和逆序区大于head的最小一位数交换
function exchangeHead (arr, index) {
var head = arr[index-1]
// 逆序区,从后向前遍历,出现的第一个大于head的数即为需要交换的数
for (var i = arr.length-1;i>0; i--) {
if (arr[i] < head) {
arr[index-1] = arr[i]
arr[i] = head
break
}
}
}
// 逆序数组改成顺序 首尾对应元素调换顺序 i从前向后索引 j从后向前索引
function reverse (arr, index) {
for (var i = index,j = arr.length -1;i<j; i++,j--) {
var temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
}
}

// 逆序边界
var index = findTransferPoint(arr)
// 为0表示整个数组已经逆序
if (index === 0) {
return null
}

var arrCopy = arr.concat([])
exchangeHead(arrCopy, index)
reverse(arrCopy, index)
return arrCopy
}

console.log(findNearestNumber([1,2,3,5,4]))
</script>
</html>
Loading

0 comments on commit a42f2f3

Please sign in to comment.