線性迴歸是什麼。 所謂線性迴歸(以單變數為例),說白了就是給你一堆點,你要從這一堆點中找出一條直線。如下圖 本文截圖均出自 Andrew Ng的< 機器學習公開課> 找到這條直線以後可以做什麼。 我們假設我們找到了 代表這條直線的 a 和b,那麼直線運算式為 y = a + b*x, 那麼當 新來一個 x,我們就可以知道 y了。
解題思路: 1,暴力法,O(n^2) time O(1) space 2,可以利用 hash表,把 數組中的值 映射到 index, 每遍曆到一個數x,就看一下hash表中是否存在 target-x,如果存在, 就return了;不存在,就把x加入到 hash表中 時間複雜度 O(n),空間複雜度O(n) public class Solution {
題目:Given a number represented as an array of digits, plus one to the number. 題意動態數組存了一些個位元字,組成一個大數,計算這個大數加一之後的值。 類比大數的加法,當沒有進位的時候就可以彈出迴圈。 特殊考慮各個位元就是9的情況,加一後會多出一位,即結果是1加上len個0,len是digits的長度。 class Solution {public: vector<int>
/** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label; * RandomListNode *next, *random; * RandomListNode(int x) :
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x),
解題思路: 寬度搜尋。 1,增加一個queue記錄height; 2,從queue出來的node,如果與前一個node的height一樣,則讓前一個node->next = curnode; 3,每次迴圈,都更新preNode 和preHeight; /** * Definition for binary tree with next pointer. * struct TreeLinkNode
此博文主要講述了構造二叉樹的兩種方法: 1、通過先序和中序構造出二叉樹( 來自leetCode OJ上的 題目:Construct Binary Tree from Preorder and Inorder Traversal ) 2、通過後序和中序構造出二叉樹( 來自leetCode OJ上的 題目:Construct Binary Tree from Inorder and Postorder Traversal)
Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. 對一個數組進行加一操作~ 維護一個進位,對每一位都判斷進位,如果到了最高位,進位為1的話,就在數組前面插入1
給出一個數組代表一個數字,返回+1之後的數,用數組表示 public class Solution { public int[] plusOne(int[] digits) { boolean flag = true; for(int i = digits.length - 1; i >= 0; --i){ digits[i] += (flag ? 1 : 0); flag = digits[i] /