随笔分类 - Algorithm
摘要:Given a binary tree, return thepreordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,2,3].Note:Recursive solution is trivial, could you do it iteratively?Code:1. Recursive:class Solution {public: void findNode(vector &nodes,TreeNode *node){...
阅读全文
posted @ 2013-11-07 08:24
WinsCoder
摘要:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.Input:(2 -> 4 -> 3) + (5 -> 6 -> 4)Output:7 -> 0 -> 8Code:class Solution {
阅读全文
posted @ 2013-11-06 08:07
WinsCoder
摘要:Given a sorted linked list, delete all nodes that have duplicate numbers, leaving onlydistinctnumbers from the original list.For example,Given1->2->3->3->4->4->5, return1->2->5.Given1->1->1->2->3, return2->3.Code:class Solution {public: ListNode *deleteDuplicat
阅读全文
posted @ 2013-11-05 17:47
WinsCoder
摘要:Given a singly linked listL:L0→L1→…→Ln-1→Ln,reorder it to:L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given{1,2,3,4}, reorder it to{1,4,2,3}.Code:class Solution {public: void reorderList(ListNode *head) { if(head==NULL) return; Lis...
阅读全文
posted @ 2013-11-05 17:37
WinsCoder
摘要:Divide two integers without using multiplication, division and mod operator.Code:class Solution {public: int divide(int dividend, int divisor) { int quotient=0; long long d1=abs((long long)dividend); // change to type 64 bits, such as 'long long' or 'double' long long d2=abs(...
阅读全文
posted @ 2013-11-05 07:45
WinsCoder
摘要:Given a sorted linked list, delete all nodes that have duplicate numbers, leaving onlydistinctnumbers from the original list.For example,Given1->2->3->3->4->4->5, return1->2->5.Given1->1->1->2->3, return2->3.Code:class Solution {public: ListNode *reverseKGroup(
阅读全文
posted @ 2013-11-05 04:59
WinsCoder
摘要:单向链表的反转是一个经常被问到的一个面试题,也是一个非常基础的问题。比如一个链表是这样的: 1->2->3->4->5 通过反转后成为5->4->3->2->1。最容易想到的方法遍历一遍链表,利用一个辅助指针,存储遍历过程中当前指针指向的下一个元素,然后将当前节点元素的指针反转后,利用已经存储的指针往后面继续遍历。源代码如下:struct linka { int data; linka* next;};void reverse(linka*& head){ if(head ==NULL) return; linka*pre, *cur,
阅读全文
posted @ 2013-11-02 08:35
WinsCoder
摘要:Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.Follow up:Can you solve it without using extra space?Code:class Solution {public: ListNode *detectCycle(ListNode *head) { ListNode *p=head; ListNode *cur=head; while(p){ i...
阅读全文
posted @ 2013-11-01 05:45
WinsCoder
摘要:Given a linked list, swap every two adjacent nodes and return its head.For example,Given1->2->3->4, you should return the list as2->1->4->3.Your algorithm should use only constant space. You maynotmodify the values inthe list, only nodesitself can bechanged.Code:class Solution {pub
阅读全文
posted @ 2013-11-01 05:35
WinsCoder
摘要:Givennnon-negative integersa1,a2, ...,an, where each represents a point at coordinate (i,ai).nvertical lines are drawn such that the two endpoints of lineiis at (i,ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.Note: You
阅读全文
posted @ 2013-10-31 08:46
WinsCoder
摘要:Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete at mosttwotransactions.Note:You may not engage in multiple transactions at the same time (ie, you must sell thestock before you buy again).Code:Method1
阅读全文
posted @ 2013-10-31 03:49
WinsCoder
摘要:Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete at mosttwotransactions.Note:You may not engage in multiple transactions at the same time (ie, you must sell thestock before you buy again).Code:class S
阅读全文
posted @ 2013-10-31 03:48
WinsCoder
摘要:Code:class Solution {public: bool hasCycle(ListNode *head) { ListNode *p=head; ListNode *cur=head; while(p){ if(p->next) p=p->next->next; else return false; cur=cur->next; if(p==cur) retu...
阅读全文
posted @ 2013-10-30 07:46
WinsCoder
摘要:Code:class Solution {public: vector > fourSum(vector &num, int target) { vector buf; vector> res; if(num.size()p+2;q--) { i=p+1; j=q-1; while(ip+2&&num[q]==num[q-1]) q--; } while(...
阅读全文
posted @ 2013-10-30 07:44
WinsCoder
摘要:Code:class Solution {public: vector> threeSum(vector &num) { vector buf; vector> res; if(num.empty()) return res; sort(num.begin(),num.end()); int n=num.size(); int i,j,k; for(i=0;i<n-2;i++) { j=i+1; k=n-1; w...
阅读全文
posted @ 2013-10-30 07:42
WinsCoder
摘要:Code:class Solution {public: string intToRoman(int num) { string roman; int nums[13] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; char symbols[13] = {'M', 'C', 'D', 'C', 'C', 'X', 'L', 'X', 'X', 'I', 'V&
阅读全文
posted @ 2013-10-30 07:41
WinsCoder
摘要:Code:class Solution {public: void merge(int A[], int m, int B[], int n) { int i, j, count; for(i=0,j=0;i=B[j];j++) // count how many smaller than i count++; if(count>0){ // if there are some elements from B that need to insert into A for(int ...
阅读全文
posted @ 2013-10-28 07:45
WinsCoder
摘要:Code:O(n):class Solution {public: vector twoSum(vector &numbers, int target) { vector res; map mapping; int n=numbers.size(); for (int i=0; i twoSum(vector &numbers, int target) { vector res; int n=numbers.size(); for(int i=0;i<n;i++) fo...
阅读全文
posted @ 2013-10-28 00:24
WinsCoder
摘要:Code:class Solution {public: int maxDep; vector buf; // pack numbers into an array vector> res; // pack arrays into an result void dfs(int dep, vector &valid, vector &num){ if(dep==maxDep){ res.push_back(buf); return; } for(int i=0; ...
阅读全文
posted @ 2013-10-26 15:57
WinsCoder
摘要:Code:class Solution {public: int singleNumber(int A[], int n) { // Note: The Solution object is instantiated only once and is reused by each test case. int res = 0; int bit,i,j; for(i=0;i>i)&1==1) bit = (bit+1)%3; } if(bit!=...
阅读全文
posted @ 2013-10-23 02:40
WinsCoder