LeetCode 面试题57 - II(剑指offer) 和为s的连续正数序列
生活随笔
收集整理的這篇文章主要介紹了
LeetCode 面试题57 - II(剑指offer) 和为s的连续正数序列
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
今天畢業五年了,一直忙于工作和享受,自從當年找完工作后就一直沒有再刷過題,作為搬磚的碼農,覺得還是應該養成長期刷題的習慣堅持下去。之前堅持了每天被一會單詞,如今雅思一本也快看完了,從今天開始準備在保證每周一題的最低基礎上堅持下去,不為別的,主要是想多動腦,刷個幾十年堅持下去,應該可以預防老年癡呆。。。
題目:
?滑動窗口的解法:注意蛋疼的int[][]返回值類型,十分蛋疼
public class Solution {public int[][] FindContinuousSequence(int target){List<int[]> res = new List<int[]>();int leftIndex = 1;int rightIndex = 2;int loopNum = target/2 + 1;target *= 2; while(rightIndex <= loopNum){int sum = (leftIndex + rightIndex) * (rightIndex - leftIndex + 1);if(sum < target){rightIndex++;}else if(sum > target){leftIndex++;}else{List<int> seq = new List<int>();for(int i = 0; i < rightIndex - leftIndex + 1; i++){seq.Add(leftIndex + i);}res.Add(seq.ToArray());leftIndex++;rightIndex = leftIndex + 1;} }return res.ToArray();} }另外數學的求根法:?https://leetcode-cn.com/problems/he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof/solution/xiang-jie-hua-dong-chuang-kou-fa-qiu-gen-fa-jian-g/
總結
以上是生活随笔為你收集整理的LeetCode 面试题57 - II(剑指offer) 和为s的连续正数序列的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2018浦发银行跨行转账免手续费吗?这样
- 下一篇: 建行信用卡额度突然为0是怎么回事?这几个