목록Programming (52)
MJay

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. Example: Input: [-2,1,-3,4,-1,2,1,-5,4], Output: 6 Explanation: [4,-1,2,1] has the largest sum = 6. Follow up: If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree. Example 1: Input: Tree 1..

Given the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclusive). The binary search tree is guaranteed to have unique values. Example 1: Input: root = [10,5,15,3,7,null,18], L = 7, R = 15 Output: 32 Example 2: Input: root = [10,5,15,3,7,13,18,1,null,6], L = 6, R = 10 Output: 23 Note: The number of nodes in the tree is at most 10000. The fi..

You have some apples, where arr[i] is the weight of the i-th apple. You also have a basket that can carry up to 5000 units of weight. Return the maximum number of apples you can put in the basket. Example 1: Input: arr = [100,200,150,1000] Output: 4 Explanation: All 4 apples can be carried by the basket since their sum of weights is 1450. Example 2: Input: arr = [900,950,800,1000,700,800] Output..

Given an array A of positive integers, let S be the sum of the digits of the minimal element of A. Return 0 if S is odd, otherwise return 1. Example 1: Input: [34,23,1,24,75,33,54,8] Output: 0 Explanation: The minimal element is 1, and the sum of those digits is S = 1 which is odd, so the answer is 0. Example 2: Input: [99,77,33,66,55] Output: 1 Explanation: The minimal element is 33, and the su..
The Wildcard Character The . (or dot) character in a regular expression is called a wildcard and will match any character except for a newline. For example, enter the following into the interactive shell: >>> atRegex = re.compile(r'.at') >>> atRegex.findall('The cat in the hat sat on the flat mat.') ['cat', 'hat', 'sat', 'lat', 'mat'] Remem..
Greedy and Nongreedy Matching Since (Ha){3,5} can match three, four, or five instances of Ha in the string 'HaHaHaHaHa', you may wonder why the Match object’s call to group() in the previous curly bracket example returns 'HaHaHaHaHa' instead of the shorter possibilities. After all, 'HaHaHa' and 'HaHaHaHa' are also valid matches of the regular expression (Ha){3,5}...
Introduction Regular expressions are huge time-savers, not just for software users but also for programmers. In fact, tech writer Cory Doctorow argues that even before teaching programming, we should be teaching regular expressions: “Knowing [regular expressions] can mean the difference between solving a problem in 3 steps and solving it in 3,000 steps. When you’re a nerd, you forget that the pr..
Python Basicsprint ('Alice' * 5) 'AliceAliceAliceAliceAlice'Flow Controlspam = ['cat', 'bat', 'rat', 'elephant'] spam[0:4] ['cat', 'bat', 'rat', 'elephant'] spam[1:3] ['bat', 'rat'] spam[0:-1] ['cat', 'bat', 'rat']Changing Values in a List with Indexesspam = ['cat', 'bat', 'rat', 'elephant'] spam[1] = 'aardvark' spam ['cat', 'aardvark', 'rat', 'elephant'] spam[2] = spam[1] spam ['cat', 'aardvark..