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
public class Solution { | |
public int canCompleteCircuit(int[] gas, int[] cost) { | |
size = gas.length; | |
if (size == 0) return -1; | |
int src = 0, dest = 0, balance = 0; | |
while (true) { | |
if (balance + gas[dest] - cost[dest] >= 0) { | |
balance += gas[dest] - cost[dest]; | |
dest = next(dest); | |
if (dest == src) return src; |
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 with a random pointer. | |
* class RandomListNode { | |
* int label; | |
* RandomListNode next, random; | |
* RandomListNode(int x) { this.label = x; } | |
* }; | |
*/ | |
public class Solution { | |
public RandomListNode copyRandomList(RandomListNode head) { |
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: | |
// single number i | |
int singleNumber(int A[], int n) { | |
int result = 0; | |
for (int i; i<n ;++i) | |
result = (result ^ A[i]); | |
return result; | |
} |
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: | |
// word break i | |
bool wordBreak(string s, unordered_set<string> &dict) { | |
if (dict.empty()) return false; | |
int maxSize = max_element(dict.begin(), dict.end(), sizeComp)->size(); | |
int wordSize = s.size(); | |
bool *dp = new bool[wordSize+1]; | |
// init dp array | |
for (int i=1; i<=wordSize; ++i) dp[i] = false; |
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(int x) : val(x), next(NULL) {} | |
* }; | |
*/ | |
// GREAT algorithm! inspired by that discussion forum. |
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(int x) : val(x), next(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
/* Merge Sort for linked list is very effective */ | |
/** | |
* Definition for singly-linked list. | |
* struct ListNode { | |
* int val; | |
* ListNode *next; | |
* ListNode(int x) : val(x), next(NULL) {} | |
* }; | |
*/ |
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: | |
enum Direction {RIGHT, LEFT, DOWN, UP}; | |
vector<vector<int> > generateMatrix(int n) { | |
// init | |
i = 0; j = 0; matrix_size = n; | |
result.clear(); | |
vector<int> v; | |
Direction d = RIGHT; | |
for (int i=0; i<n; ++i) v.push_back(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
class Solution { | |
public: | |
int candy(vector<int> &ratings) { | |
int size = (int)ratings.size(); | |
if (size == 0 || size == 1) return size; | |
vector<int> indices, candies; | |
for (int i=0; i<size; ++i) { | |
indices.push_back(i); | |
candies.push_back(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
/* | |
reduced code from 120 lines to 40 lines: | |
no need to build classes like Line and Fraction! | |
it's enough to | |
1. represent lines using only slope since point i | |
is fixed | |
2. use string as the key of hashmap | |
*/ | |
public class Solution { |