Skip to content

[pull] main from doocs:main #160

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

Merged
merged 3 commits into from
Jul 14, 2024
Merged
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
44 changes: 43 additions & 1 deletion solution/0700-0799/0726.Number of Atoms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,49 @@ tags:
#### Java

```java

class Solution {
public String countOfAtoms(String formula) {
Map<String, Integer> map = new HashMap<>();
int[] stack = new int[1000];
int top = 0, multiplier = 1, freq = 0;
char[] c = formula.toCharArray();
for (int i = c.length - 1; i >= 0; i--) {
if (c[i] >= 'a' && c[i] <= 'z') {
int end = i--;
while (i >= 0 && c[i] >= 'a' && c[i] <= 'z') i--;
String key = new String(c, i, end - i + 1);
map.put(key, map.getOrDefault(key, 0) + Math.max(freq, 1) * multiplier);
freq = 0;
} else if (c[i] >= 'A' && c[i] <= 'Z') {
String key = new String(c, i, 1);
map.put(key, map.getOrDefault(key, 0) + Math.max(freq, 1) * multiplier);
freq = 0;
} else if (c[i] >= '0' && c[i] <= '9') {
freq = c[i] - '0';
int p = 10;
while (i - 1 >= 0 && c[i - 1] >= '0' && c[i - 1] <= '9') {
freq += p * (c[--i] - '0');
p *= 10;
}
} else if (c[i] == ')') {
stack[top++] = multiplier;
multiplier *= Math.max(freq, 1);
freq = 0;
} else {
multiplier = stack[--top];
}
}
List<String> keys = new ArrayList<>(map.keySet());
Collections.sort(keys);
StringBuilder sb = new StringBuilder();
for (String key : keys) {
sb.append(key);
int f = map.get(key);
if (f > 1) sb.append(f);
}
return sb.toString();
}
}
```

#### C++
Expand Down
44 changes: 43 additions & 1 deletion solution/0700-0799/0726.Number of Atoms/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,49 @@ tags:
#### Java

```java

class Solution {
public String countOfAtoms(String formula) {
Map<String, Integer> map = new HashMap<>();
int[] stack = new int[1000];
int top = 0, multiplier = 1, freq = 0;
char[] c = formula.toCharArray();
for (int i = c.length - 1; i >= 0; i--) {
if (c[i] >= 'a' && c[i] <= 'z') {
int end = i--;
while (i >= 0 && c[i] >= 'a' && c[i] <= 'z') i--;
String key = new String(c, i, end - i + 1);
map.put(key, map.getOrDefault(key, 0) + Math.max(freq, 1) * multiplier);
freq = 0;
} else if (c[i] >= 'A' && c[i] <= 'Z') {
String key = new String(c, i, 1);
map.put(key, map.getOrDefault(key, 0) + Math.max(freq, 1) * multiplier);
freq = 0;
} else if (c[i] >= '0' && c[i] <= '9') {
freq = c[i] - '0';
int p = 10;
while (i - 1 >= 0 && c[i - 1] >= '0' && c[i - 1] <= '9') {
freq += p * (c[--i] - '0');
p *= 10;
}
} else if (c[i] == ')') {
stack[top++] = multiplier;
multiplier *= Math.max(freq, 1);
freq = 0;
} else {
multiplier = stack[--top];
}
}
List<String> keys = new ArrayList<>(map.keySet());
Collections.sort(keys);
StringBuilder sb = new StringBuilder();
for (String key : keys) {
sb.append(key);
int f = map.get(key);
if (f > 1) sb.append(f);
}
return sb.toString();
}
}
```

#### C++
Expand Down
43 changes: 43 additions & 0 deletions solution/0700-0799/0726.Number of Atoms/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Solution {
public String countOfAtoms(String formula) {
Map<String, Integer> map = new HashMap<>();
int[] stack = new int[1000];
int top = 0, multiplier = 1, freq = 0;
char[] c = formula.toCharArray();
for (int i = c.length - 1; i >= 0; i--) {
if (c[i] >= 'a' && c[i] <= 'z') {
int end = i--;
while (i >= 0 && c[i] >= 'a' && c[i] <= 'z') i--;
String key = new String(c, i, end - i + 1);
map.put(key, map.getOrDefault(key, 0) + Math.max(freq, 1) * multiplier);
freq = 0;
} else if (c[i] >= 'A' && c[i] <= 'Z') {
String key = new String(c, i, 1);
map.put(key, map.getOrDefault(key, 0) + Math.max(freq, 1) * multiplier);
freq = 0;
} else if (c[i] >= '0' && c[i] <= '9') {
freq = c[i] - '0';
int p = 10;
while (i - 1 >= 0 && c[i - 1] >= '0' && c[i - 1] <= '9') {
freq += p * (c[--i] - '0');
p *= 10;
}
} else if (c[i] == ')') {
stack[top++] = multiplier;
multiplier *= Math.max(freq, 1);
freq = 0;
} else {
multiplier = stack[--top];
}
}
List<String> keys = new ArrayList<>(map.keySet());
Collections.sort(keys);
StringBuilder sb = new StringBuilder();
for (String key : keys) {
sb.append(key);
int f = map.get(key);
if (f > 1) sb.append(f);
}
return sb.toString();
}
}
167 changes: 164 additions & 3 deletions solution/2700-2799/2751.Robot Collisions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,25 +90,186 @@ tags:
#### Python3

```python
class Solution:
def survivedRobotsHealths(
self, positions: List[int], healths: List[int], directions: str
) -> List[int]:
n = len(positions)
indices = list(range(n))
stack = []

indices.sort(key=lambda i: positions[i])

for currentIndex in indices:
if directions[currentIndex] == "R":
stack.append(currentIndex)
else:
while stack and healths[currentIndex] > 0:
topIndex = stack.pop()

if healths[topIndex] > healths[currentIndex]:
healths[topIndex] -= 1
healths[currentIndex] = 0
stack.append(topIndex)
elif healths[topIndex] < healths[currentIndex]:
healths[currentIndex] -= 1
healths[topIndex] = 0
else:
healths[currentIndex] = 0
healths[topIndex] = 0

result = [health for health in healths if health > 0]
return result

```

#### Java

```java

class Solution {
public List<Integer> survivedRobotsHealths(int[] positions, int[] healths, String directions) {
int n = positions.length;
Integer[] indices = new Integer[n];
for (int i = 0; i < n; i++) {
indices[i] = i;
}

Arrays.sort(indices, (i, j) -> Integer.compare(positions[i], positions[j]));

Stack<Integer> stack = new Stack<>();

for (int currentIndex : indices) {
if (directions.charAt(currentIndex) == 'R') {
stack.push(currentIndex);
} else {
while (!stack.isEmpty() && healths[currentIndex] > 0) {
int topIndex = stack.pop();

if (healths[topIndex] > healths[currentIndex]) {
healths[topIndex] -= 1;
healths[currentIndex] = 0;
stack.push(topIndex);
} else if (healths[topIndex] < healths[currentIndex]) {
healths[currentIndex] -= 1;
healths[topIndex] = 0;
} else {
healths[currentIndex] = 0;
healths[topIndex] = 0;
}
}
}
}

List<Integer> result = new ArrayList<>();
for (int health : healths) {
if (health > 0) {
result.add(health);
}
}

return result;
}
}
```

#### C++

```cpp

class Solution {
public:
vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {
int n = positions.size();
vector<int> indices(n);

iota(indices.begin(), indices.end(), 0);
stack<int> st;

auto lambda = [&](int i, int j) { return positions[i] < positions[j]; };

sort(begin(indices), end(indices), lambda);

vector<int> result;
for (int currentIndex : indices) {
if (directions[currentIndex] == 'R') {
st.push(currentIndex);
} else {
while (!st.empty() && healths[currentIndex] > 0) {
int topIndex = st.top();
st.pop();

if (healths[topIndex] > healths[currentIndex]) {
healths[topIndex] -= 1;
healths[currentIndex] = 0;
st.push(topIndex);
} else if (healths[topIndex] < healths[currentIndex]) {
healths[currentIndex] -= 1;
healths[topIndex] = 0;
} else {
healths[currentIndex] = 0;
healths[topIndex] = 0;
}
}
}
}

for (int i = 0; i < n; ++i) {
if (healths[i] > 0) {
result.push_back(healths[i]);
}
}
return result;
}
};
```

#### Go

```go

func survivedRobotsHealths(positions []int, healths []int, directions string) []int {
n := len(positions)
indices := make([]int, n)
for i := range indices {
indices[i] = i
}

sort.Slice(indices, func(i, j int) bool {
return positions[indices[i]] < positions[indices[j]]
})

stack := []int{}

for _, currentIndex := range indices {
if directions[currentIndex] == 'R' {
stack = append(stack, currentIndex)
} else {
for len(stack) > 0 && healths[currentIndex] > 0 {
topIndex := stack[len(stack)-1]
stack = stack[:len(stack)-1]

if healths[topIndex] > healths[currentIndex] {
healths[topIndex] -= 1
healths[currentIndex] = 0
stack = append(stack, topIndex)
} else if healths[topIndex] < healths[currentIndex] {
healths[currentIndex] -= 1
healths[topIndex] = 0
} else {
healths[currentIndex] = 0
healths[topIndex] = 0
}
}
}
}

result := []int{}
for _, health := range healths {
if health > 0 {
result = append(result, health)
}
}

return result
}
```

<!-- tabs:end -->
Expand Down
Loading