摘要:
http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/栈使用#include #include #include #includeusing namespace std;class Solution {public: int toNum(string str) { int sum = 0; int i = 0; int flagPositiveOrNegative = 1;; if(str[0] == '-') { ...
阅读全文
posted @ 2014-02-14 21:24
qingcheng奕
阅读(161)
推荐(0)
摘要:
http://oj.leetcode.com/problems/valid-parentheses/对栈的考察,看括号的使用方式是否合法。class Solution {public: bool isValid(string s) { if(s.empty() || s == "") return true; if(s.size()%2!= 0) return false; int i = 0; stack myStack; while(i!=s.size()) ...
阅读全文
posted @ 2014-02-14 20:48
qingcheng奕
阅读(172)
推荐(0)
摘要:
http://oj.leetcode.com/problems/implement-strstr/判断一个串是否为另一个串的子串比较简单的方法,复杂度为O(m*n),另外还可以用KMP时间复杂度为O(m+n),之前面试的时候遇到过。class Solution {public: bool isEqual(char *a,char *b) { char* chPointer = a; char* chPointer2 = b; while(*chPointer2!= '\0' && *chPointer!='\0' ) { ...
阅读全文
posted @ 2014-02-14 15:40
qingcheng奕
阅读(152)
推荐(0)
摘要:
http://oj.leetcode.com/problems/valid-palindrome/判断是否为回文串 bool isPalindrome(string s) { int i = 0,j = s.length() -1; int flag = 0; if(s=="") return true; while(i= '0'&&s[i]= '0'&&s[j]<='9') || s[j] == ' ') { if(j==0) ...
阅读全文
posted @ 2014-02-14 11:04
qingcheng奕
阅读(133)
推荐(0)
摘要:
http://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/处理链表的范例#include using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; class Solution {public: ListNode *deleteDuplicates(ListNode *head) { if(head == NULL)...
阅读全文
posted @ 2014-02-14 10:08
qingcheng奕
阅读(154)
推荐(0)
摘要:
http://oj.leetcode.com/problems/partition-list/链表的处理#include using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; class Solution {public: ListNode *partition(ListNode *head, int x) { ListNode *head1 = NULL,*head2 = NULL,*tai...
阅读全文
posted @ 2014-02-13 22:17
qingcheng奕
阅读(144)
推荐(0)
摘要:
http://oj.leetcode.com/problems/reverse-linked-list-ii/链表的操作#include using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; class Solution {public: ListNode *reverseBetween(ListNode *head, int m, int n) { ListNode dummy(-1); ...
阅读全文
posted @ 2014-02-13 17:57
qingcheng奕
阅读(143)
推荐(0)
摘要:
http://oj.leetcode.com/problems/3sum/找出三个数的和是0的元组.class Solution {public: vector > threeSum(vector &num) { vector > ans; if(num.size()0) break; if(num[i]+num[j]+num[num.size()-1] onePiece; onePiece.push_back(num[...
阅读全文
posted @ 2014-02-13 13:27
qingcheng奕
阅读(321)
推荐(0)
摘要:
http://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/如果在数组中有重复的元素,则不一定说必定前面或者后面的一半是有序的,所以有个while循环的处理。#include using namespace std; class Solution {public: bool binarySearch(int A[],int target,int begin,int end) { int mid = (begin+end)/2; if(target == A[mid]) ...
阅读全文
posted @ 2014-02-12 17:33
qingcheng奕
阅读(138)
推荐(0)
摘要:
http://oj.leetcode.com/problems/search-in-rotated-sorted-array/转换了一次的有序数组,进行类似二分查找。从begin到mid和从mid到end,二者中肯定有一个是有序的。#include using namespace std; class Solution {public: int binarySearch(int A[],int target,int begin,int end) { int mid = (begin+end)/2; if(target == A[mid]) ...
阅读全文
posted @ 2014-02-12 16:39
qingcheng奕
阅读(122)
推荐(0)