<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>力扣题</title>
</head>
<body>
</body>
<script>
var missingNumber = function (nums) {
let res
let newNums = nums.sort((a, b) => {
return a - b
})
if (newNums[0] != 0) {
return 0
}
for (let i = 0; i < newNums.length; i++) {
if (newNums[i] != i) {
return i
}
}
return (res || res == 0) ? res : nums[nums.length - 1] + 1;
};
nums = [0, 1, 2, 3, 4, 5, 6, 7]
var addTwoNumbers = function (l1, l2) {
let result = new ListNode(0)
let nowNode = result
let carry = 0
return result.next
};
l1 = [9, 9], l2 = [9]
var isMatch = function (s, p) {
let reg = new RegExp(`^${p}$`);
return reg.test(s);
};
s = "aa"
p = "a*"
var romanToInt = function (s) {
var mapArr = {
'a': 900,
'b': 400,
'c': 90,
'd': 40,
'e': 9,
'f': 4,
'M': 1000,
'D': 500,
'C': 100,
'L': 50,
'X': 10,
'V': 5,
'I': 1,
}
let str = s.replace(/CM/g, "a").replace(/CD/g, "b").replace(/XC/g, "c").replace(/XL/g, "d").replace(/IX/g, "e")
.replace(/IV/g, "f")
let res = null
for (let char of str) {
res += mapArr[char]
}
return res
}
var intToRoman = function (num) {
let str = ""
var mapArr = {
'M': 1000,
'CM': 900,
'D': 500,
'CD': 400,
'C': 100,
'XC': 90,
'L': 50,
'XL': 40,
'X': 10,
'IX': 9,
'V': 5,
'IV': 4,
'I': 1,
}
for (const key in mapArr) {
if (num >= mapArr[key]) {
let len = parseInt(num / mapArr[key])
for (let i = 0; i < len; i++) {
str += key
}
num -= mapArr[key] * len
}
}
console.log(str)
return str
};
num = 20
var singleNumber = function (nums) {
return nums.reduce((a, b) => a ^ b)
};
var hammingWeight = function (n) {
let num = n.toString().split("").filter(item => {
return item == 1
})
return num.length
};
var num = '00000000000000000000000000001011'
var myPow = function (x, n) {
return Math.pow(x, n)
};
x = 2.00000, n = 10
myPow(x, n)
var isPowerOfTwo = function (n) {
if (n <= 0) return false
return (n & 1) == 0;
};
var isPowerOfThree = function (n) {
if (n < 1) return false
if (n == 1) return true
while (n > 0) {
if (n % 3 == 0) {
if (n / 3 == 1) {
return true
}
n = n / 3
} else {
return false
}
}
};
var isPowerOfFour = function (n) {
if (n < 1) return false
if (n == 1) return true
while (n > 0) {
if (n % 4 == 0) {
if (n / 4 == 1) {
return true
}
n = n / 4
} else {
return false
}
}
};
var moveZeroes = function (nums) {
let id = 0;
for (let i = 0; i < nums.length; i++) {
if (nums[i]) {
[nums[id], nums[i]] = [nums[i], nums[id]];
id++;
}
}
return nums;
};
var findRepeatNumber = function (nums) {
let num1 = {}
let res
nums.forEach(item => {
if (num1[item]) {
num1[item] += 1
if (num1[item] > 1) {
res = item
}
} else {
num1[item] = 1
}
})
return res
};
var replaceSpace = function (s) {
return s.replace(/\s/g, "%20")
};
s = "We are happy."
var merge = function (A, m, B, n) {
return [...A, ...B].filter(item => item > 0).sort((a, b) => a - b)
};
A = [1, 2, 3, 0, 0, 0], m = 3
B = [2, 5, 6], n = 3
var addDigits = function (num) {
while (num > 9) {
let sum = 0
num.toString()
let str = num.toString()
for (const key of str) {
sum += parseInt(key)
}
num = sum
}
return num
};
var finalValueAfterOperations = function (operations) {
var arr = {
"++X": +1,
"X++": +1,
"--X": -1,
"X--": -1,
}
let sum = 0
operations.forEach(item => {
sum += arr[item]
})
return sum
};
operations = ["++X", "++X", "X++"]
var runningSum = function (nums) {
nums.forEach((item, index) => {
return nums[index + 1] += nums[index]
})
return nums.splice(0, nums.length - 1)
};
nums = [1, 2, 3, 4]
var xorOperation = function (n, start) {
let res = 0
for (let i = 0; i < n; i++) {
res ^= (start + 2 * i)
}
return res
};
n = 5, start = 0
var reverseLeftWords = function (s, n) {
let res = s.substring(n) + s.substring(0, n)
return res
console.log(res)
};
s = "abcdefg", k = 2
var minPartitions = function (n) {
let res
return Math.max(...n.split(""))
};
n = "32"
minPartitions(n)
var search = function (nums, target) {
let count = 0
nums.map(item => {
if (item == target) {
count++
}
})
console.log(count)
return count
};
nums = [5, 7, 7, 8, 8, 10], target = 8
var singleNumber = function (nums) {
let object = {}
nums.map(item => {
if (object[item]) {
object[item]++
} else {
object[item] = 1
}
})
for (const key in object) {
if (object[key] == 1) {
res = key
}
}
return res
};
nums = [2, 2, 3, 2]
singleNumber(nums)
var countBits = function (n) {
let num = []
for (let i = 0; i <= n; i++) {
let arr = i.toString(2).split("").filter(item => {
return item == 1
})
num.push(arr.length)
}
return num
console.log(num)
};
var twoSum = function (nums, target) {
let map = new Map();
for (let i = 0; i < nums.length; i++) {
let value = target - nums[i];
if (map.has(value)) {
return [map.get(value), i];
} else {
map.set(nums[i], i);
}
}
return res;
};
numbers = [1, 2, 4, 6, 10], target = 0
twoSum(numbers, target)
var isUnique = function (astr) {
let arr = astr.split("")
return new Set([...arr]).size == arr.length
};
s = "leetcodee"
isUnique(s)
var CheckPermutation = function (s1, s2) {
return s1.split('').sort().join('') == s2.split('').sort().join('')
};
s1 = "abc", s2 = "bca"
CheckPermutation(s1, s2)
var replaceSpaces = function (S, length) {
return S.substring(0, length).replace(/\s/g, "%20")
};
replaceSpaces(" ", 5)
var oneEditAway = function (first, second) {
if (first == second) return true
let l1 = first.length
let l2 = second.length
let str1 = l1 > l2 ? first.split("") : second.split("")
let str2 = l1 > l2 ? second.split("") : first.split("")
if (str1.length - str2.length > 2) return false
let res = 0
if (str1.length - str2.length == 0) {
str1.forEach((item, index) => {
console.log(str2)
if (item != str2[index]) {
str2[index] = item
res += 1
}
})
} else {
str1.forEach((item, index) => {
if (item != str2[index]) {
res += 1
str2.splice(index, 0, item)
}
})
}
return res == 1 ? true : false
};
first = "pales"
second = "ple"
var printNumbers = function (n) {
let res = []
let count = ""
for (let j = 0; j < n; j++) {
count += "9"
}
for (let i = 1; i <= count; i++) {
res.push(i)
}
return res
};
var detectCapitalUse = function (word) {
var count = 0
for (let i = 0; i < word.length; i++) {
var c = word.charAt(i)
if (/^[A-Z]+$/.test(word[i])) {
count++
}
}
if (count == word.length || count == 0) {
return true
} else if (count == 1) {
if (/^[A-Z]+$/.test(word[0])) {
return true
} else {
return false
}
} else {
return false
}
};
var containsNearbyAlmostDuplicate = function (nums, k, t) {
let res = false
for (let i = 0; i < nums.length; i++) {
for (let j = 0; j < nums.length; j++) {
if (i != j) {
if (Math.abs(nums[i] - nums[j]) <= t && Math.abs(i - j) <= k) {
res = true
break
}
}
}
}
return res
};
nums = [1, 2, 3, 1], k = 3, t = 0
var topKFrequent = function (nums, k) {
let object = {}
nums.map(item => {
if (object[item]) {
object[item]++
} else {
object[item] = 1
}
})
var newobj = Object.keys(object).sort((a, b) => {
return object[b] - object[a]
})
return newobj.slice(0, k)
};
nums = [4, 1, -1, 2, -1, 2, 3], k = 2
var kSmallestPairs = function (nums1, nums2, k) {
let res = []
let sum = new Map()
let count = 0
nums1.map(item2 => {
nums2.map(item1 => {
res.push([item2, item1])
sum.set(count, item2 + item1)
count++
})
})
const resultArr = Array.from(sum).sort((a, b) => {
return a[1] - b[1]
}).slice(0, k)
let resArr = []
resultArr.map(item => {
resArr.push(res[item[0]])
})
return resArr
};
nums1 = [1, 2], nums2 = [3], k = 3
var swapNumbers = function (numbers) {
return numbers.reverse()
};
numbers = [1, 2]
var massage = function (nums) {
const len = nums.length;
if (!len) return 0;
const dp = [nums[0], Math.max(nums[0], nums[1])];
for (let i = 2; i < len; i++) {
dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]);
}
return dp[len - 1];
};
var rob = function (nums) {
var len = nums.length
if (!len) return 0
const arr = [nums[0], Math.max(nums[0], nums[1])]
for (let i = 2; i < len; i++) {
arr[i] = Math.max(arr[i - 1], arr[i - 2] + nums[i])
}
return arr[len - 1]
};
var myAtoi = function (s) {
let str = s.trim()
let res = ""
let count = 0
if (str[0] == "-" || str[0] == "+") {
if (str.length == 1) return 0
res += str[0]
count++
}
for (let i = count; i < str.length; i++) {
var reg = /[0-9]/
if (!reg.test(str[i])) {
if (res == "-" || res == "+") {
return 0
}
if (res > 2147483647) {
res = 2147483647
} else if (res < -2147483648) {
res = -2147483648
}
return res == "" ? 0 : res
} else {
res += str[i]
}
}
if (res > 2147483647) {
res = 2147483647
} else if (res < -2147483648) {
res = -2147483648
}
return res
};
s = " -11919730356x"
var threeSum = function (nums) {
if (nums.length < 3) return []
let res = []
if (nums.join("") == 0) {
res.push([0, 0, 0])
return res
}
nums.sort((a, b) => a - b)
for (let i = 0; i < nums.length; i++) {
if (nums[i] > 0 || (i > 0 && nums[i] == nums[i - 1])) {
continue
}
for (let j = i + 1; j < nums.length; j++) {
if (j > i + 1 && nums[j] == nums[j - 1]) {
continue
}
for (let k = j + 1; k < nums.length; k++) {
if (k > j + 1 && nums[k] == nums[k - 1]) {
continue
}
if (nums[i] + nums[j] + nums[k] == 0) {
res.push([nums[i], nums[j], nums[k]])
}
}
}
}
var demo = [...new Set(res.map((i) => JSON.stringify(i)))].map((i) => JSON.parse(i))
return demo
};
nums = nums = [-1, 0, 1, 2, -1, -4]
var threeSumClosest = function (nums, target) {
nums.sort((a, b) => a - b)
var ans = nums[0] + nums[1] + nums[2];
for (var i = 0; i < nums.length; i++) {
var start = i + 1,
end = nums.length - 1;
while (start < end) {
var sum = nums[start] + nums[end] + nums[i];
if (Math.abs(target - sum) < Math.abs(target - ans))
ans = sum;
if (sum > target)
end--;
else if (sum < target)
start++;
else
return ans;
}
}
return ans;
}
nums = [-1, 2, 1, -4], target = 1
var fourSum = function (nums, target) {
if (nums.length < 4) return []
let res = []
nums.sort((a, b) => a - b)
for (let i = 0; i < nums.length; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue
}
for (let j = i + 1; j < nums.length; j++) {
if (j > i + 1 && nums[j] == nums[j - 1]) {
continue
}
for (let k = j + 1; k < nums.length; k++) {
if (k > j + 1 && nums[k] == nums[k - 1]) {
continue
}
for (let l = k + 1; l < nums.length; l++) {
if (l > k + 1 && nums[l] == nums[l - 1]) {
continue
}
if (nums[i] + nums[j] + nums[k] + nums[l] == target) {
res.push([nums[i], nums[j], nums[k], nums[l]])
}
}
}
}
}
var demo = [...new Set(res.map((i) => JSON.stringify(i)))].map((i) => JSON.parse(i))
return demo
};
nums = [0, 0, 0, 0, 0], target = 0
var searchRange = function (nums, target) {
return [nums.indexOf(target), nums.lastIndexOf(target)]
};
nums = [], target = 0
</script>
</html>