Commit c10737d8 authored by yaozc's avatar yaozc
Browse files

keep going

parent 92e9d7ee
......@@ -90,14 +90,14 @@ public:
int sum = nums[outer] + nums[left] + nums[right];
int diff = target - sum;
if (abs(diff) < closest) {
closest = abs(diff);
result = sum;
closest = abs(diff);
result = sum;
}
if (diff < 0) {
right--;
right--;
}
else if (diff > 0) {
left++;
left++;
}
else{
return result;
......@@ -116,7 +116,7 @@ public:
// step 2.若遇到不符,skip多少个由查ips表所知
/*
lps[i] = the longest proper prefix of pat[0..i]
which is also a suffix of pat[0..i].
which is also a suffix of pat[0..i].
For the pattern “ABCDE”,
pat = "AAAA"
......@@ -136,25 +136,25 @@ lps[] is [0, 1, 0, 1, 2, 0, 1, 2, 3, 4, 5]
// 然后对称线swap
// temp作为对角线终点变量(每一行到哪里停)
/*
* clockwise rotate
* first reverse up to down, then swap the symmetry
* 1 2 3 7 8 9 7 4 1
* 4 5 6 => 4 5 6 => 8 5 2
* 7 8 9 1 2 3 9 6 3
* clockwise rotate
* first reverse up to down, then swap the symmetry
* 1 2 3 7 8 9 7 4 1
* 4 5 6 => 4 5 6 => 8 5 2
* 7 8 9 1 2 3 9 6 3
*/
class Solution {
public:
void rotate(vector<vector<int>>& matrix) {
swap(matrix.begin(), matrix.end());
int temp = 1;
for (int outer = 0; outer < matrix.size(); outer++) {
for (int inner = 0; inner < temp; inner++) {
swap(matrix[outer][inner], matrix[inner][outer]);
}
temp++;
}
void rotate(vector<vector<int>>& matrix) {
swap(matrix.begin(), matrix.end());
int temp = 1;
for (int outer = 0; outer < matrix.size(); outer++) {
for (int inner = 0; inner < temp; inner++) {
swap(matrix[outer][inner], matrix[inner][outer]);
}
temp++;
}
}
};
......@@ -165,21 +165,21 @@ public:
// 因为1+2+3 = 2 + 2 + 2且几乎无法区分!
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>> result;
map<string, std::vector<string>> table;
for (int i = 0; i < strs.size(); i++) {
string the_key = strs[i];
sort(the_key.begin(),the_key.end());
table[the_key].push_back(strs[i]);
}
vector<vector<string>> groupAnagrams(vector<string>& strs) {
vector<vector<string>> result;
map<string, std::vector<string>> table;
for (int i = 0; i < strs.size(); i++) {
string the_key = strs[i];
sort(the_key.begin(),the_key.end());
table[the_key].push_back(strs[i]);
}
for (auto it = table.begin(); it != table.end(); it++) {
result.push_back(it->second);
}
return result;
for (auto it = table.begin(); it != table.end(); it++) {
result.push_back(it->second);
}
return result;
}
};
// First Unique Character in a String
......@@ -187,18 +187,18 @@ public:
// it's much better than have a map
class Solution {
public:
int firstUniqChar(string s) {
vector<int> count(26, 0);
for(int i=0; i<s.size(); i++){
count[s[i]-'a']++;
}
for(int i=0; i<s.size(); i++){
if(count[s[i]-'a'] == 1){
return i;
}
}
return -1;
int firstUniqChar(string s) {
vector<int> count(26, 0);
for(int i=0; i<s.size(); i++){
count[s[i]-'a']++;
}
for(int i=0; i<s.size(); i++){
if(count[s[i]-'a'] == 1){
return i;
}
}
return -1;
}
};
......@@ -207,48 +207,109 @@ public:
// solution looks ugly but work, no really better solutions
class Solution {
public:
bool isValid(string s) {
stack<char> mystack;
mystack.push('1');
for(int i=0; i<s.size(); i++){
if(s[i] == ']'){
if(mystack.top() == '['){
mystack.pop();
}
else{
return false;
}
}
else if(s[i] == '}'){
if(mystack.top() == '{'){
mystack.pop();
}
else{
return false;
}
}
else if(s[i] == ')'){
if(mystack.top() == '('){
mystack.pop();
}
else{
return false;
}
}
else{
mystack.push(s[i]);
}
bool isValid(string s) {
stack<char> mystack;
mystack.push('1');
for(int i=0; i<s.size(); i++){
if(s[i] == ']'){
if(mystack.top() == '['){
mystack.pop();
}
else{
return false;
}
}
else if(s[i] == '}'){
if(mystack.top() == '{'){
mystack.pop();
}
else{
return false;
}
}
else if(s[i] == ')'){
if(mystack.top() == '('){
mystack.pop();
}
else{
return false;
}
return mystack.top() == '1' ? true : false;
}
else{
mystack.push(s[i]);
}
}
return mystack.top() == '1' ? true : false;
}
};
int trap(vector<int>& height) {
int sum = 0;
int left = -1;
int right = -1;
for(int i=0; i<height.size(); i++){
}
}
// Merge two sorted list
// Example:
//
// Input: 1->2->4, 1->3->4
// Output: 1->1->2->3->4->4
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
// create a dummy node which is am empty node and never being filled
ListNode dummy(0);
ListNode *tail = &dummy;
while(l1 && l2){
if(l1->val < l2->val){
tail->next = l1;
l1 = l1->next;
}
else{
tail->next = l2;
l2 = l2->next;
}
tail = tail->next;
}
tail->next = l1 ? l1 : l2;
return dummy.next;
}
};
// solution2, recursive one, think one layer only!!! so elegent!!
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(!l1) return l2;
if(!l2) return l1;
if(l1->val < l2->val){
l1->next = mergeTwoLists(l1->next, l2);
return l1;
}
else{
l2->next = mergeTwoLists(l1, l2->next);
return l2;
}
}
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment