Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 828256e

Browse files
committed
feat: add python and java solutions to leetcode problem: No.1243
1 parent 8e2c3c1 commit 828256e

File tree

4 files changed

+120
-4
lines changed

4 files changed

+120
-4
lines changed

‎solution/1200-1299/1243.Array Transformation/README.md‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,53 @@
5858
<!-- 这里可写当前语言的特殊实现逻辑 -->
5959

6060
```python
61-
61+
class Solution:
62+
def transformArray(self, arr: List[int]) -> List[int]:
63+
copy = [e for e in arr]
64+
has_change, n = True, len(arr)
65+
while has_change:
66+
has_change = False
67+
for i in range(1, n - 1):
68+
if arr[i] < copy[i - 1] and arr[i] < copy[i + 1]:
69+
arr[i] += 1
70+
has_change = True
71+
elif arr[i] > copy[i - 1] and arr[i] > copy[i + 1]:
72+
arr[i] -= 1
73+
has_change = True
74+
copy = [e for e in arr]
75+
return arr
6276
```
6377

6478
### **Java**
6579

6680
<!-- 这里可写当前语言的特殊实现逻辑 -->
6781

6882
```java
69-
83+
class Solution {
84+
public List<Integer> transformArray(int[] arr) {
85+
int n = arr.length;
86+
int[] copy = Arrays.copyOf(arr, n);
87+
boolean hasChange = true;
88+
while (hasChange) {
89+
hasChange = false;
90+
for (int i = 1; i < n - 1; ++i) {
91+
if (arr[i] < copy[i - 1] && arr[i] < copy[i + 1]) {
92+
++arr[i];
93+
hasChange = true;
94+
} else if (arr[i] > copy[i - 1] && arr[i] > copy[i + 1]) {
95+
--arr[i];
96+
hasChange = true;
97+
}
98+
}
99+
System.arraycopy(arr, 0, copy, 0, n);
100+
}
101+
List<Integer> res = new ArrayList<>();
102+
for (int e : arr) {
103+
res.add(e);
104+
}
105+
return res;
106+
}
107+
}
70108
```
71109

72110
### **...**

‎solution/1200-1299/1243.Array Transformation/README_EN.md‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,51 @@ No more operations can be done to this array.
5353
### **Python3**
5454

5555
```python
56-
56+
class Solution:
57+
def transformArray(self, arr: List[int]) -> List[int]:
58+
copy = [e for e in arr]
59+
has_change, n = True, len(arr)
60+
while has_change:
61+
has_change = False
62+
for i in range(1, n - 1):
63+
if arr[i] < copy[i - 1] and arr[i] < copy[i + 1]:
64+
arr[i] += 1
65+
has_change = True
66+
elif arr[i] > copy[i - 1] and arr[i] > copy[i + 1]:
67+
arr[i] -= 1
68+
has_change = True
69+
copy = [e for e in arr]
70+
return arr
5771
```
5872

5973
### **Java**
6074

6175
```java
62-
76+
class Solution {
77+
public List<Integer> transformArray(int[] arr) {
78+
int n = arr.length;
79+
int[] copy = Arrays.copyOf(arr, n);
80+
boolean hasChange = true;
81+
while (hasChange) {
82+
hasChange = false;
83+
for (int i = 1; i < n - 1; ++i) {
84+
if (arr[i] < copy[i - 1] && arr[i] < copy[i + 1]) {
85+
++arr[i];
86+
hasChange = true;
87+
} else if (arr[i] > copy[i - 1] && arr[i] > copy[i + 1]) {
88+
--arr[i];
89+
hasChange = true;
90+
}
91+
}
92+
System.arraycopy(arr, 0, copy, 0, n);
93+
}
94+
List<Integer> res = new ArrayList<>();
95+
for (int e : arr) {
96+
res.add(e);
97+
}
98+
return res;
99+
}
100+
}
63101
```
64102

65103
### **...**
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public List<Integer> transformArray(int[] arr) {
3+
int n = arr.length;
4+
int[] copy = Arrays.copyOf(arr, n);
5+
boolean hasChange = true;
6+
while (hasChange) {
7+
hasChange = false;
8+
for (int i = 1; i < n - 1; ++i) {
9+
if (arr[i] < copy[i - 1] && arr[i] < copy[i + 1]) {
10+
++arr[i];
11+
hasChange = true;
12+
} else if (arr[i] > copy[i - 1] && arr[i] > copy[i + 1]) {
13+
--arr[i];
14+
hasChange = true;
15+
}
16+
}
17+
System.arraycopy(arr, 0, copy, 0, n);
18+
}
19+
List<Integer> res = new ArrayList<>();
20+
for (int e : arr) {
21+
res.add(e);
22+
}
23+
return res;
24+
}
25+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def transformArray(self, arr: List[int]) -> List[int]:
3+
copy = [e for e in arr]
4+
has_change, n = True, len(arr)
5+
while has_change:
6+
has_change = False
7+
for i in range(1, n - 1):
8+
if arr[i] < copy[i - 1] and arr[i] < copy[i + 1]:
9+
arr[i] += 1
10+
has_change = True
11+
elif arr[i] > copy[i - 1] and arr[i] > copy[i + 1]:
12+
arr[i] -= 1
13+
has_change = True
14+
copy = [e for e in arr]
15+
return arr

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /