Posts

Showing posts with the label 语法

401. Kth Smallest Number in Sorted Matrix

Code ( Language :C++) Edit class Solution { public : /** * @param matrix: a matrix of integers * @param k: An integer * @return: the kth smallest number in the matrix */ struct pqNode { int num; pair< int , int > pos; pqNode( int num, pair< int , int > pos){ this ->num = num; this ->pos = pos; } }; struct cmp { bool operator () (pqNode &n1, pqNode &n2) { return n1.num < n2.num; } }; int kthSmallest ( vector < vector < int >> &matrix, int k) { // write your code here const int m = matrix.size(); if (m == 0 ){ return 0 ; } const int n = matrix[ 0 ].size(); priority_queue<pqNode, vector <pqNode>, cmp> pq; pq.push(pqNode(matrix[ 0 ][ 0 ], { 0 , 0 })); vector < vector < int >> v...

471. Top K Frequent Words

ode ( Language :C++) Edit class Solution { public : /** * @param words: an array of string * @param k: An integer * @return: an array of string */ struct node { string word; int freq; node( string word, int freq){ this ->word = word; this ->freq = freq; } }; struct cmp { bool operator () ( const node &a, const node &b) { return a.freq > b.freq || (a.freq == b.freq && a.word < b.word); } }; vector < string > topKFrequentWords( vector < string > &words, int k) { // write your code here //统计次数; 以次数作为primary排序, pq unordered_map < string , int > mp; vector < string > res; const int size = words.size(); if (size == 0 ){ return res; } for ( auto i : words){ mp[i]++; } ...

454. Rectangle Area

#include < iostream > #include < vector > #include < cstdlib > using namespace std ; class Rectangle { int length ; int width ; public : Rectangle ( int x , int y ){ this ->length = x ; this ->width = y ; } int getArea (){ return width * length ; } }; /** * Your MyCalendar object will be instantiated and called as such: * MyCalendar obj = new MyCalendar(); * bool param_1 = obj.book(start,end); */ int main () {   cout << "Hello" << endl ; Rectangle * rec = new Rectangle ( 1 , 2 ); //注意new 需要用pointer //bool r2 = cal.book(10, 23); int area = rec->getArea (); cout << area << endl ;    return 0 ; }

Check LintCode 86. Binary Search Tree Iterator

Code ( Language :C++) Edit /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = this->right = NULL; * } * } * Example of iterate a tree: * BSTIterator iterator = BSTIterator(root); * while (iterator.hasNext()) { * TreeNode * node = iterator.next(); * do something for node */ class BSTIterator { public : /* * @param root: The root of binary tree. */ vector <TreeNode*> inOrder; int index = 0 ; BSTIterator(TreeNode * root) { // do intialization if necessary index = 0 ; inOrder = inOrderGen(root); } /* * @return: True if there has next node, or false */ bool hasNext () { // write your code here if (index < inOrder.size()){ return true ; } else { return false ; ...

Check LintCode 1065 My Calendar I - C++

设计类 构造函数不懂 class MyCalendar { private : vector < vector < int >> timeSlots; public : MyCalendar() { } bool book ( int start, int end) { if (timeSlots.size() == 0 ){ vector < int > times{start, end}; timeSlots.push_back(times); return true ; } for ( int i = 0 ; i < timeSlots.size(); i++){ if (!(start >= timeSlots[i][ 1 ] || end <= timeSlots[i][ 0 ])){ return false ; } } vector < int > times{start, end}; timeSlots.push_back(times); return true ; } }; /** * Your MyCalendar object will be instantiated and called as such: * MyCalendar obj = new MyCalendar(); * bool param_1 = obj.book(start,end); */ 1/30/2019更新 /*方法二:(构造特殊的二叉搜索树 (https://baike.baidu.com/item/%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91/7077855?fr=aladdin)) 1. 类似在常见的二叉搜索树中查找某个数一样,如果我们能够实现出一种特殊的二叉搜索树,使得:...

LintCode 425. Letter Combinations of a Phone Number

很有意思的一道題。DFS經典使用 DFS几个要素: 1,写入最终结果的条件 2, 黑盒子什么功能 3, 带入黑盒子的参数。 class Solution { public:     /**      * @param digits: A digital string      * @return: all posible letter combinations      */     const vector<string> letters {"", "","abc","def", // '0','1','2',...             "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};  // 语法1               vector<string> letterCombinations(string &digits) {         // write your code here         // DFS         vector<string> res;         int sizeD = digits.size();         if(sizeD == 0){             return res;         }         dfs(digits, res, 0, "");         return res;     ...

struct和typedef struct

struct 和typedef struct 分三块来讲述:   1 首先: //注意在C和C++里不同     在C中定义一个结构体类型要用typedef:      typedef struct Student     {     int a;     }Stu;     于是在声明变量的时候就可:Stu stu1;( 如果没有typedef就必须用struct Student stu1;来声明)     这里的Stu实际上就是struct Student的别名。 Stu==struct Student      另外这里也可以不写Student(于是也不能struct Student stu1;了,必须是Stu stu1;)      typedef struct     {     int a;     }Stu;     但在c++里很简单,直接      struct Student     {     int a;     };           于是就定义了结构体类型Student,声明变量时直接Student stu2; =========================================== ===========================================   2.其次:     在c++中如果用typedef的话,又会造成区别:      struct   Student        {        int   a;        }stu1;// stu1是一个变量          typedef   struct   Student2        {        int   a;        }stu2; //stu2是一个结构体类型=struct Student         使用时可以直接访问stu1.a     但是stu2则必须先...