<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>semd mail demo</title> <meta name="Description" content="geovindu,涂聚文,Geovin Du"/> <meta name="Keywords" content="geovindu,涂聚文,Geovin Du"/> <meta name="author" content="geovindu,涂聚文,Geovin Du"/> <script type="text/javascript" src="js/jquery-3.6.0.js"></script> <script src="Bootstrap/3.3.7/js/bootstrap.min.js"></script> <link href="Bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"> <script type="text/javascript"> // 1.2 空间复杂度 来源: JavaScript算法 作者: 司徒正美,李晓晨著,人民邮电出版社,2023 // ,空间复杂度(space complexity)是对一个算法在运行过程中临时占用存储空间大小的量度。 // 给定一个整数数组nums和一个整数目标值target,请你在该数组中找出“和”为目标值target的那两个整数,并返回它们的数组下标 // 方法一:暴力枚举 时间复杂度为O(n2) 空间复杂度为O(1) 采用“就地”存储 var nums=[4,0,2,9]; var target=[4]; function twoSum(nums, target) { console.log(nums); console.log(target); for (let i = 0; i < nums.length; i++) { for (let j = i + 1; j < nums.length; j++) { if (target - nums[i] === nums[j]) { console.log(nums[i]); return [i, j]; } } } console.log("No two sum1 solution"); }; //var twoSum = var sum1=twoSum(nums, target); console.log("数:"+sum1); // 方法二:散列表 散列表进行元素存储 消耗的空间复杂度为O(n) 时间复杂度的消耗,此处为O(n)。 //var twoSum2 = function twoSum2(nums, target) { console.log(nums); console.log(target); var map = new Map(); for (let i = 0; i < nums.length; i++) { let complement = target - nums[i]; if (map.has(complement)) { console.log(i); return [map.get(complement), i]; } map.set(nums[i], i); } console.log("No two sum2 solution"); }; //var twoSum2= var sum2=twoSum2(nums, target); console.log("数:"+sum2); var src = [1,1,2,1,4,1,3,5,7,9]; var dest = [2,4,6,8,10,12]; function mergeArray(src, dest, n, m) { var indexOfNew = n + m - 1; // 新数组的末位索引 var indexOfSrc = n - 1; // src有效元素的末位索引 var indexOfDest = m - 1; // dest有效元素的末位索引 // 当dest全部扫描完成,元素全部插入src,剩余src元素不需要移位操作 // 当src全部遍历完成,但dest仍有元素时,只需要操作dest即可,此时src下标已达最小值0 while (indexOfDest >= 0) { if (indexOfSrc >= 0) { // 将src或dest中较大的元素放入src的后几位中去 if (src[indexOfSrc] >= dest[indexOfDest]) { src[indexOfNew] = src[indexOfSrc]; --indexOfNew; --indexOfSrc; } else { src[indexOfNew] = dest[indexOfDest]; --indexOfNew; --indexOfDest; } } else { // 如果dest比较长,那么挪动dest到indexOfNew位置上 src[indexOfNew] = dest[indexOfDest]; --indexOfNew; --indexOfDest; } } } mergeArray(src,dest,10,6); console.log("合并的数组:"+src); </script> </head> <body> <form> <div class="form-group"> <label for="exampleFormControlInput1">Email address</label> <input type="email" class="form-control" id="exampleFormControlInput1" placeholder="[email protected]"> </div> <div class="form-group"> <label for="exampleInputName">Name</label> <input type="text" class="form-control" id="exampleInputPassword1" placeholder="enter name"> </div> <div class="form-group"> <label for="exampleFormControlSelect1">Example select</label> <select class="form-control" id="exampleFormControlSelect1"> <option>Mr.</option> <option>Ms</option> </select> </div> <div class="form-group" style="display:none;"> <label for="exampleFormControlTextarea1">内容</label> <textarea class="form-control" id="exampleFormControlTextarea1" rows="3"></textarea> </div> <button type="submit" class="btn btn-primary">Send Mail</button> </form> </body> </html>
标签:src,console,log,nums,dest,javascript,合并,数组,var From: https://www.cnblogs.com/geovindu/p/17777607.html