起因
今天做了一道题,LeetCode 560. Subarray Sum Equals K,有点蒙蔽,看了答案发现比较简单,记录一下
给定一个整数数组和一个整数 k,你需要找到该数组中和为 k 的连续的子数组的个数。
1 | |
今天做了一道题,LeetCode 560. Subarray Sum Equals K,有点蒙蔽,看了答案发现比较简单,记录一下
给定一个整数数组和一个整数 k,你需要找到该数组中和为 k 的连续的子数组的个数。
1 | |
基本上位操作就那么几个:
1 | |
x 最右边的 n 位清零, x & ( ~0 << n )x 的第 n 位值(0 或者 1),(x >> n) & 1x 的第 n 位的幂值,x & (1 << (n - 1))n 位置为 1,x | (1 << n)n 位置为 0,x & (~(1 << n))x 最⾼位⾄第 n 位(含)清零,x & ((1 << n) - 1)n 位⾄第 0 位(含)清零,x & (~((1 << (n + 1)) - 1)X & 1 == 1 判断是否是奇数(偶数)X & = (X - 1) 将最低位(LSB)的 1 清零X & -X 得到最低位(LSB)的 1
X & ~X = 0
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
Example:
1 | |
Find K-th Smallest Pair Distance
Find K-th Smallest Pair Distance
Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pair (A, B) is defined as the absolute difference between A and B.
1 | |
1 | |
1 | |
Find the Duplicated Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.
Example 1:
1 | |
Example 2:
1 | |
Note:
You must not modify the array (assume the array is read only).
You must use only constant, O(1) extra space.
Your runtime complexity should be less than O(n2).
There is only one duplicate number in the array, but it could be repeated more than once.
Basic Calculator
Implement a basic calculator to evaluate a simple expression string.
The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .
Example 1:
1 | |
Example 2:
1 | |
Example 3:
1 | |
Note:
You may assume that the given expression is always valid.
Do not use the eval built-in library function.
Best Time To Buy Sell Stock IV
Best Time to Buy and Sell Stock IV
Say you have an array for which the i-th element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most k transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
Example 1:
1 | |
Example 2:
1 | |
(1,1)->(m,n)的不同路径中有大量的重复,比如(1,1)->(i,j)有 k 条不同的路径,那么对于任何一条固定的路线(i,j)->(m,n)的路径,都需要走 k 遍来模拟。(i,j)->(m,n)有 k 条不同的路径,那么(1,1)->(i,j)->(m,n)的不同路径总数是k*s(i,j)表示从(1,1)->(i,j)的不同路径数量,f(i,j) = f(i-1,j) + f(i,j-1)f(i,j) 只需要上一个结果即可, 也就是求解f(i,j) 需要求出子问题f(i',j')f(i,j),就不用关心如何计算出f(i,j)f(i,j),只要知道f(i-1,j)和f(i,j-1)f(i,j)的定义已经蕴含最优(min, max, sum)DP 适用的问题:可以将大问题拆成几个小问题,且无后效性,具有最优子结构的性质
Update your browser to view this website correctly.&npsb;Update my browser now