手写算法:

  • 链接:https://leetcode-cn.com/problems/two-sum
  • 题目:
    • 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
    • 你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
    • 你可以按任意顺序返回答案。
  • 解题 ```javascript var twoSum = function(nums, target) { let obj = {} for (let i=0,len=nums.length;i<len;i++) {
    1. if (nums[i] in obj) {
    2. return [obj[nums[i]],i]
    3. } else {
    4. obj[target - nums[i]] = i
    5. }
    }

};

  1. <a name="Auho3"></a>
  2. ### 手写题
  3. - 题目
  4. ```javascript
  5. function compose(){
  6. //请实现
  7. let funs = arguments
  8. if (funs.length===0) {
  9. return null
  10. }
  11. if (funs.length===1) {
  12. return funs()
  13. }
  14. return funs.reduce((a,b)=>(...args)=>a(b(args)))
  15. }
  16. const multiply20 = (price) => price * 20;
  17. const divide100 = (price) => price / 100;
  18. const normalizePrice = (price) => price.toFixed(2);
  19. const discount = compose(normalizePrice, divide100, multiply20);
  20. discount(200.0); //40.00