LeetCode做错的题(个人向纪录)
原创查看一个 用ES6写 两数之和 的方法 , 自己录制 学习一下
题目:
给定整数数组 nums 以及目标值 target,请找到数组中目标值的总和 两个 整数,并返回其数组下标。
您可以假设每个输入只对应一个答案。但是,数组中的同一元素不能使用两次。
示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
var twoSum = function(nums, target) {
var map=new Map();
for(let i=0;i
77
剑指 Offer 32 - I. 从上到下打印二叉树
剑指 Offer 32 - II. 从上到下打印二叉树 II
40
617 组合二叉树
-
从中间和后序遍历序列构建二叉树。
/**
- Definition for a binary tree node.
- public class TreeNode {
- int val;
- TreeNode left;
- TreeNode right;
- TreeNode(int x) { val = x; }
-
} */ class Solution { public TreeNode buildTree(int[] inorder, int[] postorder) { int inLen = inorder.length; int postLen = postorder.length; if(inLen != postLen){ throw new RuntimeException("两个遍历输入错误"); }
return createTree(inorder, 0, inLen - 1, postorder, 0, postLen - 1); } public TreeNode createTree(int[] inorder, int inLeft, int inRight, int []postorder, int postLeft, int postRight){ if( inLeft > inRight || postLeft > postRight){ return null; } int pivot = postorder[postRight]; int pivotIndex = 0; while(inorder[pivotIndex] != pivot){ pivotIndex++; } TreeNode root = new TreeNode(inorder[pivotIndex]); // 不要混淆这里的左右子树, root.left, 就是看 中序 和 后一阶的左子树, right 是正确的吗 // 然后最好画一幅画, 否则,这不是很容易理解 root.left = createTree(inorder, inLeft, pivotIndex - 1, postorder, postLeft, postRight - inRight + pivotIndex - 1); root.right = createTree(inorder, pivotIndex + 1, inRight, postorder, postRight - inRight + pivotIndex, postRight -1); return root;}
}
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/two-sum
版权归网络所有。商业转载请联系官方授权,并注明非商业转载的来源。
版权声明
所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除