-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
提交作业0926-hard -4 #12
base: master
Are you sure you want to change the base?
提交作业0926-hard -4 #12
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2。 | ||
|
||
请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。 | ||
|
||
你可以假设 nums1 和 nums2 不会同时为空。 | ||
|
||
示例 1: | ||
|
||
nums1 = [1, 3] | ||
nums2 = [2] | ||
|
||
则中位数是 2.0 | ||
示例 2: | ||
|
||
nums1 = [1, 2] | ||
nums2 = [3, 4] | ||
|
||
则中位数是 (2 + 3)/2 = 2.5 | ||
*/ | ||
//非排序 | ||
var findMedianSortedArrays = function (nums1, nums2){ | ||
let len1 = nums1.length; | ||
let len2 = nums2.length; | ||
let sum = len1 + len2; | ||
let arr = []; | ||
let i, j; | ||
for (i = 0, j = 0; i < len1 && j < len2;){ | ||
if (nums1[i] < nums2[j]){ | ||
arr.push(nums1[i]); | ||
i++; | ||
} else{ | ||
arr.push(nums2[j]); | ||
j++; | ||
} | ||
} | ||
let start = i, end = len1 - 1; | ||
if (j < len2){ | ||
start = j, end = len2 - 1; | ||
while (start <= end){ | ||
arr.push(nums2[start++]); | ||
} | ||
} else{ | ||
while (start <= end){ | ||
arr.push(nums1[start++]); | ||
} | ||
} | ||
Comment on lines
+37
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这一段写的太复杂了,没必要再弄两个变量,直接两个循环就可以。 while (j < len2){} 由于这两个只会有一个被满足,所以不会有多余的运算。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
好的黄总 我想一下 |
||
let mid = Math.floor(sum / 2); | ||
return sum % 2 === 0?arr[mid] / 2 + arr[mid - 1] / 2:arr[mid]; | ||
}; | ||
//排序 | ||
var findMedianSortedArrays = function (nums1, nums2){ | ||
let nums = nums1.concat(nums2).sort((a,b)=>{return a-b}); | ||
nums1 = null;nums2 = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里为啥要把 nums1 和 nums2 置空,有什么必要么? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
没啥必要 就是想着手动释放内存 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 去掉吧,没必要...理论上垃圾回收应该不会很差 |
||
let len = nums.length,mid = Math.floor(len/2); | ||
return len%2===0?(nums[mid]+nums[mid-1])/2:nums[mid]; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* 将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。 | ||
|
||
比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: | ||
|
||
L C I R | ||
E T O E S I I G | ||
E D H N | ||
之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"LCIRETOESIIGEDHN"。 | ||
|
||
请你实现这个将字符串进行指定行数变换的函数: | ||
|
||
string convert(string s, int numRows); | ||
示例 1: | ||
|
||
输入: s = "LEETCODEISHIRING", numRows = 3 | ||
输出: "LCIRETOESIIGEDHN" | ||
示例 2: | ||
|
||
输入: s = "LEETCODEISHIRING", numRows = 4 | ||
输出: "LDREOEIIECIHNTSG" | ||
解释: | ||
|
||
L D R | ||
E O E I I | ||
E C I H N | ||
T S G | ||
|
||
*/ | ||
/** | ||
* @param {string} s | ||
* @param {number} numRows | ||
* @return {string} | ||
*/ | ||
var convert = function (s, numRows){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 写一下思路 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
好的 黄总 我写下思路 |
||
if(numRows === 1) return s; | ||
let res = []; | ||
let rows = numRows - 1; | ||
let r = 0; | ||
let count = 0; | ||
for (let i = 0; i < s.length; i++){ | ||
r = i===0?0:i % (rows * 2); | ||
count=i===0?0:Math.floor(i / (rows * 2)); | ||
Comment on lines
+42
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个是不是用一个 if 更好一些,可以少一次判断,且代码逻辑更加清晰 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
好的黄总 |
||
if (r <= rows){ | ||
if(!res[r]) res[r] = []; | ||
res[r][count*rows] = s[i]; | ||
} else{ | ||
res[rows*2-r][i - rows * (count+1)] = s[i] | ||
} | ||
} | ||
return res.join("") | ||
}; | ||
var convert1 = function convert(s, numRows){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个也写一下思路,不然代码太难懂了 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
好的黄总 |
||
if(numRows === 1) return s; | ||
let res = [],rows = numRows - 1, r = 0,count = 0,drows=rows*2,len=s.length; | ||
for (let i = 0; i < len ; i++){ | ||
r = i===0?0:i % drows; | ||
count=i===0?0:Math.floor(i /drows); | ||
if (r <= rows){ | ||
if(!res[r]) res[r] = ""; | ||
res[r]+=s[i]; | ||
} else{ | ||
res[drows-r]+=s[i]; | ||
} | ||
} | ||
return res.join("") | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个变量命名不是很喜欢,可以用 length, length_sum,为啥要单用 sum...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
老子乐意
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
滚