- 博客(87)
- 收藏
- 关注
![](https://csdnimg.cn/release/blogv2/dist/pc/img/listFixedTop.png)
原创 Bayesian Face Revisited: A Joint Formulation 算法流程图
Joint Bayesian Face Verification
2014-07-14 12:49:52
12935
69
原创 [LeetCode] Decode Ways
int numDecodings(string s) { if (s.length() == 0 || s[0] == '0') { return 0; } else if (s.length() == 1) { return 1; } int numWithoutLastTwo = 1, numWithoutLastOne = 1, num = 0; for (int
2014-07-24 20:29:30
909
原创 [LeetCode] ZigZag Conversion
string convert(string s, int nRows) { if (nRows <= 1) { return s; } string* rowStr = new string[nRows]; bool down = true; int i = 0, row = 0; while (i < s.length()) { if (down) { row
2014-07-20 16:26:58
582
原创 C++中 string转int 和 int转string
#include using namespace std;stringstream strStream;string str = "-123";stream<<str;int val;stream>>val;
2014-07-20 15:16:53
1389
原创 [LeetCode] Count and Say
string countAndSay(string s) { string result; int index1 = 0, index2 = 0, count = 0; while(index1 < s.length()) { if (s[index2] == s[index1]) { count++; index2++; } if (index2 =
2014-07-20 15:02:56
631
原创 [LeetCode] Anagrams
vector anagrams(vector &strs) { map hashTable; for (int i = 0; i < strs.size(); i++) { string str = strs[i]; sort(str.begin(), str.end()); if (hashTable.count(str) == 0) { hashTable[str]
2014-07-17 22:23:58
541
原创 [LeetCode] 3Sum
vector > threeSum(vector &num) { sort(num.begin(), num.end()); vector> triplets; int n = num.size(); if (n == 0) { return triplets; } int lastNum = num[0] + 1; for (int aIndex = 0; aIndex <
2014-07-17 21:41:16
435
原创 [LeetCode] Insert Interval
vector insert(vector &intervals, Interval newInterval) { int beg = intervals.size(), end = intervals.size()-1; bool getBeg = false; for (int i = 0; i < intervals.size(); i++) { if (newInterval.s
2014-07-16 19:04:16
504
原创 [LeetCode] Longest Common Prefix
string longestCommonPrefix(vector &strs) { string commonPrefix; if(strs.empty()) { return commonPrefix; } int index = 0; while(1) { for(int i = 0; i < strs.size(); i++) { string str = strs
2014-07-15 11:16:35
522
原创 [LeetCode] Pow(x, n)
double pow(double x, int n) { if (n == INT_MIN) { return 1.0/(pow(x, -(n+1))*x); } else if(n < 0) { return 1.0/pow(x, -n); } else if(n == 0) { return 1.0; } else if (n == 1) { retur
2014-07-15 10:43:56
449
原创 [LeetCode] Two Sum
vector twoSum(vector &numbers, int target) { vector twoIndex; map numIndex; for(int i = 0; i < numbers.size(); i++) { int num = target - numbers[i]; if(!numIndex.count(num)) { numIndex[numbe
2014-07-15 10:32:39
511
原创 [LeetCode] Longest Consecutive Sequence
int longestConsecutive(vector &num) { map map; for (int i = 0; i < num.size(); i++) { map[num[i]] = true; } int maxLen = 0; for (int i = 0; i < num.size(); i++) { if (map[num[i]] == tru
2014-07-14 21:45:35
490
原创 [LeetCode] Implement strStr()
char *strStr(char *haystack, char *needle) { if (haystack == NULL || needle == NULL){ return NULL; } int haystack_len = strlen(haystack); int needle_len = strlen(needle); for(int i = 0; i <= ha
2014-07-14 18:50:32
577
原创 [LeetCode] Candy
int candy(vector &ratings) { int n = ratings.size(); int* candy = new int[n]; for(int i = 0; i < n; i++) { candy[i] = 1; } for(int i = 1; i < n; i++) { if(ratings[i] > ratings[i-1] && candy[i
2014-07-14 15:51:23
529
原创 [LeetCode] Palindrome Number
bool isPalindrome(int x) { if (x < 0) { return false; } int xReverse = 0; int xTmp = x; while (xTmp != 0) { if (xReverse > INT_MAX/10) { return false; } xReverse *= 10; if ((IN
2014-07-13 22:54:12
476
原创 [LeetCode] String to Integer (atoi)
int atoi(const char *str) { if (str == NULL) { return 0; } while (*str == ' ') { str++; } bool negative = false; if (*str == '+') { negative = false; str++; } else if (*str == '-')
2014-07-13 22:07:09
591
原创 [LeetCode] Valid Palindrome
bool isPalindrome(string s) { int left = 0, right = s.length()-1; while (left <= right) { char leftChar = s[left]; if (!((leftChar >= '0' && leftChar = 'a' && leftChar = 'A' && leftChar <= 'Z')
2014-07-13 16:51:54
451
原创 [LeetCode] Validate Binary Search Tree
vector inorderTraversal(TreeNode *root) { vector pre_node; vector val; if (root == NULL) { return val; } pre_node.push_back(root); while(!pre_node.empty()) { TreeNode* cur_node = pre_node
2014-07-13 16:28:06
523
原创 [LeetCode] Partition List
ListNode *partition(ListNode *head, int x) { ListNode* virtualHead = new ListNode(-1); virtualHead->next = head; ListNode* lessNode = virtualHead; ListNode* preNode = virtualHead; ListNode* curNo
2014-07-13 16:03:50
439
原创 [LeetCode] Search in Rotated Sorted Array II
bool search(int A[], int n, int target) { int beg = 0, end = n-1; while (beg <= end) { int mid = (beg+end)/2; if (A[beg] < A[end]) { if (target < A[mid]) { end = mid - 1; }
2014-07-13 14:05:03
421
原创 [LeetCode] Subsets
vector> combine(vector &nums, int k) { vector> combinations; if (k == 0) { vector combination; combinations.push_back(combination); return combinations; } if (k == nums.size()) { combina
2014-07-13 11:26:02
521
原创 [LeetCode] Combinations
vector> combine(vector &nums, int k) { vector> combinations; if (k == 0) { vector combination; combinations.push_back(combination); return combinations; } if (k == nums.size()) { combina
2014-07-13 10:58:22
663
原创 [LeetCode] Permutations II
vector > permuteUnique(vector &num) { vector> permutations; if (num.size() <= 1) { permutations.push_back(num); return permutations; } sort(num.begin(), num.end()); int lastNum = num.back()
2014-07-12 22:33:54
476
原创 [LeetCode] Permutation
vector > permute(vector &num) { vector> permutations; if (num.size() <= 1) { permutations.push_back(num); return permutations; } int lastNum = num.back(); num.pop_back(); vector> permutatio
2014-07-12 21:47:12
465
原创 [LeetCode] Letter Combinations of a Phone Number
vector letterCombinations(string digits) { string letters[26] = {"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; vector letterCombos; if (digits.length() == 0) { string letterCombo; letter
2014-07-12 20:50:13
585
原创 [LeetCode] Convert Sorted List to Binary Search Tree
TreeNode *sortedListToBST(ListNode *head) { if(head == NULL) { return NULL; } ListNode* virtual_head = new ListNode(-1); virtual_head->next = head; ListNode *pLast = virtual_head, *pSlow = head
2014-07-11 16:58:28
1200
原创 [LeetCode] Convert Sorted Array to Binary Search Tree
TreeNode *sortedArrayToBST(vector &num) { int n = num.size(); int beg = 0, end = n-1; TreeNode* root = NULL; if(beg <= end) { int mid = (beg + end + 1)/2; root = new TreeNode(num[mid]); vect
2014-07-11 15:59:59
648
原创 [LeetCode] Balanced Binary Tree
int height(TreeNode *root) { if(root == NULL) { return 0; } int left_height = height(root->left); int right_height = height(root->right); return left_height > right_height? (left_height+1) :(ri
2014-07-11 14:51:26
460
原创 [LeetCode] Gas Station
int canCompleteCircuit(vector &gas, vector &cost) { vector gasLeft; for(int i = 0; i < gas.size(); i++) { gasLeft.push_back(gas[i] - cost[i]); } int startStation = 0, gasBalance = 0, gasCost = 0
2014-07-11 11:15:16
462
原创 [LeetCode] Copy List with Random Pointer
RandomListNode *copyRandomList(RandomListNode *head) { if(head == NULL) { return NULL; } RandomListNode* curNode = head; while(curNode != NULL) { RandomListNode* curCopyNode = new RandomLi
2014-07-11 10:29:38
538
原创 [LeetCode] First Missing Positive
int firstMissingPositive(int A[], int n) { for(int i = 0; i < n; ) { if(A[i] n || A[A[i]-1] == A[i]) i++; else swap(A[i], A[A[i]-1]); } for(int i = 0; i < n; ) { if(A[i] == i+1) i++
2014-07-11 09:35:40
409
原创 [LeetCode] Jump Game II
int jump(int A[], int n) { int* steps = new int[n]; steps[n-1] = 0; for (int i = n-2; i >= 0; i--) { steps[i] = n; int furthest_index = (i + A[i]) < (n-1)? (i + A[i]) : (n-1); steps[i] = ste
2014-07-10 19:42:18
425
原创 [LeetCode] Reorder List
void reorderList(ListNode *head) { int node_sum = 0; ListNode* cur_node = head; while(cur_node != NULL) { node_sum++; cur_node = cur_node->next; } ListNode* list1 = head; int cnt = 1;
2014-07-09 17:49:14
472
原创 [LeetCode] Insertion Sort List
ListNode *insertionSortList(ListNode *head) { if(head == NULL) { return head; } ListNode * sorted_list = new ListNode(0); sorted_list->next = head; ListNode* unsorted_list = head->next; head-
2014-07-09 16:05:54
380
原创 [LeetCode] Path Sum II
vector > pathSum(TreeNode *root, int sum) { vector> path_sum; vector nodes; vector> paths; vector sums; if(root != NULL) { nodes.push_back(root); sums.push_back(root->val); vector path;
2014-07-09 14:53:57
544
原创 [LeetCode] Minimum Depth of Binary Tree
int minDepth(TreeNode *root) { vector nodes; vector depths; int min_depth = 0; if(root != NULL) { nodes.push_back(root); depths.push_back(1); } while(!nodes.empty()) { TreeNode* cur_node
2014-07-09 14:03:23
478
原创 [LeetCode] Sum Root to Leaf Numbers
题目链接int sumNumbers(TreeNode *root) { vector nodes; vector numbers; int sum_numbers = 0; if(root != NULL) { nodes.push_back(root); numbers.push_back(root->val); } while(!no
2014-07-09 13:31:43
452
原创 [LeetCode] Flatten Binary Tree to Linked List
题目链接void flatten(TreeNode *root) { vector nodes; nodes.push_back(root); while(!nodes.empty()) { TreeNode* cur_node = nodes.back(); TreeNode* left_child = NULL; TreeNode* right_child = NULL
2014-07-09 13:01:54
345
原创 [LeetCode] Search in Rotated Sorted Array
题目链接int search(int A[], int n, int target) { int beg = 0, end = n-1; while(beg <= end) { int mid = (beg+end)/2; if(A[beg] <= A[end]) { if(target < A[mid]) { end = mid - 1; }
2014-07-09 11:17:36
327
原创 [LeetCode] Populating Next Right Pointers in Each Node II
题目链接void connect2(TreeLinkNode *root) { TreeLinkNode* level_head = root; while(level_head != NULL) { TreeLinkNode* next_level_head = NULL; bool get_next_head = false; TreeLinkNode* cur_node
2014-07-09 10:23:06
379
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人