- 博客(117)
- 收藏
- 关注
原创 Java implements 与 extends总结
接口和抽象类的区别是什么? (1)接口只有定义,其方法不能在接口中实现,只有实现接口的类才能实现接口中定义的方法,而抽象类可以有方法的定义和实现 (2)接口需要实现(implements),但抽象类只能被继承(extends)。一个类可实现多个接口,但只能继承一个类,接口可借此实现多重继承。 (3)接口:“has-a”,继承:"is-a” (4)接口中成员默...
2018-05-24 18:49:50 1176
原创 abstract class 和 interface
使用abstract class的方式定义Demo抽象类的方式如下:abstract class Demo { abstract void method1(); abstract void method2(); …}使用interface的方式定义Demo抽象类的方式如下:interface Demo { void method1(); void meth...
2018-05-24 18:47:29 182
原创 204. Count Primes
class Solution {public: int countPrimes(int n) { //素数不能被比它小的整数整除, 建一个boolean 数组, 从2开始, 把其倍数小于N的都删掉. vector<int> res(n,-1); int count=0; for(int i=2;i*i<n;...
2018-05-31 22:23:07 70
原创 202. Happy Number
class Solution {public: bool isHappy(int n) { unordered_set<int> set; while(n!=1){ //sum int sum=0; while(n/10!=0){ sum +...
2018-05-31 22:22:31 66
原创 200. Number of Islands
class Coor{public: int x,y; Coor(int x, int y){ this->x = x; this->y = y; }};class Solution {public: /** * @param grid a boolean 2D matrix * @return...
2018-05-30 21:41:56 84
原创 199. Binary Tree Right Side View
class Solution {public: vector<int> rightSideView(TreeNode* root) { vector<int> res; if(root == NULL) return res; queue<TreeNode*> q; ...
2018-05-30 21:41:18 88
原创 198. House Robber
class Solution {public: int rob(vector<int>& nums) { int res=0; if(nums.empty()) return res; vector<int> dp(nums.size()+1,0); //vector<int...
2018-05-30 21:40:42 136
原创 173. Binary Search Tree Iterator
class BSTIterator {public: stack<TreeNode*> s; TreeNode* current; BSTIterator(TreeNode *root) { current = root; } /** @return whether we have a next smallest number...
2018-05-30 21:39:58 97
原创 166. Fraction to Recurring Decimal
class Solution {public: string fractionToDecimal(int numerator, int denominator) { if(numerator==0) return "0"; string res; unordered_map<int,int> map; ...
2018-05-30 21:39:07 97
原创 162. Find Peak Element
class Solution {public: int findPeakElement(vector<int>& nums) { if(nums.empty()){ return 0; } int start = 0, end = nums.size()-1; while(star...
2018-05-30 21:38:33 69
原创 160. Intersection of Two Linked Lists
class Solution {public: ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { if(headA == NULL || headB == NULL){ return NULL; } ListNode *...
2018-05-30 21:38:01 55
原创 155. Min Stack
class MinStack {public: stack<int> s,smin; /** initialize your data structure here. */ MinStack() { } void push(int x) { if(s.empty() || x<=smin.top...
2018-05-30 21:37:19 83
原创 154. Find Minimum in Rotated Sorted Array II
class Solution {public: int findMin(vector<int>& nums) { int start = 0, end = nums.size() - 1; int mid; while(start + 1 < end){ //+1!! ...
2018-05-30 21:36:47 145
原创 Spark中的groupByKey 、aggregateByKey、reduceByKey 的区别
1.函数用法 (1)groupByKey的函数用法 groupByKey(numPartitions) (2) aggregateByKey 的函数原型def aggregateByKey[U: ClassTag](zeroValue: U, partitioner: Partitioner) (seqOp: (U, V) => U, combOp:...
2018-05-30 17:54:51 10636
原创 153. Find Minimum in Rotated Sorted Array
class Solution {public: int findMin(vector<int>& nums) { int start = 0, end = nums.size() - 1; int mid; while(start + 1 < end){ //+1!! ...
2018-05-29 23:59:56 54
原创 150. Evaluate Reverse Polish Notation
class Solution {public: int evalRPN(vector<string>& tokens) { // Write your code here stack<int> s; int i=0,res; while(i<tokens.size()){ ...
2018-05-29 23:59:20 86
原创 148. Sort List
class Solution {public: ListNode* sortList(ListNode* head) { if(head == NULL || head->next == NULL){ return head; } ListNode *first = head; ListNode...
2018-05-29 22:51:48 125
原创 144. Binary Tree Preorder Traversal
class Solution {public: vector<int> preorderTraversal(TreeNode* root) { vector<int> res; if(root == NULL) return res; stack<TreeNode*> s; ...
2018-05-29 22:49:50 57
原创 143. Reorder List
class Solution {public: void reorderList(ListNode* head) { // write your code here if(head == NULL || head->next == NULL){ return; } ListNo...
2018-05-29 18:03:59 161
原创 142. Linked List Cycle II
class Solution {public: ListNode *detectCycle(ListNode *head) { if(head == NULL || head->next == NULL){ return NULL; } ListNode *first = head; ListN...
2018-05-28 23:56:07 83
原创 141. Linked List Cycle
class Solution {public: bool hasCycle(ListNode *head) { if(head == NULL || head->next == NULL){ return false; } ListNode *first = head->next; Lis...
2018-05-28 23:55:32 61
原创 136. Single Number
class Solution {public: int singleNumber(vector<int>& nums) { unordered_set<int> s; for(int i=0;i<nums.size();i++){ if(s.find(nums[i])!=s.end()) ...
2018-05-28 23:55:00 56
原创 133. Clone Graph
/** * Definition for undirected graph. * struct UndirectedGraphNode { * int label; * vector<UndirectedGraphNode *> neighbors; * UndirectedGraphNode(int x) : label(x) {}; * }; ...
2018-05-28 23:54:27 163
原创 131. Palindrome Partitioning
class Solution {public: /** * @param s: A string * @return: A list of lists of string */ vector<vector<string>> partition(string s) { // write your code her...
2018-05-28 23:53:25 52
原创 129. Sum Root to Leaf Numbers
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas...
2018-05-28 23:52:45 68
原创 125. Valid Palindrome
class Solution {public: bool isPalindrome(string s) { if(s.empty()){ return true; } int left = 0, right = s.size()-1; while(left<right){ ...
2018-05-28 23:52:08 60
原创 124. Binary Tree Maximum Path Sum
class Solution {public: int res; int maxPathSum(TreeNode *root) { // write your code here if(root == NULL){ return 0; } res = root->val; ...
2018-05-28 23:51:03 49
原创 121. Best Time to Buy and Sell Stock
class Solution {public: int maxProfit(vector<int>& prices) { if(prices.empty()) return 0; int buy=prices[0]; int res=0; for(int i=1;i<pric...
2018-05-28 23:50:29 48
原创 120. Triangle
class Solution {public: int minimumTotal(vector<vector<int>>& triangle) { int m=triangle.size(); if(m==0) return 0; vector<int> f(m,INT_M...
2018-05-28 23:49:27 122
原创 118. Pascal's Triangle
class Solution {public: vector<vector<int>> generate(int numRows) { vector<vector<int>> res(numRows); for(int i=0;i<numRows;i++){ r...
2018-05-27 23:13:52 75
原创 117. Populating Next Right Pointers in Each Node II
/** * Definition for binary tree with next pointer. * struct TreeLinkNode { * int val; * TreeLinkNode *left, *right, *next; * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) ...
2018-05-27 23:11:52 56
原创 116. Populating Next Right Pointers in Each Node
/** * Definition for binary tree with next pointer. * struct TreeLinkNode { * int val; * TreeLinkNode *left, *right, *next; * TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) ...
2018-05-27 23:11:09 59
原创 114. Flatten Binary Tree to Linked List
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas...
2018-05-27 23:10:08 144
原创 113. Path Sum II
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */clas...
2018-05-27 23:09:32 72
原创 112. Path Sum
112. Path Sum/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} ...
2018-05-27 23:08:44 60
原创 111. Minimum Depth of Binary Tree
111. Minimum Depth of Binary Treeclass Solution {public: int minDepth(TreeNode* root) { if(root == NULL){ return 0; } int left = minDepth(root->left); ...
2018-05-27 23:08:05 56
原创 110. Balanced 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...
2018-05-27 22:59:58 50
原创 109. Convert Sorted List to Binary Search Tree
class Solution {public: TreeNode* sortedListToBST(ListNode* head) { return sortedListToBST(head,NULL); } TreeNode* sortedListToBST(ListNode* head,ListNode* tail){ if(head=...
2018-05-27 22:57:57 51
原创 108. Convert Sorted Array to Binary Search Tree
class Solution {public: TreeNode* sortedArrayToBST(vector<int>& nums) { if(nums.empty()) return NULL; int mid=nums.size()/2; TreeNode *root=new TreeN...
2018-05-27 10:39:34 101
原创 107. Binary Tree Level Order Traversal II
class Solution {public: vector<vector<int>> levelOrderBottom(TreeNode* root) { vector<vector<int>> res; if(root == NULL){ return res; }...
2018-05-26 19:38:17 59
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人