This file contains hidden or 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: | |
| void setZeroes(vector<vector<int> > &matrix) { | |
| int n = matrix.size(); | |
| int m = matrix[0].size(); | |
| int firstrow = 0; | |
| int firstcol = 0; | |
| for (int i = 0; i < n; ++i) | |
| for (int j = 0; j < m; ++j) | |
| if (matrix[i][j] == 0) { |
This file contains hidden or 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
| #define MAXN 500 | |
| int f[MAXN][MAXN]; | |
| int dp(int f[MAXN][MAXN], string& word1, string& word2, int n, int m, int k, int t) { | |
| if (k < 0) | |
| return t + 1; | |
| if (t < 0) | |
| return k + 1; | |
| if (f[k][t] != -1) |
This file contains hidden or 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
| void update(vector<string>& s, const string& t) { | |
| if (t == ".") | |
| ; | |
| else if (t == "..") { | |
| if (!s.empty()) | |
| s.pop_back(); | |
| } | |
| else | |
| s.push_back(t); | |
| } |
This file contains hidden or 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 climbStairs(int n) { | |
| int f[3]; | |
| memset(f, 0, sizeof(f)); | |
| f[0] = 1; | |
| for (int i = 1; i <= n; ++i) { | |
| f[i % 3] = f[(i + 1) % 3] + f[(i + 2) % 3]; | |
| } | |
| return f[n % 3]; |
This file contains hidden or 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 sqrt(int x) { | |
| if (x <= 0) | |
| return 0; | |
| int ret = 1; | |
| int left = 1, right = x; | |
| while (left <= right) { | |
| int mid = (left + right) >> 1; | |
| if (mid <= x / mid) { |
This file contains hidden or 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<string> fullJustify(vector<string> &words, int L) { | |
| vector<string> ret; | |
| int n = words.size(); | |
| int pos = 0; | |
| while (pos < n) { | |
| int len = words[pos].length(); | |
| int next = -1; | |
| for (int i = pos + 1; i < n; ++i) { |
This file contains hidden or 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> plusOne(vector<int> &digits) { | |
| int all9 = 1; | |
| for (int i = 0; i < digits.size(); ++i) | |
| if (digits[i] != 9) { | |
| all9 = 0; | |
| break; | |
| } | |
| int len = digits.size() + all9; |
This file contains hidden or 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 isInt(const char* s, int len) { | |
| if (len == 0) | |
| return false; | |
| int st = 0; | |
| if (s[0] == '+' || s[0] == '-') | |
| st = 1; | |
| if (st == len) | |
| return false; |
This file contains hidden or 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 addBinary(string a, string b) { | |
| string ret; | |
| int lena = a.length(); | |
| int lenb = b.length(); | |
| int t = 0; | |
| int i = lena - 1; | |
| int j = lenb - 1; | |
| while (i >= 0 || j >= 0) { |
This file contains hidden or 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 singly-linked list. | |
| * struct ListNode { | |
| * int val; | |
| * ListNode *next; | |
| * ListNode(int x) : val(x), next(NULL) {} | |
| * }; | |
| */ | |
| class Solution { | |
| public: |