原创 Leetcode——面試題 01.07. 旋轉矩陣

題目傳送門 思路: 先按對角線交換。 後每一行以中間的點爲對稱軸交換。 class Solution { public: void rotate(vector<vector<int>>& matrix) {

原创 Leetcode——876. 鏈表的中間結點(快慢指針法)

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListN

原创 Leetcode——8. 字符串轉換整數 (atoi)

題目傳送門 思路一:就簡單的面向過程但是要處理好邊界問題比較費勁 class Solution { public: int myAtoi(string str) { int strLength=st

原创 python易誤點——類的私有變量

先舉一個python類的例子 class Student(object): def __init__(self, name, score): self.name = name self.s

原创 Leetcode——1160. 拼寫單詞

class Solution { public: int countCharacters(vector<string>& words, string chars) { int re=0;

原创 python類 __contains(self,x)__方法

在Class裏添加__contains__(self,x)函數可以在類的實例化對象上進行 in 操作. class Graph(): def __init__(self): self.items = {'a

原创 Leetcode——289. 生命遊戲

傳送門 複製原數組模擬 class Solution { public: int index1[8]={1,1,1,0,0,-1,-1,-1}; int index2[8]={0,1,-1,1,-1,-1,0,1

原创 Leetcode——836. 矩形重疊

首先介紹一種比較笨的方法: 排除法:排除了不重疊的不就是重疊了嘛。 不重疊就那麼四種情況在左邊/右邊/上邊/下邊(左下右上等其實是可以歸於左邊/上邊的某種情況的) class Solution { public: bo

原创 Leetcode——999. 車的可用捕獲量

題目傳送門 這個題不難就是讀懂題意需要耐心一點。 class Solution { public: int north(int i,int j,vector<vector<char>>& board) {

原创 Leetcode——1162. 地圖分析

題目傳送門 本題的思路爲多源bfs,所謂多源,可以想象成有一個創世節點,然後多源的節點均爲其子節點,所以多源bfs本質上還是普通的單源bfs,只不過是從第二輪開始的。然後從每個陸地出發,一圈一圈搜,最後搜到的海洋即爲最遠的海洋。

原创 Leetcode——72. 編輯距離

題目傳送門 推薦看這個題解很精彩 下面給出c++寫法 自頂向下 class Solution { public: struct HashPair { size_t operator() (const pai

原创 Leetcode——151. 翻轉字符串裏的單詞

題目傳送門 最近開始轉戰python了,因爲以後工作可能會用python比較多。還是提取熟悉一下python吧。之前一直用c++刷是認爲用c++可以更加專注於算法本身,因爲python的api實在是太強了!!! class Sol

原创 Leetcode——409. 最長迴文串

class Solution { public: int longestPalindrome(string s) { int index[128]={0}; int sleng

原创 Leetcode——404. 左葉子之和

c++ /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * T

原创 Leetcode——563. 二叉樹的坡度

/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeN