Skip to content
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
10 changes: 5 additions & 5 deletions images/contributors.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions solution/0000-0099/0012.Integer to Roman/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,26 @@ class Solution {
}
```

### **C++**

```cpp
class Solution {
public:
string intToRoman(int num) {
vector<int> nums{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
vector<string> romans{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
string ans;
for (int i = 0; i < nums.size(); ++i) {
while (num >= nums[i]) {
num -= nums[i];
ans.append(romans[i]);
}
}
return ans;
}
};
```

### **...**

```
Expand Down
20 changes: 20 additions & 0 deletions solution/0000-0099/0012.Integer to Roman/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,26 @@ class Solution {
}
```

### **C++**

```cpp
class Solution {
public:
string intToRoman(int num) {
vector<int> nums{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
vector<string> romans{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
string ans;
for (int i = 0; i < nums.size(); ++i) {
while (num >= nums[i]) {
num -= nums[i];
ans.append(romans[i]);
}
}
return ans;
}
};
```

### **...**

```
Expand Down
15 changes: 15 additions & 0 deletions solution/0000-0099/0012.Integer to Roman/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
string intToRoman(int num) {
vector<int> nums{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
vector<string> romans{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
string ans;
for (int i = 0; i < nums.size(); ++i) {
while (num >= nums[i]) {
num -= nums[i];
ans.append(romans[i]);
}
}
return ans;
}
};
27 changes: 27 additions & 0 deletions solution/0000-0099/0013.Roman to Integer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,33 @@ class Solution {
}
```

### **C++**

```cpp
class Solution {
public:
int romanToInt(string s) {
unordered_map<char, int> nums{
{'I', 1},
{'V', 5},
{'X', 10},
{'L', 50},
{'C', 100},
{'D', 500},
{'M', 1000},
};
int ans = 0;
for (int i = 0; i < s.size() - 1; ++i) {
if (nums[s[i]] < nums[s[i + 1]])
ans -= nums[s[i]];
else
ans += nums[s[i]];
}
return ans + nums[s.back()];
}
};
```

### **...**

```
Expand Down
27 changes: 27 additions & 0 deletions solution/0000-0099/0013.Roman to Integer/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,33 @@ class Solution {
}
```

### **C++**

```cpp
class Solution {
public:
int romanToInt(string s) {
unordered_map<char, int> nums{
{'I', 1},
{'V', 5},
{'X', 10},
{'L', 50},
{'C', 100},
{'D', 500},
{'M', 1000},
};
int ans = 0;
for (int i = 0; i < s.size() - 1; ++i) {
if (nums[s[i]] < nums[s[i + 1]])
ans -= nums[s[i]];
else
ans += nums[s[i]];
}
return ans + nums[s.back()];
}
};
```

### **...**

```
Expand Down
22 changes: 22 additions & 0 deletions solution/0000-0099/0013.Roman to Integer/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
int romanToInt(string s) {
unordered_map<char, int> nums{
{'I', 1},
{'V', 5},
{'X', 10},
{'L', 50},
{'C', 100},
{'D', 500},
{'M', 1000},
};
int ans = 0;
for (int i = 0; i < s.size() - 1; ++i) {
if (nums[s[i]] < nums[s[i + 1]])
ans -= nums[s[i]];
else
ans += nums[s[i]];
}
return ans + nums[s.back()];
}
};
133 changes: 113 additions & 20 deletions solution/0100-0199/0155.Min Stack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ minStack.getMin(); --&gt; 返回 -2.
<li><code>pop</code>、<code>top</code> 和 <code>getMin</code> 操作总是在 <strong>非空栈</strong> 上调用。</li>
</ul>


## 解法

“辅助栈”实现。

<!-- 这里可写通用的实现逻辑 -->

<!-- tabs:start -->
Expand All @@ -64,28 +65,26 @@ class MinStack:
initialize your data structure here.
"""
self.s = []
self.helper = []

self.mins = [float('inf')]

def push(self, x: int) -> None:
self.s.append(x)
element = x if not self.helper or x < self.helper[-1] else self.helper[-1]
self.helper.append(element)
def push(self, val: int) -> None:
self.s.append(val)
self.mins.append(min(self.mins[-1], val))

def pop(self) -> None:
self.s.pop()
self.helper.pop()
self.mins.pop()

def top(self) -> int:
return self.s[-1]

def getMin(self) -> int:
return self.helper[-1]
return self.mins[-1]


# Your MinStack object will be instantiated and called as such:
# obj = MinStack()
# obj.push(x)
# obj.push(val)
# obj.pop()
# param_3 = obj.top()
# param_4 = obj.getMin()
Expand All @@ -97,46 +96,140 @@ class MinStack:

```java
class MinStack {

private Deque<Integer> s;
private Deque<Integer> helper;
private Deque<Integer> mins;

/** initialize your data structure here. */
public MinStack() {
s = new ArrayDeque<>();
helper = new ArrayDeque<>();
mins = new ArrayDeque<>();
mins.push(Integer.MAX_VALUE);
}

public void push(int x) {
s.push(x);
int element = helper.isEmpty() || x < helper.peek() ? x : helper.peek();
helper.push(element);
public void push(int val) {
s.push(val);
mins.push(Math.min(mins.peek(), val));
}

public void pop() {
s.pop();
helper.pop();
mins.pop();
}

public int top() {
return s.peek();
}

public int getMin() {
return helper.peek();
return mins.peek();
}
}

/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.push(val);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
```

### **C++**

```cpp
class MinStack {
private:
stack<int> s;
stack<int> mins;

public:
/** initialize your data structure here. */
MinStack() {
mins.push(INT_MAX);
}

void push(int val) {
s.push(val);
mins.push(min(mins.top(), val));
}

void pop() {
s.pop();
mins.pop();
}

int top() {
return s.top();
}

int getMin() {
return mins.top();
}
};

/**
* Your MinStack object will be instantiated and called as such:
* MinStack* obj = new MinStack();
* obj->push(val);
* obj->pop();
* int param_3 = obj->top();
* int param_4 = obj->getMin();
*/
```

### **JavaScript**

```js
/**
* initialize your data structure here.
*/
var MinStack = function() {
this.s = [];
this.mins = [Infinity];
};

/**
* @param {number} val
* @return {void}
*/
MinStack.prototype.push = function(val) {
this.s.push(val);
this.mins.push(Math.min(this.mins[this.mins.length - 1], val));
};

/**
* @return {void}
*/
MinStack.prototype.pop = function() {
this.s.pop();
this.mins.pop();
};

/**
* @return {number}
*/
MinStack.prototype.top = function() {
return this.s[this.s.length - 1];
};

/**
* @return {number}
*/
MinStack.prototype.getMin = function() {
return this.mins[this.mins.length - 1];
};

/**
* Your MinStack object will be instantiated and called as such:
* var obj = new MinStack()
* obj.push(val)
* obj.pop()
* var param_3 = obj.top()
* var param_4 = obj.getMin()
*/
```

### **...**

```
Expand Down
Loading