From 8eebe1bc1e1cd7868070ff42417a2c14fc5598cd Mon Sep 17 00:00:00 2001 From: sarahHe Date: Thu, 8 Jan 2015 14:08:44 -0500 Subject: [PATCH 1/3] 2015.1.8 BFS + unordered_map --- Word Ladder.cpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Word Ladder.cpp diff --git a/Word Ladder.cpp b/Word Ladder.cpp new file mode 100644 index 0000000..ed1dead --- /dev/null +++ b/Word Ladder.cpp @@ -0,0 +1,33 @@ +class Solution { +public: + int ladderLength(string start, string end, unordered_set &dict) { + if (dict.empty()) return 0; + + queue matched; + matched.push(start); + unordered_map step_map; + step_map[start] = 1; + + while (!matched.empty()) { + string str = matched.front(); + matched.pop(); + for (int i = 0; i < str.length(); i++) { + string copy_str = str; + for (char j = 'a'; j <= 'z'; j++) { + if (copy_str[i] == j) continue; + + copy_str.replace(i, 1, 1, j); + if (copy_str == end) + return step_map[str] + 1; + + if (dict.find(copy_str) != dict.end() && !step_map[copy_str]) { + matched.push(copy_str); + step_map[copy_str] = step_map[str] + 1; + } + + } + } + } + return 0; + } +}; From 537576d7b605c54a75c158f96284d7fab7a8c008 Mon Sep 17 00:00:00 2001 From: sarahHe Date: Thu, 8 Jan 2015 14:09:51 -0500 Subject: [PATCH 2/3] Delete Word Ladder.cpp --- Word Ladder.cpp | 33 --------------------------------- 1 file changed, 33 deletions(-) delete mode 100644 Word Ladder.cpp diff --git a/Word Ladder.cpp b/Word Ladder.cpp deleted file mode 100644 index ed1dead..0000000 --- a/Word Ladder.cpp +++ /dev/null @@ -1,33 +0,0 @@ -class Solution { -public: - int ladderLength(string start, string end, unordered_set &dict) { - if (dict.empty()) return 0; - - queue matched; - matched.push(start); - unordered_map step_map; - step_map[start] = 1; - - while (!matched.empty()) { - string str = matched.front(); - matched.pop(); - for (int i = 0; i < str.length(); i++) { - string copy_str = str; - for (char j = 'a'; j <= 'z'; j++) { - if (copy_str[i] == j) continue; - - copy_str.replace(i, 1, 1, j); - if (copy_str == end) - return step_map[str] + 1; - - if (dict.find(copy_str) != dict.end() && !step_map[copy_str]) { - matched.push(copy_str); - step_map[copy_str] = step_map[str] + 1; - } - - } - } - } - return 0; - } -}; From ded538a73745265967616a8dcf3ce9d4d512e98c Mon Sep 17 00:00:00 2001 From: sarahHe Date: Mon, 16 Feb 2015 11:40:31 -0500 Subject: [PATCH 3/3] Update Remove_Duplicates_from_Sorted_List.cc --- Remove_Duplicates_from_Sorted_List.cc | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/Remove_Duplicates_from_Sorted_List.cc b/Remove_Duplicates_from_Sorted_List.cc index 0ae691b..54020af 100644 --- a/Remove_Duplicates_from_Sorted_List.cc +++ b/Remove_Duplicates_from_Sorted_List.cc @@ -9,22 +9,15 @@ class Solution { public: ListNode *deleteDuplicates(ListNode *head) { - // Start typing your C/C++ solution below - // DO NOT write int main() function - ListNode **pCur = &head; - ListNode *pPre = NULL; - ListNode *entry = NULL; - while (*pCur != NULL) { - entry = *pCur; - if (pPre != NULL && pPre->val == entry->val) { - *pCur = entry->next; - delete entry; - entry = NULL; - } else { - pCur = &(entry->next); - pPre = entry; - } + ListNode *p = head, *q; + while (p && p->next) { + q = p->next; + while (q && p->val == q->val) + q = q->next; + + p->next = q; + p = q; } return head; } -}; \ No newline at end of file +};