- 博客(189)
- 资源 (4)
- 收藏
- 关注
原创 Algorithms: Design and Analysis, Part 1, Programming Assignment #5
import heapqimport sysfname = 'dijkstraData.txt'#fname = "tc.txt"fh = open(fname)lines = [line.rstrip('\n') for line in fh]# Generate graph as a list of dictionaries.# Index of list is the ver
2016-09-06 13:55:46
754
原创 Algorithms: Design and Analysis, Part 1Problem Set #5
Correct1 / 1 points1. Consider a directed graph with distinct and nonnegative edge lengths and a source svertex. Fix a destination vertex t, and assume that the graph contains
2016-09-06 02:21:41
1340
原创 aligned memory allocation
void *aligned_alloc(size_t size, size_t align) { void *p = NULL; void *ret = NULL; p = malloc(size + align + sizeof(void *)); if (!p) return NULL; size_t offset = align - (size_t)p % alig
2016-03-22 23:23:56
586
原创 Zenefits 第一轮编程题
两道题都是所有test cases都过了,直接贴代码了/* * #1 Flip bit */int bitFlip(int arr_size, int* arr) { int no_flip = 0; int flip = 0, flip_min = 0; int i = 0; int *transform = NULL; // Vali
2016-02-29 03:14:17
470
原创 Find the Duplicate Number
class Solution {public: int findDuplicate(vector& nums) { int n = nums.size(); int s = 1, e = n; while (s < e) { int m = s + (e - s) / 2; int count
2016-02-17 14:28:55
404
原创 Find Peak Element
class Solution {public: int findPeakElement(vector& nums) { if (nums.size() < 2) return 0; int i = 1; for (; i < nums.size(); i++) { if (nums[i] < nums[i - 1])
2016-02-16 03:46:01
411
原创 Find Median from Data Stream
class MedianFinder {private: vector nums;public: // Adds a number into the data structure. void addNum(int num) { int s = 0, e = nums.size() - 1; int m; while (s
2016-02-15 13:11:23
289
原创 Longest Increasing Subsequence
DP: O(n^2)class Solution {public: int lengthOfLIS(vector& nums) { int n = nums.size(); if (n < 2) return n; int *res = new int[n]; for (int i = 0; i <
2016-02-14 15:38:23
285
原创 297. Serialize and Deserialize Binary Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas
2016-02-10 17:29:33
349
原创 278. First Bad Version
class Solution {public: int firstBadVersion(int n) { int s = 1; int e = n; while (s < e) { int m = s + (e - s) / 2; if (isBadVersion(m)) {
2016-02-09 06:13:04
378
原创 328. Odd Even Linked List
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* od
2016-02-03 13:30:33
353
原创 num->string
C++:int Number = 123;string String = static_cast( &(ostringstream() str();std::string s = std::to_string(42);
2015-08-29 04:07:37
451
原创 Excel Sheet Column Number
class Solution {public: int titleToNumber(string s) { int num = 0; for (int i = 0; i < s.size(); i++) { num = num * 26 + (s[i] - 'A' + 1); } return num
2015-08-29 04:03:55
429
原创 Add Digits
class Solution {public: int addDigits(int num) { int temp = num; while(temp >= 10) { int sum = 0; while (temp) { sum += temp % 10;
2015-08-29 03:32:13
423
原创 Binary Tree Paths
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas
2015-08-29 03:27:45
378
原创 Missing Number
class Solution {public: int missingNumber(vector& nums) { int sum = 0; int num_sum = 0; int i = 0; for (; i < nums.size(); i++) { sum += i;
2015-08-28 14:42:40
398
原创 Implement Stack using Queues && Implement Queue using Stacks
Implement Stack using Queuesclass Stack {private: queueint> q;public: // Push element x onto stack. void push(int x) { queueint> t; while (!q.empty()) { t.p
2015-07-23 01:42:22
391
原创 Power of Two
class Solution {public: bool isPowerOfTwo(int n) { if (n < 1) return false; while (n > 2) { if (n % 2) return false; n = n/2; } return true
2015-07-23 01:03:22
350
原创 Maximum Gap
#1: sortO(nlogn)class Solution {public: int maximumGap(vectorint>& nums) { int n = nums.size(); if (n < 2) return 0; int res = 0; sort(nums.begin(), nums.end()
2015-07-21 14:44:16
312
原创 Summary Ranges
class Solution {public: string num2str(int num) { string res; int p = 0; long n = num; if (n < 0) { res.push_back('-'); p = 1;
2015-07-19 07:52:41
395
原创 Kth Smallest Element in a BST
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas
2015-07-17 03:04:45
331
原创 Delete Node in a Linked List
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */void deleteNode(struct ListNode* node) { *node = *(node->next);}
2015-07-16 01:33:12
418
原创 Lowest Common Ancestor of a Binary Tree
BST:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; *
2015-07-16 01:26:12
313
原创 Palindrome Linked List
#1 using stacktime: O(n); space O(n)/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */c
2015-07-14 15:34:25
393
原创 Rotate Array
class Solution {public: void rotate(vectorint>& nums, int k) { int n = nums.size(); if (k > n) k = k % n; if (n == k || k < 1) return; vectorint> temp(nums.begin(
2015-07-09 14:20:23
415
原创 Kth Largest Element in an Array
struct mintop { bool operator() (const int& a, const int& b) const { return a>b; }};typedef priority_queue, mintop> myqueue; class Solution {public: int findKthLargest(vector&
2015-06-26 15:32:06
370
原创 Minimum Size Subarray Sum
1. O(n^2)int minSubArrayLen(int s, int* nums, int numsSize) { if (!numsSize) return 0; int i = 0, j = 1; int res = numsSize + 1; int sum = nums[0]; for (i = 0; i < numsSize; i++)
2015-06-26 15:12:41
330
原创 Binary Search Tree Iterator
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class BSTIte
2015-06-24 15:15:53
279
原创 Min Stack
class MinStack { vector inputNum; vector minNum; public: void push(int x) { inputNum.push_back(x); int n = minNum.size(); if ((!n) || (n && x <= minNum[n - 1])
2015-06-24 14:48:02
318
原创 Invert Binary Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas
2015-06-24 14:25:22
496
原创 Convert a Ternary expression to a Binary tree structure
#include #include #include using namespace std;struct TreeNode { char val; TreeNode *left; TreeNode *right; TreeNode(char c): val(c), left(NULL), right(NULL) {}};// To execute C++, ple
2015-06-24 08:08:38
676
原创 c++ container with user defined compare function
1) sort by key:using function pointerbool key_compare(int a, int b){ return (a > b);}int main() { bool(*fn_cmp)(int, int) = key_compare; map m(fn_cmp);}2) sort by value:http://w
2015-06-23 16:27:23
614
原创 Majority Element
O(nlogn) -- sortvoid sort(int *nums, int s, int e) { if (s >= e) return; int p = nums[e]; int i = s, j = e; while (i < j) { while (i < j && nums[i] <= p) i++; nums[j]
2015-06-11 15:03:11
274
原创 Intersection of Two Linked Lists
用哈希表/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNo
2015-06-10 14:23:30
295
原创 Count Complete Tree Nodes
int countNodes(struct TreeNode* root) { if (!root) return 0; int left_height = 1, right_height = 1; struct TreeNode *left = root->left, *right = root->right; while(left) {
2015-06-08 13:59:47
407
原创 Repeated DNA Sequences
第一想法用map,直接存string会超内存,既然只有四个字母,用4进制数可以表示:class Solution {public: int str2num(string s) { int res = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == 'A') res = re
2015-06-06 01:34:00
302
原创 Reverse Linked List
Iteration:struct ListNode* reverseList(struct ListNode* head) { if (!head) return NULL; struct ListNode *res = (struct ListNode *)malloc(sizeof(struct ListNode)); struct ListNode *c
2015-06-03 13:21:49
307
原创 ZigZag Conversion
class Solution {public: string convert(string s, int numRows) { if(numRows <= 1 || s.size() <= 1) return s; string ret; int len = s.size(); int maxgap = numRows *
2015-06-03 03:32:36
328
原创 Reverse Bits
v1:class Solution {public: uint32_t reverseBits(uint32_t n) { uint32_t res = 0; uint32_t r = 1; for (int i =0; i< 32; i++) { uint32_t temp = n & r;
2015-06-02 11:00:46
322
原创 Happy Number
class Solution {public: bool isHappy(int n) { unordered_set num; int sum = n; do { int old = sum; num.insert(old); sum = 0;
2015-05-01 09:52:31
288
Cracking the Coding Interview
2013-01-17
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人
RSS订阅