large number addition
large number multiply
{1,2,3,4,5},1 / \ 2 3 / \ 4 5return the root of the binary tree
[4,5,2,#,#,3,1]. 4
/ \
5 2
/ \
3 1
----------------------------------------------------------------/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode *helper(TreeNode *root, TreeNode *left, TreeNode *right) {
if (root == NULL) return right;
TreeNode *tempLeft = root->left;
TreeNode *tempRight = root->right;
root->right = right;
root->left = left;
return helper(tempLeft,tempRight,root);
}
TreeNode *upsideDownBinaryTree(TreeNode *root) {
return helper(root,NULL,NULL);
}
};
Solution #2, iterative, with a stack containing all left childrenint read4(char *buf) reads 4 characters at a time from a file.read4 API, implement the function int read(char *buf, int n) that reads n characters from the file.read function will only be called once for each test case.// Forward declaration of the read4 API.
int read4(char *buf);
class Solution {
public:
/**
* @param buf Destination buffer
* @param n Maximum number of characters to read
* @return The number of characters read
*/
int read(char *buf, int n) {
int count = 0;
while (count < n) {
int curRead = read4(buf);
if (curRead < 4) {
return min(count + curRead,n);
}
count += curRead;
buf += 4;
}
return n;
}
};
/* The read4 API is defined in the parent class Reader4.
int read4(char[] buf); */
public class Solution extends Reader4 {
/**
* @param buf Destination buffer
* @param n Maximum number of characters to read
* @return The number of characters read
*/
public int read(char[] buf, int n) {
int count = 0;
while (count < n) {
char[] temp = new char[4];
int curRead = read4(temp);
for (int i = 0; i < curRead && count < n; i++) {
buf[count++] = temp[i];
}
if (curRead < 4) break;
}
return count;
}
}
int read4(char *buf) reads 4 characters at a time from a file.read4 API, implement the function int read(char *buf, int n) that reads n characters from the file.read function may be called multiple times.// Forward declaration of the read4 API.
int read4(char *buf);
class Solution {
public:
Solution() {
bufferSize = 0;
offset = 0;
}
/**
* @param buf Destination buffer
* @param n Maximum number of characters to read
* @return The number of characters read
*/
int read(char *buf, int n) {
int readChar = 0;
bool eof = false;
while (!eof && readChar < n) {
if (bufferSize == 0) {
bufferSize = read4(buffer);
if (bufferSize < 4) {
eof = true;
}
}
int len = min(n - readChar,bufferSize);
memcpy(buf,buffer + offset,len);
bufferSize -= len;
offset = (offset + len) % 4;
readChar += len;
buf += len;
}
return readChar;
}
private:
char buffer[4];
int offset;
int bufferSize;
};
/* The read4 API is defined in the parent class Reader4.
int read4(char[] buf); */
public class Solution extends Reader4 {
private char[] local = new char[4];
private int index = 0;
private int size = 0;
/**
* @param buf Destination buffer
* @param n Maximum number of characters to read
* @return The number of characters read
*/
public int read(char[] buf, int n) {
int count = 0;
boolean eof = false;
while (count < n && !eof) {
if (size == 0) {
size = read4(local);
index = 0;
if (size < 4) eof = true;
}
while (size > 0 && count < n) {
buf[count] = local[index];
count++;
index++;
size--;
}
}
return count;
}
}
class MinStack {
public:
MinStack() {
minElem = INT_MAX;
}
void push(int x) {
st.push(x);
if (x <= minElem) {
minSt.push(x);
minElem = x;
}
}
void pop() {
if (st.empty()) return;
if (st.top() == minElem) {
minSt.pop();
if (!minSt.empty()) {
minElem = minSt.top();
}else {
minElem = INT_MAX;
}
}
st.pop();
}
int top() {
if (st.empty()) return -1;
return st.top();
}
int getMin() {
return (int)minElem;
}
private:
stack<int> st;
stack<int> minSt;
long minElem;
};
with one stack
class MinStack {
public:
MinStack() {
minElem = INT_MAX;
}
void push(int x) {
if (x <= minElem) {
st.push(minElem);
minElem = x;
}
st.push(x);
}
void pop() {
if (st.empty()) return;
if (st.top() == minElem) {
st.pop();
if (!st.empty()) {
minElem = st.top();
st.pop();
}else {
minElem = INT_MAX;
}
}else
st.pop();
}
int top() {
if (st.empty()) return -1;
return st.top();
}
int getMin() {
return (int)minElem;
}
private:
stack<int> st;
long minElem;
};
0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).class Solution {
public:
int findMin(vector<int> &num) {
if (num.size() == 1) return num[0];
if (num[0] < num.back()) return num[0];
int left = 0;
int right = num.size() - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (num[mid] < num[mid - 1]) {
return num[mid];
}
if (num[mid] > num[right]) {
left = mid + 1;
}else {
right = mid - 1;
}
}
return -1;
}
};
Follow up for "Find Minimum in Rotated Sorted Array":Suppose a sorted array is rotated at some pivot unknown to you beforehand.
What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).class Solution {
public:
int findMin(vector<int> &num) {
if (num.size() == 1) return num[0];
if (num[0] < num.back()) return num[0];
int left = 0;
int right = num.size() - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (num[mid] < num[mid - 1]) {
return num[mid];
}
if (num[mid] == num[right]) {
right--;
}else if (num[mid] > num[right]) {
left = mid + 1;
}else {
right = mid - 1;
}
}
return num[0];
}
};
the sky is blue",blue is sky the".
class Solution {
public:
void reverseWords(string &s) {
string rt = "";
for (int i = 0; i < s.length(); i++) {
string word = "";
while (i < s.length() && !isspace(s[i])) {
word += s[i];
i++;
}
if (word == "") continue;
if (rt == "") {
rt = word;
}else {
rt = word + " " + rt;
}
word = "";
i--;
}
s = rt;
}
};
[2,3,-2,4],[2,3] has the largest product = 6.
class Solution {
public:
int maxProduct(int A[], int n) {
vector<int> maxDp(n);
vector<int> minDp(n);
maxDp[0] = A[0];
minDp[0] = A[0];
int maxP = maxDp[0];
int i = 1;
while (i < n) {
if (A[i] < 0) {
minDp[i] = min(A[i],A[i] * maxDp[i - 1]);
maxDp[i] = max(A[i],A[i] * minDp[i - 1]);
}else {
minDp[i] = min(A[i],A[i] * minDp[i - 1]);
maxDp[i] = max(A[i],A[i] * maxDp[i - 1]);
}
maxP = max(maxP,maxDp[i]);
i++;
}
return maxP;
}
};
class Solution {
public:
int maxProduct(vector<int>& nums) {
int high = 1, low = 1;
int maxSub = INT_MIN;
for (int i = 0; i < nums.size(); i++) {
if (nums[i] > 0) {
high = max(nums[i],nums[i] * high);
low = min(nums[i],nums[i] * low);
}else {
int th = high;
high = max(nums[i],nums[i] * low);
low = min(nums[i],nums[i] * th);
}
maxSub = max(maxSub,high);
}
return maxSub;
}
};
get and set.
get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.set(key, value) - Set or insert the value if the key is not
already present. When the cache reached its capacity, it should
invalidate the least recently used item before inserting a new item.
struct LinkedNode {
int key;
int val;
LinkedNode *pre;
LinkedNode *next;
LinkedNode(int key,int val) {
this->key = key;
this->val = val;
pre = NULL;
next = NULL;
}
};
class LRUCache{
public:
LRUCache(int capacity) {
this->capacity = capacity;
size = 0;
head = new LinkedNode(-1,-1);
tail = new LinkedNode(-1,-1);
head->next = tail;
tail->pre = head;
}
int get(int key) {
if (dic.find(key) == dic.end()) {
return -1;
}
LinkedNode *node = removeNode(dic[key]);
insertToFront(node);
return node->val;
}
LinkedNode* removeNode(LinkedNode *node) {
LinkedNode *preNode = node->pre;
LinkedNode *nextNode = node->next;
preNode->next = nextNode;
nextNode->pre = preNode;
return node;
}
void insertToFront(LinkedNode *node) {
LinkedNode *temp = head->next;
head->next = node;
temp->pre = node;
node->next = temp;
node->pre = head;
}
void set(int key, int value) {
if (dic.find(key) == dic.end()) {
LinkedNode *temp = head->next;
LinkedNode *add = new LinkedNode(key,value);
head->next = add;
add->pre = head;
add->next = temp;
temp->pre = add;
dic[key] = add;
size++;
}else {
LinkedNode *catched = removeNode(dic[key]);
insertToFront(dic[key]);
dic[key]->val = value;
}
if (size > capacity) {
LinkedNode *catched = removeNode(tail->pre);
dic.erase(catched->key);
delete catched;
size--;
}
}
private:
int capacity;
int size;
LinkedNode *head;
LinkedNode *tail;
unordered_map<int,LinkedNode*> dic;
};
class LRUCache {
private int capacity;
private DoublyLinkedNode head;
private DoublyLinkedNode tail;
private Map<Integer, DoublyLinkedNode> map;
public LRUCache(int capacity) {
this.capacity = capacity;
head = new DoublyLinkedNode(-1, -1);
tail = new DoublyLinkedNode(-1, -1);
head.next = tail;
tail.pre = head;
map = new HashMap<>();
}
public int get(int key) {
if (map.containsKey(key)) {
DoublyLinkedNode node = pickupAndReturn(key);
insertFront(node);
return node.val;
}
return -1;
}
public void put(int key, int value) {
DoublyLinkedNode node = null;
if (!map.containsKey(key)) {
node = new DoublyLinkedNode(key, value);
map.put(key, node);
}else {
node = pickupAndReturn(key);
node.val = value;
}
insertFront(node);
if (map.size() > capacity) {
removeLast();
}
}
private void removeLast() {
int key = tail.pre.key;
tail.pre.pre.next = tail;
tail.pre = tail.pre.pre;
map.remove(key);
}
private void insertFront(DoublyLinkedNode node) {
head.next.pre = node;
node.next = head.next;
head.next = node;
node.pre = head;
}
private DoublyLinkedNode pickupAndReturn(int key) {
DoublyLinkedNode node = map.get(key);
node.pre.next = node.next;
node.next.pre = node.pre;
return node;
}
}
class DoublyLinkedNode{
public DoublyLinkedNode pre;
public DoublyLinkedNode next;
public int key;
public int val;
public DoublyLinkedNode(int key, int val) {
this.key = key;
this.val = val;
}
}
/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache obj = new LRUCache(capacity);
* int param_1 = obj.get(key);
* obj.put(key,value);
*/