原创 劍指offer 連續子數組的最大和

題目 HZ偶爾會拿些專業問題來忽悠那些非計算機專業的同學。今天測試組開完會後,他又發話了:在古老的一維模式識別中,常常需要計算連續子向量的最大和,當向量全爲正數的時候,問題很好解決。但是,如果向量中包含負數,是否應該包含某個負數,

原创 劍指offer 對稱的二叉樹

題目 請實現一個函數,用來判斷一顆二叉樹是不是對稱的。注意,如果一個二叉樹同此二叉樹的鏡像是同樣的,定義其爲對稱的。 思路 遞歸 代碼 # -*- coding:utf-8 -*- # class TreeNode: #

原创 python按照多個條件排序的方法

今天小編就爲大家分享一篇python按照多個條件排序的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧 對tuple進行排序,先按照第一個元素升序,如果第一個元素相同,再按照第二個元素降序排列。

原创 LeetCode 605. Can Place Flowers

題目 代碼 class Solution: def canPlaceFlowers(self, flowerbed, n): """ :type flowerbed: List[int]

原创 LeetCode 122. Best Time to Buy and Sell Stock II

題目 代碼 class Solution: def maxProfit(self, prices): """ :type prices: List[int] :rtype: int

原创 LeetCode 665. Non-decreasing Array

題目 代碼 class Solution: def checkPossibility(self, nums): """ :type nums: List[int] :rtype:

原创 LeetCode 167. Two Sum II - Input array is sorted

題目 代碼 class Solution: def twoSum(self, numbers, target): """ :type numbers: List[int] :typ

原创 劍指offer 二叉樹的深度

題目 輸入一棵二叉樹,求該樹的深度。從根結點到葉結點依次經過的結點(含根、葉結點)形成樹的一條路徑,最長路徑的長度爲樹的深度。 思路 遞歸 代碼 # -*- coding:utf-8 -*- # class TreeNode: #

原创 LeetCode 455. Assign Cookies

題目 思路 貪心 代碼 class Solution: def findContentChildren(self, g, s): """ :type g: List[int] :t

原创 LeetCode 392. Is Subsequence

題目 代碼 class Solution: def isSubsequence(self, s, t): """ :type s: str :type t: str

原创 LeetCode 406. Queue Reconstruction by Height

題目 代碼 class Solution: def insert(self, people, fromindex, toindex): tmp = people[fromindex] for i

原创 LeetCode 763. Partition Labels

題目 代碼 class Solution: def partitionLabels(self, S): """ :type S: str :rtype: List[int]

原创 LeetCode 452. Minimum Number of Arrows to Burst Balloons

題目 代碼 class Solution: def findMinArrowShots(self, points): """ :type points: List[List[int]]

原创 劍指offer 和爲s的兩個數字

題目 輸入一個遞增排序的數組和一個數字S,在數組中查找兩個數,是的他們的和正好是S,如果有多對數字的和等於S,輸出兩個數的乘積最小的。 輸出描述: 對應每個測試案例,輸出兩個數,小的先輸出。 思路 雙指針,一個從左向右遍歷,一個從右向

原创 劍指offer 左旋轉字符串

題目 彙編語言中有一種移位指令叫做循環左移(ROL),現在有個簡單的任務,就是用字符串模擬這個指令的運算結果。對於一個給定的字符序列S,請你把其循環左移K位後的序列輸出。例如,字符序列S=”abcXYZdef”,要求輸出循環左移3位後的結