-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update: 添加问题“3264.K次乘运算后的最终数组I”的代码和题解
- Loading branch information
1 parent
39ee7ec
commit a6db8cc
Showing
10 changed files
with
301 additions
and
173 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
Codes/3264-final-array-state-after-k-multiplication-operations-i.cpp
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,26 @@ | ||
/* | ||
* @Author: LetMeFly | ||
* @Date: 2024-12-13 09:32:29 | ||
* @LastEditors: LetMeFly.xyz | ||
* @LastEditTime: 2024-12-13 09:33:35 | ||
*/ | ||
#ifdef _WIN32 | ||
#include "_[1,2]toVector.h" | ||
#endif | ||
|
||
class Solution { | ||
public: | ||
vector<int> getFinalState(vector<int>& nums, int k, int multiplier) { | ||
while (k--) { | ||
int m = nums[0], loc = 0; | ||
for (int i = 1; i < nums.size(); i++) { | ||
if (nums[i] < m) { | ||
m = nums[i]; | ||
loc = i; | ||
} | ||
} | ||
nums[loc] *= multiplier; | ||
} | ||
return nums; | ||
} | ||
}; |
20 changes: 20 additions & 0 deletions
20
Codes/3264-final-array-state-after-k-multiplication-operations-i.go
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,20 @@ | ||
/* | ||
* @Author: LetMeFly | ||
* @Date: 2024-12-13 09:40:31 | ||
* @LastEditors: LetMeFly.xyz | ||
* @LastEditTime: 2024-12-13 09:42:10 | ||
*/ | ||
package main | ||
|
||
func getFinalState(nums []int, k int, multiplier int) []int { | ||
for round := 0; round < k; round++ { | ||
m, loc := nums[0], 0 | ||
for i, val := range nums { | ||
if val < m { | ||
m, loc = val, i | ||
} | ||
} | ||
nums[loc] *= multiplier | ||
} | ||
return nums | ||
} |
21 changes: 21 additions & 0 deletions
21
Codes/3264-final-array-state-after-k-multiplication-operations-i.java
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,21 @@ | ||
/* | ||
* @Author: LetMeFly | ||
* @Date: 2024-12-13 09:37:28 | ||
* @LastEditors: LetMeFly.xyz | ||
* @LastEditTime: 2024-12-13 09:40:00 | ||
*/ | ||
class Solution { | ||
public int[] getFinalState(int[] nums, int k, int multiplier) { | ||
for (int round = 0; round < k; round++) { | ||
int m = nums[0], loc = 0; | ||
for (int i = 1; i < nums.length; i++) { | ||
if (nums[i] < m) { | ||
m = nums[i]; | ||
loc = i; | ||
} | ||
} | ||
nums[loc] *= multiplier; | ||
} | ||
return nums; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
Codes/3264-final-array-state-after-k-multiplication-operations-i.py
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,17 @@ | ||
''' | ||
Author: LetMeFly | ||
Date: 2024-12-13 09:34:55 | ||
LastEditors: LetMeFly.xyz | ||
LastEditTime: 2024-12-13 09:36:26 | ||
''' | ||
from typing import List | ||
|
||
class Solution: | ||
def getFinalState(self, nums: List[int], k: int, multiplier: int) -> List[int]: | ||
for _ in range(k): | ||
m, loc = nums[0], 0 | ||
for i, val in enumerate(nums): | ||
if val < m: | ||
m, loc = val, i | ||
nums[loc] *= multiplier | ||
return nums |
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,198 @@ | ||
--- | ||
title: 3264.K 次乘运算后的最终数组 I | ||
date: 2024-12-13 09:43:07 | ||
tags: [题解, LeetCode, 简单, 数组, 数学, 模拟, 堆(优先队列)] | ||
--- | ||
|
||
# 【LetMeFly】3264.K 次乘运算后的最终数组 I:模拟 | ||
|
||
力扣题目链接:[https://leetcode.cn/problems/final-array-state-after-k-multiplication-operations-i/](https://leetcode.cn/problems/final-array-state-after-k-multiplication-operations-i/) | ||
|
||
<p>给你一个整数数组 <code>nums</code> ,一个整数 <code>k</code> 和一个整数 <code>multiplier</code> 。</p> | ||
|
||
<p>你需要对 <code>nums</code> 执行 <code>k</code> 次操作,每次操作中:</p> | ||
|
||
<ul> | ||
<li>找到 <code>nums</code> 中的 <strong>最小</strong> 值 <code>x</code> ,如果存在多个最小值,选择最 <strong>前面</strong> 的一个。</li> | ||
<li>将 <code>x</code> 替换为 <code>x * multiplier</code> 。</li> | ||
</ul> | ||
|
||
<p>请你返回执行完 <code>k</code> 次乘运算之后,最终的 <code>nums</code> 数组。</p> | ||
|
||
<p> </p> | ||
|
||
<p><strong class="example">示例 1:</strong></p> | ||
|
||
<div class="example-block"> | ||
<p><span class="example-io"><b>输入:</b>nums = [2,1,3,5,6], k = 5, multiplier = 2</span></p> | ||
|
||
<p><span class="example-io"><b>输出:</b>[8,4,6,5,6]</span></p> | ||
|
||
<p><strong>解释:</strong></p> | ||
|
||
<table> | ||
<tbody> | ||
<tr> | ||
<th>操作</th> | ||
<th>结果</th> | ||
</tr> | ||
<tr> | ||
<td>1 次操作后</td> | ||
<td>[2, 2, 3, 5, 6]</td> | ||
</tr> | ||
<tr> | ||
<td>2 次操作后</td> | ||
<td>[4, 2, 3, 5, 6]</td> | ||
</tr> | ||
<tr> | ||
<td>3 次操作后</td> | ||
<td>[4, 4, 3, 5, 6]</td> | ||
</tr> | ||
<tr> | ||
<td>4 次操作后</td> | ||
<td>[4, 4, 6, 5, 6]</td> | ||
</tr> | ||
<tr> | ||
<td>5 次操作后</td> | ||
<td>[8, 4, 6, 5, 6]</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</div> | ||
|
||
<p><strong class="example">示例 2:</strong></p> | ||
|
||
<div class="example-block"> | ||
<p><span class="example-io"><b>输入:</b></span>nums = [1,2], k = 3, multiplier = 4</p> | ||
|
||
<p><span class="example-io"><b>输出:</b></span>[16,8]</p> | ||
|
||
<p><strong>解释:</strong></p> | ||
|
||
<table> | ||
<tbody> | ||
<tr> | ||
<th>操作</th> | ||
<th>结果</th> | ||
</tr> | ||
<tr> | ||
<td>1 次操作后</td> | ||
<td>[4, 2]</td> | ||
</tr> | ||
<tr> | ||
<td>2 次操作后</td> | ||
<td>[4, 8]</td> | ||
</tr> | ||
<tr> | ||
<td>3 次操作后</td> | ||
<td>[16, 8]</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
</div> | ||
|
||
<p> </p> | ||
|
||
<p><strong>提示:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= nums.length <= 100</code></li> | ||
<li><code>1 <= nums[i] <= 100</code></li> | ||
<li><code>1 <= k <= 10</code></li> | ||
<li><code>1 <= multiplier <= 5</code></li> | ||
</ul> | ||
|
||
|
||
|
||
## 解题方法:模拟 | ||
|
||
进行$k$次如下操作: | ||
|
||
> 每次找到最小值所在位置,将最小值乘以$multiplier$ | ||
最后返回$nums$原数组。 | ||
|
||
+ 时间复杂度$O(len(nums)\times k)$ | ||
+ 空间复杂度$O(1)$ | ||
|
||
### AC代码 | ||
|
||
#### C++ | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
vector<int> getFinalState(vector<int>& nums, int k, int multiplier) { | ||
while (k--) { | ||
int m = nums[0], loc = 0; | ||
for (int i = 1; i < nums.size(); i++) { | ||
if (nums[i] < m) { | ||
m = nums[i]; | ||
loc = i; | ||
} | ||
} | ||
nums[loc] *= multiplier; | ||
} | ||
return nums; | ||
} | ||
}; | ||
``` | ||
#### Python | ||
```python | ||
from typing import List | ||
class Solution: | ||
def getFinalState(self, nums: List[int], k: int, multiplier: int) -> List[int]: | ||
for _ in range(k): | ||
m, loc = nums[0], 0 | ||
for i, val in enumerate(nums): | ||
if val < m: | ||
m, loc = val, i | ||
nums[loc] *= multiplier | ||
return nums | ||
``` | ||
|
||
#### Java | ||
|
||
```java | ||
class Solution { | ||
public int[] getFinalState(int[] nums, int k, int multiplier) { | ||
for (int round = 0; round < k; round++) { | ||
int m = nums[0], loc = 0; | ||
for (int i = 1; i < nums.length; i++) { | ||
if (nums[i] < m) { | ||
m = nums[i]; | ||
loc = i; | ||
} | ||
} | ||
nums[loc] *= multiplier; | ||
} | ||
return nums; | ||
} | ||
} | ||
``` | ||
|
||
#### Go | ||
|
||
```go | ||
package main | ||
|
||
func getFinalState(nums []int, k int, multiplier int) []int { | ||
for round := 0; round < k; round++ { | ||
m, loc := nums[0], 0 | ||
for i, val := range nums { | ||
if val < m { | ||
m, loc = val, i | ||
} | ||
} | ||
nums[loc] *= multiplier | ||
} | ||
return nums | ||
} | ||
``` | ||
|
||
> 同步发文于CSDN和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2024/12/13/LeetCode%203264.K%E6%AC%A1%E4%B9%98%E8%BF%90%E7%AE%97%E5%90%8E%E7%9A%84%E6%9C%80%E7%BB%88%E6%95%B0%E7%BB%84I/)哦~ | ||
> | ||
> Tisfy:[https://letmefly.blog.csdn.net/article/details/144442552](https://letmefly.blog.csdn.net/article/details/144442552) |
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 |
---|---|---|
|
@@ -35,9 +35,17 @@ git clone --branch paper --single-branch [email protected]:LetMeFly666/SecFFT.git | |
git reset --hard/--soft/--mixed HEAD^ | ||
``` | ||
|
||
<font color="red">下面先别看了,写的不对</font> | ||
其中: | ||
|
||
其中`--hard`会强制变成上个版本(工作区暂存区清空),`--soft`会将相对上一个版本的变化放到暂存区,`--mixed`(默认选项)会将相对上一个版本的变化放到工作区。 | ||
+ `--hard`会强制变成上个版本(工作区暂存区清空) | ||
+ `--soft`会将相对上一个版本的变化保留到暂存区和工作区(已经add到暂存区过的变化还在暂存区 未add的变化还在工作区) | ||
+ `--mixed`(默认选项)会将相对上一个版本的变化全部放到工作区。 | ||
|
||
另一个解释版本: | ||
|
||
+ `--hard`:完全回退提交,丢弃暂存区和文件的所有修改。 | ||
+ `--soft`:回退提交,但保留文件和暂存区的修改。 | ||
+ `--mixed`:回退提交,丢弃暂存区的修改,但保留文件的修改。 | ||
|
||
举个例子: | ||
|
||
|
@@ -134,7 +142,7 @@ git reset --hard/--soft/--mixed HEAD^ | |
> | ||
> 注意Windows系统中`cmd`中的`^`大概是连接符的意思,可以使用`git reset --hard "HEAD^"`或`git reset --hard HEAD"^"`或`git reset --hard HEAD^^`来表示`HEAD^`。 | ||
> | ||
> 请注意,如果有未跟踪的内容(例如`echo 4 > 4`但是不`git add`),那么无论`git reset`时传递哪个参数,文件`4`都会原封不动地躺在工作区(这是因为历史记录中也没有文件`4`) TODO: 历史记录中存在文件4 | ||
> 如果有**从**未跟踪的内容(例如`echo 4 > 4`但是不`git add`),那么无论`git reset`时传递哪个参数,文件`4`都会原封不动地躺在工作区(这是因为历史记录中也没有文件`4`) | ||
#### 查看日志/commit记录 | ||
|
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
Oops, something went wrong.