-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
佐毅
committed
Feb 7, 2020
1 parent
090fee1
commit cb81b20
Showing
6 changed files
with
292 additions
and
1 deletion.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,67 @@ | ||
// | ||
// main.cpp | ||
// leetcode-155 最小栈 | ||
// | ||
// Created by 佐毅 on 2020/2/7. | ||
// Copyright © 2020 dfjr. All rights reserved. | ||
// | ||
|
||
/** | ||
最小栈 | ||
设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。 | ||
push(x) -- 将元素 x 推入栈中。 | ||
pop() -- 删除栈顶的元素。 | ||
top() -- 获取栈顶元素。 | ||
getMin() -- 检索栈中的最小元素。 | ||
*/ | ||
|
||
#include <iostream> | ||
#include <stack> | ||
|
||
using namespace std; | ||
class MinStack { | ||
public: | ||
stack<int> s;//数据栈 | ||
stack<int> min;//辅助栈 | ||
/** initialize your data structure here. */ | ||
MinStack() { | ||
|
||
} | ||
|
||
void push(int x) { | ||
s.push(x); | ||
if(min.empty()||x<=min.top()) | ||
{ | ||
min.push(x); | ||
} | ||
} | ||
|
||
void pop() { | ||
if(s.top()==min.top()) | ||
min.pop(); | ||
s.pop(); | ||
} | ||
|
||
int top() { | ||
return s.top(); | ||
} | ||
int getMin() { | ||
return min.top(); | ||
} | ||
}; | ||
|
||
|
||
int main(int argc, const char * argv[]) { | ||
|
||
MinStack minStack ; | ||
|
||
minStack.push(2); | ||
minStack.push(1); | ||
minStack.push(5); | ||
minStack.push(4); | ||
minStack.push(0); | ||
|
||
cout<< minStack.getMin() <<endl; | ||
return 0; | ||
} |
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,34 @@ | ||
// | ||
// main.cpp | ||
// leetcode-344 反转字符串 | ||
// | ||
// Created by 佐毅 on 2020/2/7. | ||
// Copyright © 2020 dfjr. All rights reserved. | ||
// | ||
|
||
#include <iostream> | ||
#include <string> | ||
|
||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
string reverseString(string s) { | ||
int length = (int)s.size(); | ||
for (int i = 0; i< length/2; i++) { | ||
char ch = s[i]; | ||
s[i] = s[length -i-1]; | ||
s[length -i-1] = ch; | ||
} | ||
return s; | ||
} | ||
}; | ||
|
||
int main(int argc, const char * argv[]) { | ||
|
||
Solution solu; | ||
string str("abcabcbb"); | ||
std::cout << solu.reverseString(str); | ||
cout << endl; | ||
return 0; | ||
} |
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
Binary file modified
BIN
+3.47 KB
(100%)
...xcodeproj/project.xcworkspace/xcuserdata/kevin.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
Oops, something went wrong.