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.
/** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; * } */ /** * @param {ListNode}l1 * @param {ListNode}l2 * @return {ListNode} */ var mergeTwoLists = function(l1, l2) { if (!l1 && !l2) { returnnull } let array1 = listNodeToArray(l1) let array2 = listNodeToArray(l2) let array = array1.concat(array2) array.sort((a, b) => (a - b)) return arrayToListNode(array) };
functionlistNodeToArray (head) { let array = [] while (head) { array.push(head.val) head = head.next } return array }
functionarrayToListNode(array) { if(!array || !array.length) { returnnull } let node let head = new ListNode(array[0]) let pnode = head for(let i = 1; i < array.length; i++) { node = new ListNode(array[i]) pnode.next = node pnode = node } return head }
Given an array nums and a value val, remove all instances of that value in-place 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.
The order of elements can be changed. It doesn’t matter what you leave beyond the new length.
Example 1:
Given nums = [3,2,2,3], val = 3,
Your function should return length = 2, with the first two elements of nums being 2.
It doesn't matter what you leave beyond the returned length.
Example 2:
Given nums = [0,1,2,2,3,0,4,2], val = 2,
Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.
Note that the order of those five elements can be arbitrary.
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 = removeElement(nums, val);
// 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]);
}
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Example 1:
Input: [1,3,5,6], 5 Output: 2 Example 2:
Input: [1,3,5,6], 2 Output: 1 Example 3:
Input: [1,3,5,6], 7 Output: 4 Example 4:
Input: [1,3,5,6], 0 Output: 0
解题方案
思路 1 **- 时间复杂度: O(logN)**- 空间复杂度: O(N)**
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/** * @param {number[]}nums * @param {number}target * @return {number} */ let searchInsert = function(nums, target) { let lo = 0,high = nums.length-1; while(lo<=high){ let mid = Math.floor((high-lo)/2)+lo; if(nums[mid]===target) return mid; if(nums[mid]>target) high = mid-1; else lo = mid+1; } return lo; };
var rotate = function(matrix) { if (!matrix || !matrix.length) { returnnull } let newMatrix = [] let m = matrix[0].length let n = matrix.length for (let y = m - 1; y >= 0; y--) { let newLine = [] for (let x = n - 1; x >= 0; x--) { newLine.unshift(matrix[x][y]) } newMatrix.push(newLine); } return newMatrix; }