Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions leetcode/wxs/hard/4-190926-findMedianSortedArrays.js
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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个变量命名不是很喜欢,可以用 length, length_sum,为啥要单用 sum...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个变量命名不是很喜欢,可以用 length, length_sum,为啥要单用 sum...

老子乐意

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这一段写的太复杂了,没必要再弄两个变量,直接两个循环就可以。
while (i < ken1){}

while (j < len2){}

由于这两个只会有一个被满足,所以不会有多余的运算。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这一段写的太复杂了,没必要再弄两个变量,直接两个循环就可以。
while (i < ken1){}

while (j < len2){}

由于这两个只会有一个被满足,所以不会有多余的运算。

好的黄总 我想一下

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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里为啥要把 nums1 和 nums2 置空,有什么必要么?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里为啥要把 nums1 和 nums2 置空,有什么必要么?

没啥必要 就是想着手动释放内存

Copy link
Owner

Choose a reason for hiding this comment

The 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];
}
8 changes: 4 additions & 4 deletions leetcode/wxs/normal/3-190925-lengthOfLongestSubstring.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
* 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。

示例 1:
示例 1:

输入: "abcabcbb"
输出: 3
Expand All @@ -15,8 +15,8 @@

输入: "pwwkew"
输出: 3
解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
  请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。
解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。
*/
/**
* @param {string} s
Expand Down
67 changes: 67 additions & 0 deletions leetcode/wxs/normal/6-190928-convert.js
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){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

写一下思路

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个是不是用一个 if 更好一些,可以少一次判断,且代码逻辑更加清晰

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个是不是用一个 if 更好一些,可以少一次判断,且代码逻辑更加清晰

好的黄总

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){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个也写一下思路,不然代码太难懂了

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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("")
};