Coding Puzzles, 2nd edition: Thinking in code
Here you can read online Coding Puzzles, 2nd edition: Thinking in code full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2014, genre: Home and family. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:
Romance novel
Science fiction
Adventure
Detective
Science
History
Home and family
Prose
Art
Politics
Computer
Non-fiction
Religion
Business
Children
Humor
Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.
- Book:Coding Puzzles, 2nd edition: Thinking in code
- Author:
- Genre:
- Year:2014
- Rating:3 / 5
- Favourites:Add to favourites
- Your mark:
- 60
- 1
- 2
- 3
- 4
- 5
Coding Puzzles, 2nd edition: Thinking in code: summary, description and annotation
We offer to read an annotation, description, summary or preface (depends on what the author of the book "Coding Puzzles, 2nd edition: Thinking in code" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.
Unknown: author's other books
Who wrote Coding Puzzles, 2nd edition: Thinking in code? Find out the surname, the name of the author of the book and a list of all author's works by series.
Coding Puzzles, 2nd edition: Thinking in code — read online for free the complete book (whole text) full work
Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "Coding Puzzles, 2nd edition: Thinking in code" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.
Font size:
Interval:
Bookmark:
Coding Puzzles Thinking in code nd Edition Dedicated to the memory of my dog, Maomao, who was passed away last year, aged 13. Preface to the second edition I made this book for fun at beginning, but I didnt expect I will get so many reponse from readers. Its really an interesting experience! What this new edition brings to you:
- Fix some typo in the first edition, reformat the code for readability, replace some graphs with high resolution images.
- Add 32 new problems to fullfil the problem list. In the second edition, it contains 136 problems in Recursion, Divid and Conquer, Binary Search, Tree Traversal, Graph Traversal, Dynamic Programming, String Search etc, which is more than enough for preparing a software engineer interview. For each new problem, I add more details of the problem and algorithm analysis.
- Add an Appendix in the end of this book for designing question preparation. This appendix includes some selected papers, books I had read in the past two years.
And I think this is the most important change in the second edition. Learning what current industry does and keeping improving the design skill will help yourself in a long-term career.
Make sure you have basic knowledge of data structure and algorithm, because this book is mostly focus on how to resolve the coding puzzles with existing data structure and algorithm. If you need some refresh of data structure and algorithm, there is a good book you might want to take a look first, by Thomas H. Cormen. This book has 105 puzzles. Every puzzle contains a detailed explanation and some implementations. If you find any error in this book, please send your findings to .
And at last, this book is only for fun, so that my friends can read it in their kindle offline. If you want to a free version, you can get it at http://fisherlei.blogspot.com/ , but the prerequisite is you need to be able to understand Chines e CodingTMD @Seattle Dec. 2013 r Contents *
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2 Solve this problem
[Thoughts] There are three sorts of solutions: The first solution is hashing. Scan the array from the left to the right, and store the number and its index(the original index in the array) in the map. Then, scan the array again with the help of map. The time complexity is O(n). The second solution is two-pointer scanning. Sort the array first, unfortunately, you need to remember the original index during sorting.
Its better to use a new object to hold the number-index mapping.After the array is sorted, then, use two pointers to scan from two bounds to the middle. Find the solution and return their original index. The time complexity is O(n*lgn). Note: Two-pointers Scanning Two-pointer is a optimized way to search targets on top of a SORTED array. One pointer points to the head of the sorted array, and the other one points to the tail of the sorted array. The two points move towards the middle of the array until they meet.
The third solution is brute-force searching. This is the most obvious solution, but with the highest time complexity O(n*n)
[Code]
Following is the execution of solution 1 : 1: vector twoSum(vector &numbers, int target) { 2: map mapping; 3: vector result; 4: for(int i =0; i< numbers.size(); i++) 5: { 6: mapping[numbers[i]]=i; 7: } 8: for(int i =0; i< numbers.size(); i++) 9: { 10: int searched = target - numbers[i]; 11: if(mapping.find(searched) != mapping.end()) 12: { 13: result.push_back(i+1); 14: result.push_back(mapping[searched]+1); 15: break; 16: } 17: } 18: return result; 19: } Solution 2: 1: struct Node 2: { 3: int val; 4: int index; 5: Node(int pVal, int pIndex):val(pVal), index(pIndex){} 6: }; 7: static bool compare(const Node &left, const Node &right) 8: { 9: return left.val < right.val; 10: } 11: vector twoSum(vector &numbers, int target) { 12: vector elements; 13: for(int i =0; i< numbers.size(); i++) 14: { 15: elements.push_back(Node(numbers[i], i)); 16: } 17: std::sort(elements.begin(), elements.end(), compare); 18: int start = 0, end = numbers.size()-1; 19: vector result; 20: while(start < end) 21: { 22: int sum = elements[start].val + elements[end].val; 23: if(sum == target) 24: { 25: result.push_back(elements[start].index+1); 26: if(elements[start].index < elements[end].index) 27: result.push_back(elements[end].index+1); 28: else 29: result.insert(result.begin(), elements[end].index+1); 30: break; 31: } 32: else if(sum > target) 33: end--; 34: else 35: start++; 36: } 37: return result; 38: }
Solution 3: two nested loop. The implementation of the solution is left out here.
- Elements in a triplet ( a , b , c ) must be in non-descending order. a + b + c =0 => a + b = -c So with the new equation, the problem is changed as finding two element a and b, which sums to c. a + b + c =0 => a + b = -c So with the new equation, the problem is changed as finding two element a and b, which sums to c.
Sort the array first. And use two pointers to scan from both sorted array bounds to middle. Since the problem requires no duplicate solution set. Need add some tricks to de-duplicate as below:
- Line 19 ~ 24.
- Line 37
For example [-2, -2, -2, 0,2].
[Code] 1: vector > threeSum(vector &num) { 2: std::sort(num.begin(), num.end()); 3: vector > result; 4: int len = num.size(); 5: for(int i =0; i< len; i++) 6: { 7: int target = 0-num[i]; 8: int start = i+1, end =len-1; 9: while(start 10: { 11: if(num[start] + num[end] == target) 12: { 13: vector solution; 14: solution.push_back(num[i]); 15: solution.push_back(num[start]); 16: solution.push_back(num[end]); 17: result.push_back(solution); 18: start++; end--; 19: while(start 20: && num[start] == num[start-1]) 21: start++; 22: while(start 23: && num[end] == num[end+1]) 24: end--; 25: } 26: else if(num[start] + num[end] < target) 27: { 28: start++; 29: } 30: else 31: { 32: end--; 33: } 34: } 35: if(i 36: { 37: while(num[i] == num[i+1]) i++; 38: } 39: } 40: return result; 41: }
Font size:
Interval:
Bookmark:
Similar books «Coding Puzzles, 2nd edition: Thinking in code»
Look at similar books to Coding Puzzles, 2nd edition: Thinking in code. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.
Discussion, reviews of the book Coding Puzzles, 2nd edition: Thinking in code and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.