Movatterモバイル変換


[0]ホーム

URL:


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

Commitad648c5

Browse files
add article (#5040)
* fix c# param type, maximum-distance-in-arrays.md* add add-bold-tag-in-string.md article
1 parent39e33ef commitad648c5

File tree

2 files changed

+168
-5
lines changed

2 files changed

+168
-5
lines changed

‎articles/add-bold-tag-in-string.md‎

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
##1. Mark Bold Characters
2+
3+
::tabs-start
4+
5+
```python
6+
classSolution:
7+
defaddBoldTag(self,s:str,words: List[str]) ->str:
8+
n=len(s)
9+
bold= [False]* n
10+
11+
for wordin words:
12+
start= s.find(word)
13+
while start!=-1:
14+
for iinrange(start, start+len(word)):
15+
bold[i]=True
16+
17+
start= s.find(word, start+1)
18+
19+
open_tag="<b>"
20+
close_tag="</b>"
21+
ans= []
22+
23+
for iinrange(n):
24+
if bold[i]and (i==0ornot bold[i-1]):
25+
ans.append(open_tag)
26+
27+
ans.append(s[i])
28+
29+
if bold[i]and (i== n-1ornot bold[i+1]):
30+
ans.append(close_tag)
31+
32+
return"".join(ans)
33+
```
34+
35+
```java
36+
classSolution {
37+
publicStringaddBoldTag(Strings,String[]words) {
38+
int n= s.length();
39+
boolean[] bold=newboolean[n];
40+
41+
for (String word: words) {
42+
int start= s.indexOf(word);
43+
while (start!=-1) {
44+
for (int i= start; i< start+ word.length(); i++) {
45+
bold[i]=true;
46+
}
47+
48+
start= s.indexOf(word, start+1);
49+
}
50+
}
51+
52+
String openTag="<b>";
53+
String closeTag="</b>";
54+
StringBuilder ans=newStringBuilder();
55+
56+
for (int i=0; i< n; i++) {
57+
if (bold[i]&& (i==0||!bold[i-1])) {
58+
ans.append(openTag);
59+
}
60+
61+
ans.append(s.charAt(i));
62+
63+
if (bold[i]&& (i== n-1||!bold[i+1])) {
64+
ans.append(closeTag);
65+
}
66+
}
67+
68+
return ans.toString();
69+
}
70+
}
71+
```
72+
73+
```cpp
74+
classSolution {
75+
public:
76+
string addBoldTag(string s, vector<string>& words) {
77+
int n = s.size();
78+
vector<bool> bold(n);
79+
80+
for (string word: words) {
81+
int start = s.find(word);
82+
while (start != -1) {
83+
for (int i = start; i < start + word.size(); i++) {
84+
bold[i] = true;
85+
}
86+
87+
start = s.find(word, start + 1);
88+
}
89+
}
90+
91+
string openTag = "<b>";
92+
string closeTag = "</b>";
93+
string ans = "";
94+
95+
for (int i = 0; i < n; i++) {
96+
if (bold[i] && (i == 0 || !bold[i - 1])) {
97+
ans += openTag;
98+
}
99+
100+
ans += s[i];
101+
102+
if (bold[i] && (i == n - 1 || !bold[i + 1])) {
103+
ans += closeTag;
104+
}
105+
}
106+
107+
return ans;
108+
}
109+
};
110+
```
111+
112+
```javascript
113+
class Solution {
114+
/**
115+
* @param {string} s
116+
* @param {string[]} words
117+
* @return {string}
118+
*/
119+
addBoldTag(s, words) {
120+
const n = s.length;
121+
const bold = new Array(n).fill(false);
122+
123+
for (const word of words) {
124+
let start = s.indexOf(word);
125+
while (start !== -1) {
126+
for (let i = start; i < start + word.length; i++) {
127+
bold[i] = true;
128+
}
129+
start = s.indexOf(word, start + 1);
130+
}
131+
}
132+
133+
const openTag = "<b>";
134+
const closeTag = "</b>";
135+
const ans = [];
136+
137+
for (let i = 0; i < n; i++) {
138+
if (bold[i] && (i === 0 || !bold[i - 1])) {
139+
ans.push(openTag);
140+
}
141+
ans.push(s[i]);
142+
if (bold[i] && (i === n - 1 || !bold[i + 1])) {
143+
ans.push(closeTag);
144+
}
145+
}
146+
147+
return ans.join("");
148+
}
149+
}
150+
```
151+
152+
::tabs-end
153+
154+
###Time & Space Complexity
155+
The time complexity may differ between languages. It is dependent on how the built-in method is implemented.
156+
For this analysis, we will assume that we are using Java.
157+
158+
- Time complexity: $O(m \cdot (n^2 \cdot k - n \cdot k^2))$
159+
- Space complexity: $O(n)$
160+
161+
> Where $n$ is `s.length`, $m$ is `words.length`,and $k$ is the average length of the words.

‎articles/maximum-distance-in-arrays.md‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class Solution {
7979

8080
```csharp
8181
publicclassSolution {
82-
publicintMaxDistance(List<List<int>>arrays) {
82+
publicintMaxDistance(IList<IList<int>>arrays) {
8383
intres=0;
8484
intn=arrays.Count;
8585
for (inti=0;i<n-1;i++) {
@@ -212,8 +212,8 @@ class Solution {
212212

213213
```csharp
214214
publicclassSolution {
215-
publicintMaxDistance(List<List<int>>arrays) {
216-
List<int>array1,array2;
215+
publicintMaxDistance(IList<IList<int>>arrays) {
216+
IList<int>array1,array2;
217217
intres=0;
218218
intn=arrays.Count;
219219
for (inti=0;i<n-1;i++) {
@@ -355,18 +355,20 @@ class Solution {
355355

356356
```csharp
357357
publicclassSolution {
358-
publicintMaxDistance(List<List<int>>arrays) {
358+
publicintMaxDistance(IList<IList<int>>arrays) {
359359
intres=0;
360360
intn=arrays[0].Count;
361361
intmin_val=arrays[0][0];
362362
intmax_val=arrays[0][arrays[0].Count-1];
363+
363364
for (inti=1;i<arrays.Count;i++) {
364365
n=arrays[i].Count;
365366
res=Math.Max(res,Math.Max(Math.Abs(arrays[i][n-1]-min_val),
366-
Math.Abs(max_val-arrays[i][0])));
367+
Math.Abs(max_val-arrays[i][0])));
367368
min_val=Math.Min(min_val,arrays[i][0]);
368369
max_val=Math.Max(max_val,arrays[i][n-1]);
369370
}
371+
370372
returnres;
371373
}
372374
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp