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
/* | |
Example of how to write binary stuff in C++ | |
Illustrates some of the various quirks/annoyances | |
*/ | |
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include <vector> |
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 search(vector<int>& nums, int target) { | |
if (nums[0] > target || nums.back() < target) | |
return -1; | |
int left = 0, right = nums.size() - 1; | |
// [left, right] | |
while (left <= right) { | |
int mid = left + (right - left) / 2; |
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 removeElement(vector<int>& nums, int val) { | |
auto iter = nums.begin(); | |
for (;iter != nums.end();) { | |
if (*iter == val) { | |
iter = nums.erase(iter); | |
} else { | |
iter++; | |
} |
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> sortedSquares(vector<int>& nums) { | |
int i = 0, j = nums.size() - 1; | |
int k = 0; | |
vector<int> rets(nums.size(), 0); | |
for (int k = nums.size() - 1; k >=0; k--) { | |
int ii = nums[i] * nums[i]; | |
int jj = nums[j] * nums[j]; |
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 minSubArrayLen(int target, vector<int>& nums) { | |
int sub_len = nums.size() + 1; | |
int i = 0; | |
int sum = 0; | |
for (int j = 0; j < nums.size(); ++j) { | |
sum += nums[j]; | |
while (sum >= target){ | |
int len = j - i + 1; |
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<vector<int>> generateMatrix(int n) { | |
// 行 列 | |
vector<vector<int>> mat; | |
for (int i = 0; i < n; ++i) { | |
vector<int> tmp; | |
for (int j = 0; j < n; ++j) | |
tmp.push_back(0); | |
mat.push_back(tmp); |
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 singly-linked list. | |
* struct ListNode { | |
* int val; | |
* ListNode *next; | |
* ListNode() : val(0), next(nullptr) {} | |
* ListNode(int x) : val(x), next(nullptr) {} | |
* ListNode(int x, ListNode *next) : val(x), next(next) {} | |
* }; | |
*/ |
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 MyLinkedList { | |
struct Node { | |
int val; | |
struct Node* next; | |
Node(): val(0), next(NULL) {} | |
Node(int v): val(v), next(NULL) {} | |
}; | |
Node* head_; | |
int size_; |
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 singly-linked list. | |
* struct ListNode { | |
* int val; | |
* ListNode *next; | |
* ListNode() : val(0), next(nullptr) {} | |
* ListNode(int x) : val(x), next(nullptr) {} | |
* ListNode(int x, ListNode *next) : val(x), next(next) {} | |
* }; | |
*/ |
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 singly-linked list. | |
* struct ListNode { | |
* int val; | |
* ListNode *next; | |
* ListNode() : val(0), next(nullptr) {} | |
* ListNode(int x) : val(x), next(nullptr) {} | |
* ListNode(int x, ListNode *next) : val(x), next(next) {} | |
* }; | |
*/ |
OlderNewer