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
class MyStack { | |
public: | |
queue<int> q; | |
/** Initialize your data structure here. */ | |
MyStack() { | |
} | |
/** Push element x onto stack. */ | |
void push(int x) { |
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
class MyQueue { | |
public: | |
/** Initialize your data structure here. */ | |
MyQueue() { | |
} | |
/** Push element x to the back of queue. */ | |
void push(int x) { | |
while(!s2.empty()) { |
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
class Solution { | |
public: | |
double largestSumOfAverages(vector<int>& A, int K) { | |
// const int n = A.size(); | |
// vector<vector<double>> dp(K+1, vector<double>(n+1, 0.0)); | |
// vector<double> sum(n+1, 0.0); | |
// for(int i = 1; i <= n; i++) { | |
// sum[i] = sum[i-1] + A[i-1]; | |
// dp[1][i] = static_cast<double>(sum[i]) / i; | |
// } |
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
class Solution { | |
public: | |
bool isIsomorphic(string s, string t) { | |
map<char, char> m1; | |
map<char, char> m2; | |
bool flag = true; | |
for(int i = 0; i < s.size(); i++) { | |
if (m1.find(s[i]) == m1.end() && m2.find(t[i]) == m2.end()) { | |
m1[s[i]] = t[i]; | |
m2[t[i]] = s[i]; |
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
import sys | |
import time | |
def spinning_cursor(): | |
while True: | |
for cursor in '|/-\\': | |
yield cursor | |
spinner = spinning_cursor() | |
for _ in range(50): |
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
class Solution { | |
public: | |
vector<int> partitionLabels(string S) { | |
vector<int> last_indx(128, 0); | |
for(int i = 0; i < S.size(); i++) | |
last_indx[S[i]] = i; | |
vector<int> ans; | |
int start = 0; | |
int end = 0; | |
for(int i = 0; i < S.size(); i++) { |
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
/** | |
* Definition for a binary tree node. | |
* struct TreeNode { | |
* int val; | |
* TreeNode *left; | |
* TreeNode *right; | |
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | |
* }; | |
*/ | |
class Solution { |
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
class Solution { | |
public: | |
string convertToTitle(int n) { | |
string result; | |
int remain = 0; | |
while(n > 0) { | |
remain = (--n) % 26; | |
result.push_back(remain + 65); | |
n /= 26; | |
} |
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
class Solution { | |
public: | |
Solution() { | |
for(string::size_type i = 0; i < chars.size(); i++) { | |
charToValueMap[chars[i]] = i; | |
} | |
} | |
// Encodes a URL to a shortened URL. | |
string encode(string longUrl) { |
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
class Solution { | |
public: | |
int countPrimes(int n) { | |
if (n == 1 || n == 0) return 0; | |
vector<bool> passed(n, true); | |
passed[0] = false, passed[1] = false; | |
for(int i = 0; i < sqrt(n); i++) { | |
if (passed[i]) { | |
for(int j = i * i; j < n; j += i) { | |
passed[j] = false; |