中级农民
- 积分
- 100
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2019-10-15
- 最后登录
- 1970-1-1
|
昨天前天
33 search inrotate array
这里肯定要利用sorted 事实, 然后binary search logn
怎么找pivot, 注意有可能没rotate的情况和只有一个元素的情况,
所以也是 binary 找更快, 总是偏左, 所以排除一个元素的特殊情况, 总有一个在mid 右边, mid》mid+1, pivot=mid+1;
不然 缩小空间 index,接着找, 直到找到就return, 不然没找到说明没有rotate,return=0(或者一开始最左边比最右边小, 一定没rotate,直接return0)
while(l《=r), 这样,一个元素也可以
接下来, pivot分成两个sorted list, 所以可以分别binary search, 直到找到,l《=r,
还有一个 直接target和最左, 最右比, 可以知道是哪个接着search。
答案one pass bs, mid 看是不是rotate,
Algorithm
Initiate start to be equal to 0, and end to be equal to n - 1.
Perform standard binary search. While start <= end:
Take an index in the middle mid as a pivot.
If nums[mid] == target, the job is done, return mid.
Now there could be two situations:
Pivot element is larger than the first element in the array, i.e. the part of array from the first element to the pivot one is non-rotated.
If the target is in that non-rotated part as well: go left: end = mid - 1.
Otherwise: go right: start = mid + 1.
Pivot element is smaller than the first element of the array, i.e. the rotation index is somewhere between 0 and mid. That means that the part of array from the pivot element to the last one is non-rotated.
If target is in that non-rotated part as well: go right: end = mid + 1.
Otherwise: go left: start = mid - 1.
We're here because the target is not found. Return -1.
380 get delete getrandom aveO(1)
肯定用到, java.util.Random ran=new Random();
这样, 删除一定不能有index 空位, 不然 random nexInt(), 不对,
所以删了的index 用last index填补, 挪位置 填空,
hashmap //val,loc get remove 都方便在hashmap, 然后list 存这样它通过index random 方便
因为虽然linkedlist arraylist 删除O(1), 但是你要找到v=8, 还是得search,
所以O(n), 除非你知道index,remove
或者dequearray 有removeLastOccurence(element) 不过不太好,时间
364 nested list sum II
简单, reverse, 所以巧妙利用数学depth(d+1) elementsum-正常顺序结果,
因为index,正反都是一样, 就是互为补充, 所以 反不方便就正
或者, bfs, 套着的解开,加到q 后面, 下一层的时候总是把虽有的pre 又加到total里,pre 不会重新为0,而累计了之前出现过的,
这样就保证,前面出现的加的层次多,d大
easy:
88 merge sorted list
直接一个一个比, 但是会有额外空间, 所以巧妙从后往前, 666, O(m)空间, 因为只要copy 被merge的, 然后就不用O(m+n) 空间
反向思想重要,'
O(m+n)
O(1) 空间
13 roman integer
hashmap 记录所有, IX 也是在里面H2, 这样先看两个, 那就i+=2, 不然i++,
注意index outofboundary, 所以
len==1,
不然len-2, 然后剩下一个 if(i>=c.length) return sum;
else return sum+=h.get(""+c[c.length-1]);
有可能不剩下,或者剩下, 判断就好
716 max stack
popmax, 所以肯定知道之前的max堆,这样pop一个max才能知道在下一个max
这里就是一个dp感觉, 然后maxstack, 记录到目前的最大值,
pop一个就也pop一个
不用index 什么的记录太复杂
339 nested list sum
简单, 直接recursive, 然后 NestedList, 是integer,直接return不然
List<NestedInteger> curls=ls.getList();
int sum=0;
for(NestedInteger childls:curls){
sum+=help(mylevel+1,childls);//elementSum,depth,
}
return sum;
就可以
53 maximum subarray
因为可能有-
因为不能 sum min 然后sum max, 因为还要保证sunmax 在summin 之后, 所以很麻烦,
这里 greddy, 因为有-;
过程中, global max ,这样就能one pass 所有potential sum,
如果这个sum before是+, 肯定sum=A【i】+sum前面的, 然后 global max
sum《0, 那肯定sun=A【i】, 这样重新开始的比累加跨的好, 所以 greedy
注意这里是sum 而不是元素, 因为可能
22-122. 跨可能更好
不然 肯定变小了
总结:
反向思想, 空间,
补充,填补位置思想
dp, 到目前遇到的最大值思想。
i++ i+2
if(特殊//一个) return
其他《= 找思想
没找到,最后return没找到
greedy, 更小, 下一个可能性, global比
还差3道medium 和之前的2easy 6 medium
2 easy 9 medim
|
|