leetcode算法学习
2019-05-09 本文已影响0人
QRFF
1.
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.
Example 1:
Input: "()"
Output: true
Example 2:
Input: "()[]{}"
Output: true
Example 3:
Input: "(]"
Output: false
Example 4:
Input: "([)]"
Output: false
Example 5:
Input: "{[]}"
Output: true
//答案
var isValid = function(s) {
let valid = true;
const stack = [];
const mapper = {
'{': "}",
"[": "]",
"(": ")"
}
for (let i in s) {
const v = s[i];
if (['(', '[', '{'].indexOf(v) > -1) {
stack.push(v);
console.log('stack', stack)
} else {
const peak = stack.pop();
console.log('peak', peak)
if (v !== mapper[peak]) {
valid = false
}
}
}
if (stack.length > 0) return false;
return valid;
};
console.log(isValid("[{()}]"))
- 解题思路
使用栈,遍历输入字符串
如果当前字符为左半边括号时,则将其压入栈中
如果遇到右半边括号时,分类讨论:
1)如栈不为空且为对应的左半边括号,则取出栈顶元素,继续循环
2)若此时栈为空,则直接返回false
3)若不为对应的左半边括号,反之返回false
2.
Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
Example 1:
Given nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
It doesn't matter what you leave beyond the returned length. Example 2:
Given nums = [0,0,1,1,1,2,2,3,3,4],
Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.
It doesn't matter what values are set beyond the returned length. Clarification:
Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
Internally you can think of this:
// nums is passed in by reference. (i.e., without making a copy)
int len = removeDuplicates(nums);
// any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
print(nums[i]);
}
//答案
var removeDuplicates = function(nums) {
const size = nums.length;
let slowP = 0;
for (let fastP = 0; fastP < size; fastP++) {
if (nums[fastP] !== nums[slowP]) {
slowP++;
nums[slowP] = nums[fastP]
}
}
return slowP + 1;
};
- 解题思路
使用快慢指针来记录遍历的坐标。
- 开始时这两个指针都指向第一个数字
- 如果两个指针指的数字相同,则快指针向前走一步
- 如果不同,则两个指针都向前走一步
- 当快指针走完整个数组后,慢指针当前的坐标加1就是数组中不同数字的个数
其他
0.1+0.2为什么等于0.30000000000000004,如何通过代码解决这个问题?
function add() {
const args = [...arguments]
const maxLen = Math.max.apply(
null,
args.map(item => {
const str = String(item).split('.')[1]
return str ? str.length : 0
})
)
return (
args.reduce((sum, cur) => sum + cur * 10 ** maxLen, 0) / 10 ** maxLen
)
}
console.log(add(0.1, 0.2)) // => 0.3
console.log(add(10, 11)) // => 21
console.log(add(0.001, 0.003)) // => 0.004
console.log(add(0.001, 0.003, 0.005)) // => 0.009
console.log(add(0.001)) // => 0.001
- 解题思路
将浮点数转为整数
- 先将数字转为字符串再转数组计算出有多少个小数;
- 通过reduce函数将 计算好的整数相加,再转为小数;