forked from azl397985856/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
luzhipeng
committed
May 29, 2019
1 parent
3032758
commit aea6437
Showing
14 changed files
with
542 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<mxfile modified="2019-05-29T06:57:33.441Z" host="www.draw.io" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36" etag="QQSD99Je1HArhkPWcUTv" version="10.7.0" type="device"><diagram id="UEnr-eE_yVzsJPlaBaPp" name="第 1 页">7Zpdb5s6GMc/jS8XAebFXAIlPZPOdI7UStOuJgoOsQaYA26Snk8/m5gEsNt1W9KiJTeJefyC/fz89xsGMCp3t01Srz/RDBfAMrIdgDfAskzoI/4nLE97C/Lh3pA3JJOJjoY78j+WRkNaH0mG21FCRmnBSD02prSqcMpGtqRp6HacbEWL8VvrJMeK4S5NCtX6mWRsLVtheUf7X5jk6/7NpuvvY8qkTyxb0q6TjG4HJhgDGDWUsn2o3EW4EM7r/bLPt3wm9lCxBlfsNRms5Da4/fQ5Ltf/BPe7p6jxvtx/EBlEMZukeJQtlrVlT70LGvpYZViUYgIYbteE4bs6SUXslkPntjUrCxm9ohWTFE1bPke0oE1XFgwdD/o33N6yhn7Dg5jARUaIREzvJ1GcrB1uGN49227z4E3eDTEtMWueeBKZAckeKDugaUse2yNO15G29QCl0ydMZBfKD0UfvcwD0tE/4XRT8Xnf2wde5w1mY9eOXVbRCgv/kqKYmJKC5BV/TLmDMLeHwn2Ed+lARpQky8RrtCyPtI0xTss4DQ/LNRbOCAlSidgaINa5eKgagJeDw/bnhgOqQ1K8BGEE0PJysEDPmRkWWx21LgeHhaY43p2Ho/BQp+4/lge0Z8fDvehZxJgdD0/hYV8QD3d2PJA6rU9x1JQIb8Yb3sq2B9JvB4RnsqRdH9z2I2pF8oCLf2lLGKFaVH9PEjxQxmipYclorUPOq1aLmpe7XGx+F2WbJniR4brBacJwtqhpy1N+7fahk27EdzyGEUZL60zzla/wt1T+lm7bcy7+vmZZ5wDk8WWdCWIXBBEITNjZliBYgtgGYQgCpwv4oN/GzVm9L+54T0HVnq7VTV+lakLnDWVtqrvZq67PpmvbhPNStak5QOplDa+yft1i1pkcUb2/qDV78KuotaLOCC9A1mmLW3aqLjHWOTSdd9a5uv0/6Ny66vyVi7Lp9A2RivWNla4eI1yV/pZKt5WDvvfXunqUcdC6fdX6Ly3UZqB09UDkqvQfK72ijeB1ij5hKofItqM5lNGdypzvW6RuW+4CPxIijxEIbRCgzoI6pXdRyBfaDwMhf2HhYQPEnhgEUDdA+MtuNHBFyjDuApEYP3iuIAZ+IBLzX5GYjyGG+Lgz7Ypj5f7sV+cx6CzBaJUq3ZTHuCnCD6sTTe/QHsF1+gPHAdzDfYghXN89E9x+LHlJ8klb7+9NrMhOuHvo1ho3hNcEvzQcPz88kLK7WzGCYx3tN6TMeasK8iDaJpTL/z9WK/rVWrSb/ERMzOmSy9FITvv5/2xfm3V7K7dg3bizGcFx/3sUt0M6D35oOxcGPIFl17vOPX08D+XyvyunrZPq9wpyQssHDs9p3JANyTga435L+e9Hjj3HTdu/irtg/7ZxDbi5a0xvneVyYHTTZDgHaNYGXhy48cluQIzXBh7UHOIgzdrgF2YB/ni8YtTFDS5qwfg7</diagram></mxfile> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<mxfile modified="2019-05-29T08:21:49.168Z" host="www.draw.io" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36" etag="Q9yys75j99bNvDjmcXMv" version="10.7.0" type="device"><diagram id="71IVt6DXrzH63Tv4SlmF" name="第 1 页">7Vxdb+I4FP01PBYlMQnhkbYwI+2uVKkrDfu0chMDngkxctwC++vXTuwQfGnLzLJ1LLUvxTeO7Zx7j7kfJgN0t9l/4Xi7/oPlpBhEQb4foPtBFIVoksp/SnJoJOlo1AhWnOa601HwSP8hWhho6TPNSXXSUTBWCLo9FWasLEkmTmSYc7Y77bZkxemsW7wiQPCY4QJKv9FcrPVTROOj/Cuhq7WZOUwmzZUNNp31k1RrnLNdR4RmA3THGRPNp83+jhQKPINLc9/8lavtwjgpxSU3LHL0bbGf7dff0ddFdaj+jOe/3ehRXnDxfPrAlTgYCDh7LnOiRgkG6Ha3poI8bnGmru6k0qVsLTaFbIXy45KVQmsxTHT7jhWM12OhIEAonEq5npdwQfavPlDYwiTti7ANEfwgu5gbAo2sNq3QIL07KirWonVHR0aGtWms2pGP6MkPGsCfADMCYEb+gBn3DEwEwETegBn1zTJjAObIGzABzceOwUwAmLE/YNo0dw3mGICZeAMmoLlrMFMA5tgbMG2aR673zAkAM/UHzLhnYBp3voPmxBs0bZ67RxO67f4QPU565hyF0G/3xztKwr6hCR13f0LKpHe2OfL4Wwgw3bV/FMI4yB/XHTDdOZowEPIn3wGY7hxNGAn54yHZTHfvIcFQyJ+40ma6ezRhLORPMs5munM0zfwdNAGYW0ZLQfjsRT5kpTFrCwgK3xxX6xbsDrCV4OwHMVCWrJQD3hb4iRQPrKKCslKKM6IGlxcUsDTDxe9WhycmBNt0OkwLulIXBF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v</diagram></mxfile> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<mxfile modified="2019-05-29T07:51:03.040Z" host="www.draw.io" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36" etag="BsbFmJvolCeadDQuiTwE" version="10.7.0"><diagram id="vLQqbmewtO1LD4aSa5GU" name="第 1 页">3ZnRkpowFIafhkt3gADKpVq105l2uvWi1ylkId1AKERFn74HCSBGZ9bWHbNcCX+SE/Ll58wJGmielKscZ/FXHhJm2GZYGuiTYdsW8ifwUyn7Wpk4Ti1EOQ1lp05Y0wORoinVDQ1J0esoOGeCZn0x4GlKAtHTcJ7zXb/bC2f9WTMcEUVYB5ip6k8ailiuwh53+mdCo7iZ2fL8uiXBTWe5kiLGId+dSGhhoHnOuaivknJOWAWv4VKPW15pbR8sJ6l4y4DVgca/v/zg7vPraDbbf3tm/PtIRtlitpELNmyPQbzZC4ewthdV11YjQvRTXS5M7BtaOd+kIakmNKF5F1NB1hkOqtYd+AO0WCQM7iw5w5wznh/HItP0PITaqFuSC1JeXavVEgTrEZ4Qke+hSzPAk9Cl69pN2HV72HSJT7bPlRqWronayB1YuJBsb+BsK5zN/4N3B0jtM+gCCekIaaIZJEdDSMjRDJKrQHp6eno4JsfUDJP3puxvf7Ts7+iW/ccavrOubtl/oiMk3bK/pRZsWKEE6xV9FIXI+StpXreUp6R6AyljZxJmNErhNgA+BPRZRY9CNTyVDQkNw2qai+z7u3MH/Nb4DL+v4ncu4LffDb9ax/0aLn7IyprhVyvEYMD4fd3wq7VnOFz8yNUNv1rVlsPF72iX+9VqeT9c/K52uV8tog/Dxe+dHxUfjl8tz5tjYUi3vV3w/mx4c4YcFccvulPoYHlZ2TV2h8g6SJHh9N+jBMBfFCfn1DpcfwqQj4965fSqh3fOjsXLJULHY3Gly6/jkIju4rHzBIvcB1vMv4vFjht+xWX3inOTWy8GcmeOb7gw0lyBATL4naY4ynFyq4s/rrfHi6m3MN/H2+ffttti7sTc6JK7vdvdDbfdvzjHtpP/wtDiLw==</diagram></mxfile> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
## 题目地址 | ||
https://leetcode.com/problems/divide-two-integers/description/ | ||
|
||
## 题目描述 | ||
``` | ||
Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator. | ||
Return the quotient after dividing dividend by divisor. | ||
The integer division should truncate toward zero. | ||
Example 1: | ||
Input: dividend = 10, divisor = 3 | ||
Output: 3 | ||
Example 2: | ||
Input: dividend = 7, divisor = -3 | ||
Output: -2 | ||
Note: | ||
Both dividend and divisor will be 32-bit signed integers. | ||
The divisor will never be 0. | ||
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 231 − 1 when the division result overflows. | ||
``` | ||
|
||
## 思路 | ||
|
||
符合直觉的做法是,减数一次一次减去被减数,不断更新差,直到差小于0,我们减了多少次,结果就是多少。 | ||
|
||
核心代码: | ||
|
||
```js | ||
let acc = divisor; | ||
let count = 0; | ||
|
||
while (dividend - acc >= 0) { | ||
acc += divisor; | ||
count++; | ||
} | ||
|
||
return count; | ||
|
||
``` | ||
|
||
这种做法简单直观,但是性能却比较差. 下面来介绍一种性能更好的方法。 | ||
|
||
![29.divide-two-integers](../assets/problems/29.divide-two-integers.png) | ||
|
||
通过上面这样的分析,我们直到可以使用二分法来解决,性能有很大的提升。 | ||
|
||
## 关键点解析 | ||
|
||
- 二分查找 | ||
|
||
- 正负数的判断中,这样判断更简单。 | ||
|
||
```js | ||
const isNegative = dividend > 0 !== divisor > 0; | ||
``` | ||
|
||
|
||
## 代码 | ||
|
||
```js | ||
|
||
|
||
/* | ||
* @lc app=leetcode id=29 lang=javascript | ||
* | ||
* [29] Divide Two Integers | ||
*/ | ||
/** | ||
* @param {number} dividend | ||
* @param {number} divisor | ||
* @return {number} | ||
*/ | ||
var divide = function(dividend, divisor) { | ||
if (divisor === 1) return dividend; | ||
|
||
// 这种方法很巧妙,即符号相同则为正,不同则为负 | ||
const isNegative = dividend > 0 !== divisor > 0; | ||
|
||
const MAX_INTERGER = Math.pow(2, 31); | ||
|
||
const res = helper(Math.abs(dividend), Math.abs(divisor)); | ||
|
||
// overflow | ||
if (res > MAX_INTERGER - 1 || res < -1 * MAX_INTERGER) { | ||
return MAX_INTERGER - 1; | ||
} | ||
|
||
return isNegative ? -1 * res : res; | ||
}; | ||
|
||
function helper(dividend, divisor) { | ||
// 二分法 | ||
if (dividend <= 0) return 0; | ||
if (dividend < divisor) return 0; | ||
if (divisor === 1) return dividend; | ||
|
||
let acc = 2 * divisor; | ||
let count = 1; | ||
|
||
while (dividend - acc > 0) { | ||
acc += acc; | ||
count += count; | ||
} | ||
// 直接使用位移运算,比如acc >> 1会有问题 | ||
const last = dividend - Math.floor(acc / 2); | ||
|
||
return count + helper(last, divisor); | ||
} | ||
``` | ||
|
||
## 相关题目 | ||
[875.koko-eating-bananas](./875.koko-eating-bananas.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
## 题目地址 | ||
|
||
https://leetcode.com/problems/rotate-image/description/ | ||
|
||
## 题目描述 | ||
|
||
``` | ||
You are given an n x n 2D matrix representing an image. | ||
Rotate the image by 90 degrees (clockwise). | ||
Note: | ||
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation. | ||
Example 1: | ||
Given input matrix = | ||
[ | ||
[1,2,3], | ||
[4,5,6], | ||
[7,8,9] | ||
], | ||
rotate the input matrix in-place such that it becomes: | ||
[ | ||
[7,4,1], | ||
[8,5,2], | ||
[9,6,3] | ||
] | ||
Example 2: | ||
Given input matrix = | ||
[ | ||
[ 5, 1, 9,11], | ||
[ 2, 4, 8,10], | ||
[13, 3, 6, 7], | ||
[15,14,12,16] | ||
], | ||
rotate the input matrix in-place such that it becomes: | ||
[ | ||
[15,13, 2, 5], | ||
[14, 3, 4, 1], | ||
[12, 6, 8, 9], | ||
[16, 7,10,11] | ||
] | ||
``` | ||
|
||
## 思路 | ||
|
||
这道题目让我们 in-place,也就说空间复杂度要求 O(1),如果没有这个限制的话,很简单。 | ||
|
||
通过观察发现,我们只需要将第 i 行变成第 n - i - 1 列, 因此我们只需要保存一个原有矩阵,然后按照这个规律一个个更新即可。 | ||
|
||
![48.rotate-image-1](../assets/problems/48.rotate-image-1.png) | ||
|
||
代码: | ||
|
||
```js | ||
var rotate = function(matrix) { | ||
// 时间复杂度O(n^2) 空间复杂度O(n) | ||
const oMatrix = JSON.parse(JSON.stringify(matrix)); // clone | ||
const n = oMatrix.length; | ||
for (let i = 0; i < n; i++) { | ||
for (let j = 0; j < n; j++) { | ||
matrix[j][n - i - 1] = oMatrix[i][j]; | ||
} | ||
} | ||
}; | ||
``` | ||
|
||
如果要求空间复杂度是O(1)的话,我们可以用一个temp记录即可,这个时候就不能逐个遍历了。 | ||
比如遍历到1的时候,我们把1存到temp,然后更新1的值为7。 1被换到了3的位置,我们再将3存到temp,依次类推。 | ||
但是这种解法写起来比较麻烦,这里我就不写了。 | ||
|
||
事实上有一个更加巧妙的做法,我们可以巧妙地利用对称轴旋转达到我们的目的,如图,我们先进行一次以对角线为轴的翻转,然后 | ||
再进行一次以水平轴心线为轴的翻转即可。 | ||
|
||
![48.rotate-image-2](../assets/problems/48.rotate-image-2.png) | ||
|
||
这种做法的时间复杂度是O(n^2) ,空间复杂度是O(1) | ||
|
||
## 关键点解析 | ||
|
||
- 矩阵旋转操作 | ||
|
||
## 代码 | ||
|
||
```js | ||
/* | ||
* @lc app=leetcode id=48 lang=javascript | ||
* | ||
* [48] Rotate Image | ||
*/ | ||
/** | ||
* @param {number[][]} matrix | ||
* @return {void} Do not return anything, modify matrix in-place instead. | ||
*/ | ||
var rotate = function(matrix) { | ||
// 时间复杂度O(n^2) 空间复杂度O(1) | ||
|
||
// 做法: 先沿着对角线翻转,然后沿着水平线翻转 | ||
const n = matrix.length; | ||
function swap(arr, [i, j], [m, n]) { | ||
const temp = arr[i][j]; | ||
arr[i][j] = arr[m][n]; | ||
arr[m][n] = temp; | ||
} | ||
for (let i = 0; i < n - 1; i++) { | ||
for (let j = 0; j < n - i; j++) { | ||
swap(matrix, [i, j], [n - j - 1, n - i - 1]); | ||
} | ||
} | ||
|
||
for (let i = 0; i < n / 2; i++) { | ||
for (let j = 0; j < n; j++) { | ||
swap(matrix, [i, j], [n - i - 1, j]); | ||
} | ||
} | ||
}; | ||
``` |
Oops, something went wrong.