摘要: Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.Code:class Solution {public: int romanToInt(string s) { int len=s.length(); map character; character['I'] = 1; character['V'] = 5; character['X'] = 10; ... 阅读全文
posted @ 2013-11-08 17:39 WinsCoder 阅读(105) 评论(0) 推荐(0) 编辑
摘要: Given a sorted array, remove the duplicates in place such that each element appear onlyonceand return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.For example,Given input array A =[1,1,2],Your function should return length =2, and A is 阅读全文
posted @ 2013-11-08 17:19 WinsCoder 阅读(86) 评论(0) 推荐(0) 编辑
摘要: Implementint sqrt(int x).Compute and return the square root ofx.Code:class Solution {public: int sqrt(int x) { int start=0; int end=x/2>std::sqrt(INT_MAX)?std::sqrt(INT_MAX):x/2+1; while(start<=end){ int mid=(start+end)/2; if(x==mid*mid) r... 阅读全文
posted @ 2013-11-08 17:17 WinsCoder 阅读(149) 评论(0) 推荐(0) 编辑
摘要: 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 阅读(118) 评论(0) 推荐(0) 编辑
摘要: 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 阅读(152) 评论(0) 推荐(0) 编辑
摘要: 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 阅读(143) 评论(0) 推荐(0) 编辑
摘要: 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 阅读(195) 评论(0) 推荐(0) 编辑
摘要: 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 阅读(134) 评论(0) 推荐(0) 编辑
摘要: 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 阅读(140) 评论(0) 推荐(0) 编辑
摘要: 单向链表的反转是一个经常被问到的一个面试题,也是一个非常基础的问题。比如一个链表是这样的: 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 阅读(222) 评论(0) 推荐(0) 编辑