Leetcode#33. Search in Rotated Sorted Array
ProblemThere is an integer array nums sorted in ascending order (with distinct values).
Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2].
Given the array nums after the possible rotation and an integer target, return the ...
Leetcode#1. Two Sum
ProblemGiven an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
Example 1:
1234Input: nums = [2,7,11,15], target = 9Output: [0,1]Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:
123Input: nums = [3,2,4], target = 6Output: [1,2]
Example 3:
123Input: nums = [3,3 ...
Leetcode#337. House Robber III
ProblemThe thief has found himself a new place for his thievery again. There is only one entrance to this area, called root.
Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if two directly-linked houses were broken into on the same night.
Given the root of the binary tree, return the maximum amount of money the thief can rob without alerting the police ...
Leetcode#139. Word Break
ProblemGiven a string s and a dictionary of strings wordDict, return true if s can be segmented into a space-separated sequence of one or more dictionary words.
Note that the same word in the dictionary may be reused multiple times in the segmentation.
Example 1:
1234Input: s = "leetcode", wordDict = ["leet","code"]Output: trueExplanation: Return true because "leetcode" can be segmented as "leet code".
Example 2:
12345Input: s = "applepenap ...
時間複雜度
時間複雜度由小到大依次為:O(1) < O(logn) < O(n) < O(nlogn) < O(n^2) < O(n^3) < O(2^n) < O(n!) < O(n^n)
時間複雜度
描述
O(1)
常數時間
O(logn)
對數時間
O(n)
線性時間
O(nlogn)
線性對數時間
O(n^2)
平方時間
O(n^3)
立方時間
O(2^n)
指數時間
O(n!)
階乘時間
O(n^n)
階乘時間
常見的時間複雜度O(1):常數時間123def constant_algo(items): result = items[0] * items[0] print(result)
O(n):線性時間123def linear_algo(items): for item in items: print(item)
O(n^2):平方時間1234def quadratic_algo(items): for item in items: ...