├── README.md ├── cheung ├── ana.md ├── aws.md ├── cka.md ├── elasticsearch.md ├── ff.md ├── ff2.md ├── hokken.md ├── kubernetes.md ├── money.md ├── pint.md ├── wmf.md └── work_root.md ├── go ├── middle_of_the_linked_list.go ├── reverse_linked_list.go └── two_sum.go ├── java ├── 3Sum.java ├── 3SumClosest.java ├── 4Sum.java ├── AddandSearchWord.java ├── Backpack.java ├── Backpack2.java ├── Backpack3.java ├── Backpack4.java ├── Backpack5.java ├── Backpack6.java ├── BalancedBinaryTree.java ├── BestTimetoBuyandSellStock.java ├── BestTimetoBuyandSellStock2.java ├── BestTimetoBuyandSellStock3.java ├── BestTimetoBuyandSellStock4.java ├── BestTimetoBuyandSellStockwithCooldown.java ├── BestTimetoBuyandSellStockwithTransactionFee.java ├── BinaryTreeInorderTraversal.java ├── BinaryTreeLongestConsecutiveSequence.java ├── BinaryTreeLongestConsecutiveSequence2.java ├── BinaryTreeLongestConsecutiveSequence3.java ├── BinaryTreeMaximumPathSum.java ├── BinaryTreeMaximumPathSum2.java ├── BinaryTreePathSum.java ├── BinaryTreePathSum2.java ├── BinaryTreePathSum3.java ├── BinaryTreePostorderTraversal.java ├── BinaryTreePreorderTraversal.java ├── BinaryTreeSerialization.java ├── BinaryTreeZigzagLevelOrderTraversal.java ├── BoggleGame.java ├── BombEnemy.java ├── BuildPostOffice.java ├── BuildPostOffice2.java ├── BuildingOutline.java ├── BurstBalloons.java ├── ClimbingStairs.java ├── ClimbingStairs2.java ├── CloneGraph.java ├── CoinsinaLine.java ├── CoinsinaLine2.java ├── CoinsinaLine3.java ├── CombinationSum4.java ├── ConnectedComponentinUndirectedGraph.java ├── ConnectingGraph.java ├── ConnectingGraph2.java ├── ConnectingGraph3.java ├── ContinuousSubarraySum.java ├── ContinuousSubarraySum2.java ├── ConvertBinarySearchTreetoDoublyLinkedList.java ├── ConvertBinaryTreetoLinkedListsbyDepth.java ├── ConvertExpressiontoPolishNotation.java ├── ConvertExpressiontoReversePolishNotation.java ├── CopyBooks.java ├── CopyBooks2.java ├── CountofSmallerNumberBeforeItself.java ├── CourseSchedule.java ├── CourseSchedule2.java ├── DataStreamMedian.java ├── DistinctSubsequences.java ├── DivideTwoIntegers.java ├── DropEggs2.java ├── EditDistance.java ├── ExpressionEvaluation.java ├── ExpressionExpand.java ├── ExpressionTreeBuild.java ├── FastPower.java ├── FindMinimuminRotatedSortedArray2.java ├── FindPeakElement2.java ├── FindtheWeakConnectedComponentintheDirectedGraph.java ├── Flatten2DVector.java ├── FlattenBinaryTreetoLinkedList.java ├── FlattenList.java ├── FlattenNestedListIterator.java ├── FrogJump.java ├── GraphValidTree.java ├── HashFunction.java ├── Heapify.java ├── HighFive.java ├── HouseRobber.java ├── HouseRobber2.java ├── HouseRobber3.java ├── ImplementQueuebyTwoStacks.java ├── ImplementStackbyTwoQueues.java ├── ImplementTrie.java ├── InorderSuccessorinBinarySearchTree.java ├── InterleavingString.java ├── IntervalSum.java ├── IntervalSum2.java ├── JumpGame.java ├── JumpGame2.java ├── KClosestNumbersInSortedArray.java ├── KClosestPoints.java ├── KEditDistance.java ├── KnightShortestPath.java ├── KnightShortestPath2.java ├── KthLargestElement.java ├── KthLargestinNArrays.java ├── KthSmallestNumberInSortedMatrix.java ├── KthSmallestNumbersinUnsortedArray.java ├── KthSmallestSumInTwoSortedArrays.java ├── LFUCache.java ├── LRUCache.java ├── LargestDivisibleSubset.java ├── LargestRectangleinHistogram.java ├── LongestCommonSubsequence.java ├── LongestConsecutiveSequence.java ├── LongestIncreasingContinuousSubsequence.java ├── LongestIncreasingContinuousSubsequence2.java ├── LongestIncreasingSubsequence.java ├── LongestPalindromicSubstring.java ├── LongestRepeatingSubsequence.java ├── LongestSubstringWithAtMostKDistinctCharacters.java ├── LongestSubstringWithoutRepeatingCharacters.java ├── LowestCommonAncestor.java ├── LowestCommonAncestor2.java ├── LowestCommonAncestor3.java ├── MaxTree.java ├── MaximalRectangle.java ├── MaximalSquare.java ├── MaximalSquare2.java ├── MaximumAverageSubarray.java ├── MaximumAverageSubarray2.java ├── MaximumDepthofBinaryTree.java ├── MaximumGap.java ├── MaximumProductSubarray.java ├── MaximumSubarray.java ├── MaximumSubarray2.java ├── MaximumSubarray3.java ├── MaximumSubarray4.java ├── MaximumSubarray5.java ├── MaximumSubarrayDifference.java ├── MergeSortedArray.java ├── MergeTwoSortedArrays.java ├── MergekSortedArrays.java ├── MergekSortedLists.java ├── MiddleofLinkedList.java ├── MinStack.java ├── MinimumAdjustmentCost.java ├── MinimumDepthofBinaryTreewithMaximumAverage.java ├── MinimumPathSum.java ├── MinimumWindowSubstring.java ├── MoveZeroes.java ├── NestedListWeightSum.java ├── NestedListWeightSum2.java ├── NextPermutation.java ├── NextPermutation2.java ├── NumberOfIslands.java ├── NumberOfIslands2.java ├── NumberofAirplanesintheSky.java ├── PalindromePartitioning2.java ├── PancakeSorting.java ├── PartitionList.java ├── PerfectSquares.java ├── PermutationIndex.java ├── PermutationIndex2.java ├── PostOfficeProblem.java ├── Pow.java ├── PreviousPermutation.java ├── PreviousPermutation2.java ├── Rehashing.java ├── RemoveDuplicatesfromSortedArray.java ├── RemoveSubstrings.java ├── RussianDollEnvelopes.java ├── ScrambleString.java ├── SearchGraphNodes.java ├── Searcha2DMatrix2.java ├── SearchforaRange.java ├── SearchinRotatedSortedArray2.java ├── SequenceReconstruction.java ├── SixDegrees.java ├── SlidingWindowMatrixMaximum.java ├── SlidingWindowMaximum.java ├── SlidingWindowMedian.java ├── SmallestRectangleEnclosingBlackPixels.java ├── SortColors2.java ├── SortIntegers.java ├── SortIntegers2.java ├── Sqrt.java ├── Sqrt2.java ├── StoneGame.java ├── StoneGame2.java ├── StringPermutation.java ├── StringPermutation2.java ├── SubarraySum.java ├── SubarraySum2.java ├── SubarraySumClosest.java ├── SubmatrixSum.java ├── SurroundedRegions.java ├── TheSkylineProblem.java ├── TopKFrequentWords.java ├── TopKFrequentWords2.java ├── TopKFrequentWordsMapReduce.java ├── TopkLargestNumbers.java ├── TopkLargestNumbers2.java ├── TopologicalSorting.java ├── TrappingRainWater.java ├── TrappingRainWater2.java ├── Triangle.java ├── TriangleCount.java ├── TwoSum-Differenceequalstotarget.java ├── TwoSum-Greaterthantarget.java ├── TwoSum-Inputarrayissorted.java ├── TwoSum.java ├── TwoSum3-DataStructureDesign.java ├── UglyNumber.java ├── UglyNumber2.java ├── UniquePaths.java ├── UniquePaths2.java ├── ValidateBinarySearchTree.java ├── WindowSum.java ├── WoodCut.java ├── WordBreak.java ├── WordBreak2.java ├── WordSearch.java ├── WordSearch2.java ├── WordSquares.java ├── ZigzagIterator.java ├── ZigzagIterator2.java ├── ZombieinMatrix.java ├── addTwoNumbers.java ├── binaryTreeLevelOrderTraversal.java ├── binaryTreeLevelOrderTraversal2.java ├── binaryTreePaths.java ├── closetNumberInSortedArray.java ├── combinationSum.java ├── combinationSum2.java ├── combinations.java ├── copyListwithRandomPointer.java ├── dropEggs.java ├── findMinimumInRotatedSortedArray.java ├── findPeakElement.java ├── findTheDuplicateNumber.java ├── firstBadVersion.java ├── firstPositionOfTarget.java ├── insertInToACyclicSortedList.java ├── intersectionOfTwoArrays.java ├── intersectionOfTwoArrays2.java ├── kSum.java ├── kSum2.java ├── lastPositionofTarget.java ├── linkedListCycle.java ├── linkedListCycle2.java ├── longestPalindrome.java ├── maximumNumberInMountainSequence.java ├── medianOfTwoSortedArrays.java ├── mergeTwoSortedLists.java ├── minimumDepthOfBinaryTree.java ├── minimumSizeSubarraySum.java ├── minimumSubtree.java ├── palindromePartitioning.java ├── partitionArray.java ├── partitionArray2.java ├── permutations.java ├── permutations2.java ├── removeDuplicateNumbersInArray.java ├── reverseNodesInk-Group.java ├── searchInABigSortedArray.java ├── searchInRotatedSortedArray.java ├── searcha2DMatrix.java ├── sortColors.java ├── sortList.java ├── strStr.java ├── strStr2.java ├── subsets.java ├── subsets2.java ├── subtreeWithMaximumAverage.java ├── totalOccurrenceOfTarget.java ├── twoSum-ClosestToTarget.java ├── twoSum-LessThanOrEqualToTarget.java ├── twoSum-UniquePairs.java ├── twoSumDataStructureDesign.java ├── validPalindrome.java ├── validPalindrome2.java ├── wordLadder.java └── wordLadder2.java ├── menu.md ├── ruby ├── longest_palindrome.rb ├── merge_two_sorted_lists.rb ├── reverseVowelsOfAString.rb ├── str_str.rb ├── trie_tree.rb ├── two_sum.rb └── valid_palindrome.rb ├── tmp ├── 12311566973056.jpg ├── 12321566973057.jpg ├── 12331566973058.jpg ├── 12341566973059.jpg ├── 12351566973060.jpg ├── 12361566973061.jpg ├── 12371566973062.jpg ├── 12391566973651.jpg ├── 12401566973652.jpg ├── 12411566973653.jpg ├── 12451566973663.jpg ├── 12461566973666.jpg ├── 12471566973668.jpg ├── 13401569471602.jpg ├── 13421569471605.jpg ├── 13431569471606.jpg ├── 13441569471620.jpg ├── 13451569471623.jpg ├── 13471569471629.jpg ├── 13481569471639.jpg ├── 150740.jpg ├── 150759.jpg ├── 150807.jpg ├── 150835.jpg ├── 150847.jpg ├── 20190221001.jpg ├── 20190221002.jpg ├── 20190221003.jpg ├── 20190221004.jpg ├── 20190221005.jpg ├── 20190221006.jpg ├── 20190221007.jpg ├── 20190221008.jpg ├── 20190221009.jpg ├── 20190221010.jpg ├── 20190221011.jpg ├── 20190221012.jpg ├── 20190301_203532.jpg ├── 20190302_141115.jpg ├── 20190303_115043.jpg ├── 20190303_121425.jpg ├── 20190303_135842.jpg ├── 20190303_153546.jpg ├── 20190303_164212.jpg ├── 441567669203.jpg ├── 5721550414890.jpg ├── 5731550414891.jpg ├── 5741550414892.jpg ├── 5751550414893.jpg ├── 5761550414894_.pic_hd.jpg ├── 5771550414895.jpg ├── 5791550414897_.pic_hd.jpg ├── 5801550414898.jpg ├── 5821550414927.jpg ├── 5831550414929_.pic_hd.jpg ├── 5841550415359.jpg ├── 5871550417469.jpg ├── 6211551016325.jpg ├── 6231551437558.jpg ├── 6241551437561.jpg ├── 6711553250282.jpg ├── 6721553250719.jpg ├── 6751553250722.jpg ├── 6761553250723.jpg ├── 6771553250724.jpg ├── 6781553250725.jpg ├── 6791553250726.jpg ├── 6811553250728.jpg ├── 6821553250931.jpg ├── 6831553251677.jpg ├── 6861553251681.jpg ├── 6871553251682.jpg ├── 6881553251683.jpg ├── 6891553251683.jpg ├── 6901553251684.jpg ├── 6911553251685.jpg ├── 6921553251686.jpg ├── 6931553252064.jpg ├── 6941553252065.jpg ├── 6951553252065.jpg ├── 6961553252066.jpg ├── 6971553252067.jpg ├── 7681554555106.jpg ├── 7691554555107.jpg ├── 7711554678212.jpg ├── 7721554678216.jpg ├── 7731554678217.jpg ├── 7771554678221.jpg ├── 7781554678222.jpg ├── 7791554678254.jpg ├── 7811554678256.jpg ├── 7851554678260.jpg ├── 7861554678261.jpg └── a1f7f7f4da19b06d929fbc05ba225459.png └── travel ├── amami_20190830.md ├── hawaii_20191010.md ├── iceland_20191227.md ├── ishigaki_20190405.md ├── israel_2017.md ├── kending_20190711.md ├── kozu_20190920.md ├── monbetsu_20190221.md ├── nagazaki_20180603.md ├── okinawa_20190119.md ├── okinawa_20190301.md ├── south_african_2018.md └── wuhan_20181003.md /cheung/pint.md: -------------------------------------------------------------------------------- 1 | # 雀巢薅羊毛攻略 2 | 大家好,去年给大家介绍的里程薅到手了嘛,
3 | 反正莘哥是换了15万里程,
4 | 换了年底回上海的往返商务舱(3w里程,花钱买大概十几万吧)
5 | 6 | 冬天到了,各大厂商开始消化预算,所以又到了薅羊毛的季节了,
7 | 这次还是雀巢,虽然去年的咖啡还没喝完,洗衣粉洗手液还有好几瓶。。。
8 | 9 | 适合人群: 热爱薅资本主义羊毛群众
10 | 不适合人群: 土豪,时间成本高的大佬
11 | 12 | 这次的福利:花2万多日币换3万里程和8万价值的商品
13 | 14 | 关于定期便,需要接收4次之后才能取消,
15 | 第一次定商品不需要付钱,上限是65000日元,
16 | 只有第二次继续续订的商品才需要付钱,
17 | 所以最好的方法就是第一次尽量把能定的商品都下单,
18 | 等第一次商品到货后再调整定期便里的商品,把价格控制在10000内,
19 | 第三次第四次类推,
20 | 最好你所支付的价格就是第二次第三次第四次续订的商品和最后一次商品的价格
21 | 10000 + 10000 + 1000 + 1000 = 22000
22 | 定的商品在2000一下会发生邮费,所以实际还会高出一些。
23 | 下单的时候记得填好信用卡,不然最终成功了也会变成被取消的状态,
24 | 如果出货状态变成キャンセル的话就需要联系客服人工删除定期便,
25 | 然后再删除账号再重新注册一遍,所以尽量不要出错。
26 | 27 | 不同的名字可以注册不同的雀巢账号,
28 | 即使住址相同也可以再刷一遍,
29 | 大家可以多刷几次,反正需要的手续都差不多,
30 | 31 | 收到商品之后可以更改第二次的商品,
32 | 把不需要的商品都删除,
33 | 第二次还可以追加其它品类的商品,
34 | 比如洗发液和剃须刀都是不错的选择
35 | 36 | 所能收到的是,积分网站的400000(40000日元相当)的积分,
37 | 和价值8万的商品,如果自己不用的话可以试着在メルカリ等网站出售,
38 | 基本上是只有挣没有亏的活动。
39 | 40 | 积分可以换成line point当现金用,或者亚马逊点卷等
41 | 看个人偏好,个人推荐换里程,
42 | 43 | 首先需要注册积分网站,
44 | 下面是注册链接
45 | [点击注册](https://pointi.jp/p/?a=rbf880550022)
46 | 47 | 之后通过积分网站的活动界面跳转雀巢的网站就可以获得4万日币的积分
48 | [活动界面](https://pointi.jp/ad/132150/)
49 | 第一次下单的商品只有第二次续订的需要付费,上限65000,
50 | 第二次需要下单10000的商品,所以65000 - 10000 = 55000日元份的商品不需要付费
51 | 第三次第四次类推,第四次商品到后记得电话退订定期便,
52 | 需要1年的时间,大家记得写进日历
53 | 54 | 积分大概3个月后到账,可以通过积分网站point income的通帐来查看
55 | 56 | 如何把积分换成里程参考另外一篇[里程攻略](https://github.com/cheungYX/algorithm/blob/master/cheung/ana.md) 57 | -------------------------------------------------------------------------------- /go/middle_of_the_linked_list.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * type ListNode struct { 4 | * Val int 5 | * Next *ListNode 6 | * } 7 | */ 8 | func middleNode(head *ListNode) *ListNode { 9 | if head.Next == nil { 10 | return head 11 | } 12 | 13 | faster := head 14 | slow := head 15 | 16 | for { 17 | if faster == nil || faster.Next == nil { 18 | break 19 | } 20 | slow = slow.Next 21 | faster = faster.Next.Next 22 | } 23 | return slow 24 | } 25 | -------------------------------------------------------------------------------- /go/reverse_linked_list.go: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * type ListNode struct { 4 | * Val int 5 | * Next *ListNode 6 | * } 7 | */ 8 | 9 | 10 | func reverseList(head *ListNode) *ListNode { 11 | var pre *ListNode 12 | 13 | for { 14 | if head == nil { 15 | break 16 | } 17 | tmp := head 18 | head = head.Next 19 | tmp.Next = pre 20 | pre = tmp 21 | } 22 | return pre 23 | } 24 | -------------------------------------------------------------------------------- /go/two_sum.go: -------------------------------------------------------------------------------- 1 | func twoSum(nums []int, target int) []int { 2 | // 方法1: 暴力破解 o(n2) 3 | for i := range nums { 4 | for j := i + 1; j < len(nums); j++ { 5 | if nums[i] + nums[j] == target { 6 | return []int{i, j} 7 | } 8 | } 9 | } 10 | return nil 11 | } 12 | 13 | func twoSum2(nums []int, target int) []int { 14 | // 方法2: 哈希表 用空间换时间 15 | m := map[int]int{} 16 | 17 | for i, v := range nums { 18 | // 不能直接取 m[v] != nil 19 | if idx, ok := m[v]; ok { 20 | return []int{idx, i} 21 | } 22 | m[target - v] = i 23 | } 24 | 25 | return nil 26 | } -------------------------------------------------------------------------------- /java/3SumClosest.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | /* 4 | * @param numbers: Give an array numbers of n integer 5 | * @param target: An integer 6 | * @return: return the sum of the three integers, the sum closest target. 7 | */ 8 | int threeSumClosest(vector &numbers, int target) { 9 | // write your code here 10 | sort(numbers.begin(), numbers.end()); 11 | int size = numbers.size(); 12 | int result = numbers[0] + numbers[1] + numbers[size-1]; 13 | int temp = abs(target - result); 14 | 15 | for (int i = 0; i < size - 2; i++) 16 | { 17 | if (i >=1 && numbers[i - 1] == numbers[i]) 18 | { 19 | continue; 20 | } 21 | int start = i + 1, end = size - 1; 22 | while (start < end) 23 | { 24 | if (start > i + 1 && numbers[start] == numbers[start - 1]) 25 | { 26 | start++; 27 | continue; 28 | } 29 | int value = numbers[i] + numbers[start] + numbers[end]; 30 | if (value== target) 31 | { 32 | return target; 33 | } 34 | else if (value< target) 35 | { 36 | start++; 37 | if (abs(value- target) < temp) 38 | { 39 | result = value; 40 | temp = abs(value - target); 41 | } 42 | } 43 | else if (value > target) 44 | { 45 | 46 | end--; 47 | if (abs(value - target) < temp) 48 | { 49 | result = value; 50 | temp = abs(value - target); 51 | } 52 | } 53 | } 54 | } 55 | return result; 56 | } 57 | }; -------------------------------------------------------------------------------- /java/4Sum.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public List> fourSum(int[] num, int target) { 3 | List> rst = new ArrayList>(); 4 | Arrays.sort(num); 5 | 6 | for (int i = 0; i < num.length - 3; i++) { 7 | if (i != 0 && num[i] == num[i - 1]) { 8 | continue; 9 | } 10 | 11 | for (int j = i + 1; j < num.length - 2; j++) { 12 | if (j != i + 1 && num[j] == num[j - 1]) 13 | continue; 14 | 15 | int left = j + 1; 16 | int right = num.length - 1; 17 | while (left < right) { 18 | int sum = num[i] + num[j] + num[left] + num[right]; 19 | if (sum < target) { 20 | left++; 21 | } else if (sum > target) { 22 | right--; 23 | } else { 24 | ArrayList tmp = new ArrayList(); 25 | tmp.add(num[i]); 26 | tmp.add(num[j]); 27 | tmp.add(num[left]); 28 | tmp.add(num[right]); 29 | rst.add(tmp); 30 | left++; 31 | right--; 32 | while (left < right && num[left] == num[left - 1]) { 33 | left++; 34 | } 35 | while (left < right && num[right] == num[right + 1]) { 36 | right--; 37 | } 38 | } 39 | } 40 | } 41 | } 42 | 43 | return rst; 44 | } 45 | } -------------------------------------------------------------------------------- /java/Backpack.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param m: An integer m denotes the size of a backpack 4 | * @param A: Given n items with size A[i] 5 | * @return: The maximum size 6 | */ 7 | public int backPack(int m, int[] A) { 8 | boolean f[][] = new boolean[A.length + 1][m + 1]; 9 | for (int i = 0; i <= A.length; i++) { 10 | for (int j = 0; j <= m; j++) { 11 | f[i][j] = false; 12 | } 13 | } 14 | f[0][0] = true; 15 | for (int i = 1; i <= A.length; i++) { 16 | for (int j = 0; j <= m; j++) { 17 | f[i][j] = f[i - 1][j]; 18 | if (j >= A[i-1] && f[i-1][j - A[i-1]]) { 19 | f[i][j] = true; 20 | } 21 | } // for j 22 | } // for i 23 | 24 | for (int i = m; i >= 0; i--) { 25 | if (f[A.length][i]) { 26 | return i; 27 | } 28 | } 29 | 30 | return 0; 31 | } 32 | } 33 | 34 | 35 | // O(m) 空间复杂度的解法 36 | public class Solution { 37 | /** 38 | * @param m: An integer m denotes the size of a backpack 39 | * @param A: Given n items with size A[i] 40 | * @return: The maximum size 41 | */ 42 | public int backPack(int m, int[] A) { 43 | int f[] = new int[m + 1]; 44 | 45 | for (int i = 0; i < A.length; i++) { 46 | for (int j = m; j >= A[i]; j--) { 47 | f[j] = Math.max(f[j], f[j - A[i]] + A[i]); 48 | } 49 | } 50 | return f[m]; 51 | } 52 | } -------------------------------------------------------------------------------- /java/Backpack2.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param m: An integer m denotes the size of a backpack 4 | * @param A & V: Given n items with size A[i] and value V[i] 5 | * @return: The maximum value 6 | */ 7 | 8 | public int backPackII(int m, int[] A, int V[]) { 9 | // write your code here 10 | int[][] dp = new int[A.length + 1][m + 1]; 11 | for(int i = 0; i <= A.length; i++){ 12 | for(int j = 0; j <= m; j++){ 13 | if(i == 0 || j == 0){ 14 | dp[i][j] = 0; 15 | } 16 | else if(A[i-1] > j){ 17 | dp[i][j] = dp[(i-1)][j]; 18 | } 19 | else{ 20 | dp[i][j] = Math.max(dp[(i-1)][j], dp[(i-1)][j-A[i-1]] + V[i-1]); 21 | } 22 | } 23 | } 24 | return dp[A.length][m]; 25 | } 26 | } 27 | 28 | // 方法二 29 | public class Solution { 30 | /** 31 | * @param m: An integer m denotes the size of a backpack 32 | * @param A & V: Given n items with size A[i] and value V[i] 33 | */ 34 | public int backPackII(int m, int[] A, int V[]) { 35 | // write your code here 36 | int[] f = new int[m+1]; 37 | for (int i = 0; i <=m ; ++i) f[i] = 0; 38 | int n = A.length , i, j; 39 | for(i = 0; i < n; i++){ 40 | for(j = m; j >= A[i]; j--){ 41 | if (f[j] < f[j - A[i]] + V[i]) 42 | f[j] = f[j - A[i]] + V[i]; 43 | } 44 | } 45 | return f[m]; 46 | } 47 | } -------------------------------------------------------------------------------- /java/Backpack3.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param A an integer array 4 | * @param V an integer array 5 | * @param m an integer 6 | * @return an array 7 | */ 8 | public int backPackIII(int[] A, int[] V, int m) { 9 | // Write your code here 10 | int n = A.length; 11 | int[] f = new int[m+1]; 12 | for (int i = 0; i < n; ++i) 13 | for (int j = A[i]; j <= m; ++j) 14 | if (f[j - A[i]] + V[i] > f[j]) 15 | f[j] = f[j - A[i]] + V[i]; 16 | return f[m]; 17 | } 18 | } 19 | 20 | // 2D version, 如果你无法理解一维的solution, 可以从二维的solution入手,然后思考空间的优化 21 | public class Solution { 22 | /** 23 | * @param A an integer array 24 | * @param V an integer array 25 | * @param m an integer 26 | * @return an array 27 | */ 28 | public int backPackIII(int[] A, int[] V, int m) { 29 | // Write your code here 30 | int n = A.length; 31 | int[][] f = new int[n + 1][m + 1]; 32 | for (int i = 1; i <= n; ++i) 33 | for (int j = 0; j <= m; ++j) { 34 | f[i][j] = f[i - 1][j]; 35 | if (j >= A[i - 1]) 36 | f[i][j] = Math.max(f[i][j - A[i - 1]] + V[i - 1], f[i][j]); 37 | } 38 | return f[n][m]; 39 | } 40 | } -------------------------------------------------------------------------------- /java/Backpack4.java: -------------------------------------------------------------------------------- 1 | // 方法一 2 | public class Solution { 3 | /** 4 | * @param nums an integer array and all positive numbers, no duplicates 5 | * @param target an integer 6 | * @return an integer 7 | */ 8 | public int backPackIV(int[] nums, int target) { 9 | // Write your code here 10 | int m = target; 11 | int []A = nums; 12 | int f[][] = new int[A.length + 1][m + 1]; 13 | 14 | f[0][0] = 1; 15 | for (int i = 1; i <= A.length; i++) { 16 | for (int j = 0; j <= m; j++) { 17 | int k = 0; 18 | while(k * A[i-1] <= j) { 19 | f[i][j] += f[i-1][j-A[i-1]*k]; 20 | k+=1; 21 | } 22 | } // for j 23 | } // for i 24 | return f[A.length][target]; 25 | } 26 | } 27 | 28 | 29 | // 方法二 30 | 31 | public class Solution { 32 | /** 33 | * @param nums an integer array and all positive numbers, no duplicates 34 | * @param target an integer 35 | * @return an integer 36 | */ 37 | public int backPackIV(int[] nums, int target) { 38 | // Write your code here 39 | int[] f = new int[target + 1]; 40 | f[0] = 1; 41 | for (int i = 0; i < nums.length; ++i) 42 | for (int j = nums[i]; j <= target; ++j) 43 | f[j] += f[j - nums[i]]; 44 | 45 | return f[target]; 46 | } 47 | } -------------------------------------------------------------------------------- /java/Backpack5.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param nums an integer array and all positive numbers 4 | * @param target an integer 5 | * @return an integer 6 | */ 7 | public int backPackV(int[] nums, int target) { 8 | // Write your code here 9 | int[] f = new int[target + 1]; 10 | f[0] = 1; 11 | for (int i = 0; i < nums.length; ++i) 12 | for (int j = target; j >= nums[i]; --j) 13 | f[j] += f[j - nums[i]]; 14 | 15 | return f[target]; 16 | } 17 | } -------------------------------------------------------------------------------- /java/Backpack6.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param nums an integer array and all positive numbers, no duplicates 4 | * @param target an integer 5 | * @return an integer 6 | */ 7 | public int backPackVI(int[] nums, int target) { 8 | // Write your code here 9 | int[] f = new int[target + 1]; 10 | f[0] = 1; 11 | for (int i = 1; i <= target; ++i) 12 | for (int j = 0; j < nums.length; ++j) 13 | if (i >= nums[j]) 14 | f[i] += f[i - nums[j]]; 15 | 16 | return f[target]; 17 | } 18 | } -------------------------------------------------------------------------------- /java/BestTimetoBuyandSellStock.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int maxProfit(int[] prices) { 3 | if (prices == null || prices.length == 0) { 4 | return 0; 5 | } 6 | 7 | int min = Integer.MAX_VALUE; //just remember the smallest price 8 | int profit = 0; 9 | for (int i : prices) { 10 | min = i < min ? i : min; 11 | profit = (i - min) > profit ? i - min : profit; 12 | } 13 | 14 | return profit; 15 | } 16 | } -------------------------------------------------------------------------------- /java/BestTimetoBuyandSellStock2.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int maxProfit(int[] prices) { 3 | int profit = 0; 4 | for (int i = 0; i < prices.length - 1; i++) { 5 | int diff = prices[i+1] - prices[i]; 6 | if (diff > 0) { 7 | profit += diff; 8 | } 9 | } 10 | return profit; 11 | } 12 | } -------------------------------------------------------------------------------- /java/BestTimetoBuyandSellStockwithCooldown.java: -------------------------------------------------------------------------------- 1 | int maxProfit(vector& prices) { 2 | int sold = 0, rest = 0, hold = INT_MIN; 3 | for (const int p : prices) { 4 | int prev_sold = sold; 5 | sold = hold + p; 6 | hold = max(hold, rest - p); 7 | rest = max(rest, prev_sold); 8 | } 9 | return max(rest, sold); 10 | } -------------------------------------------------------------------------------- /java/BestTimetoBuyandSellStockwithTransactionFee.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProfit(int[] prices, int fee) { 3 | int sell=0; 4 | int own=-prices[0]; 5 | for (int i = 1; i < prices.length; i++){ 6 | sell = Math.max(sell, own + prices[i] - fee); 7 | own = Math.max(own, sell - prices[i]); 8 | } 9 | return sell; 10 | } 11 | } -------------------------------------------------------------------------------- /java/BinaryTreeInorderTraversal.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param root: The root of binary tree. 4 | * @return: Inorder in ArrayList which contains node values. 5 | */ 6 | public ArrayList inorderTraversal(TreeNode root) { 7 | Stack stack = new Stack<>(); 8 | ArrayList result = new ArrayList<>(); 9 | 10 | while (root != null) { 11 | stack.push(root); 12 | root = root.left; 13 | } 14 | 15 | while (!stack.isEmpty()) { 16 | TreeNode node = stack.peek(); 17 | result.add(node.val); 18 | 19 | if (node.right == null) { 20 | node = stack.pop(); 21 | while (!stack.isEmpty() && stack.peek().right == node) { 22 | node = stack.pop(); 23 | } 24 | } else { 25 | node = node.right; 26 | while (node != null) { 27 | stack.push(node); 28 | node = node.left; 29 | } 30 | } 31 | } 32 | return result; 33 | } 34 | } -------------------------------------------------------------------------------- /java/BinaryTreeLongestConsecutiveSequence2.java: -------------------------------------------------------------------------------- 1 | class ResultType { 2 | public int max_length, max_down, max_up; 3 | ResultType(int len, int down, int up) { 4 | max_length = len; 5 | max_down = down; 6 | max_up = up; 7 | } 8 | } 9 | 10 | public class Solution { 11 | /** 12 | * @param root the root of binary tree 13 | * @return the length of the longest consecutive sequence path 14 | */ 15 | public int longestConsecutive2(TreeNode root) { 16 | // Write your code here 17 | return helper(root).max_length; 18 | } 19 | 20 | ResultType helper(TreeNode root) { 21 | if (root == null) { 22 | return new ResultType(0, 0, 0); 23 | } 24 | 25 | ResultType left = helper(root.left); 26 | ResultType right = helper(root.right); 27 | 28 | int down = 0, up = 0; 29 | if (root.left != null && root.left.val + 1 == root.val) 30 | down = Math.max(down, left.max_down + 1); 31 | 32 | if (root.left != null && root.left.val - 1 == root.val) 33 | up = Math.max(up, left.max_up + 1); 34 | 35 | if (root.right != null && root.right.val + 1 == root.val) 36 | down = Math.max(down, right.max_down + 1); 37 | 38 | if (root.right != null && root.right.val - 1 == root.val) 39 | up = Math.max(up, right.max_up + 1); 40 | 41 | int len = down + 1 + up; 42 | len = Math.max(len, Math.max(left.max_length, right.max_length)); 43 | 44 | return new ResultType(len, down, up); 45 | } 46 | } -------------------------------------------------------------------------------- /java/BinaryTreeLongestConsecutiveSequence3.java: -------------------------------------------------------------------------------- 1 | class ResultType { 2 | public int max_len, max_down, max_up; 3 | ResultType(int len, int down, int up) { 4 | max_len = len; 5 | max_down = down; 6 | max_up = up; 7 | } 8 | } 9 | 10 | public class Solution { 11 | /** 12 | * @param root the root of k-ary tree 13 | * @return the length of the longest consecutive sequence path 14 | */ 15 | public int longestConsecutive3(MultiTreeNode root) { 16 | // Write your code here 17 | return helper(root).max_len; 18 | } 19 | 20 | ResultType helper(MultiTreeNode root) { 21 | if (root == null) { 22 | return new ResultType(0, 0, 0); 23 | } 24 | 25 | int down = 0, up = 0, max_len = 1; 26 | for (MultiTreeNode node : root.children) { 27 | ResultType type = helper(node); 28 | if (node.val + 1 == root.val) 29 | down = Math.max(down, type.max_down + 1); 30 | if (node.val - 1 == root.val) 31 | up = Math.max(up, type.max_up + 1); 32 | max_len = Math.max(max_len, type.max_len); 33 | } 34 | 35 | max_len = Math.max(down + 1 + up, max_len); 36 | return new ResultType(max_len, down, up); 37 | } 38 | } -------------------------------------------------------------------------------- /java/BinaryTreeMaximumPathSum2.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param root the root of binary tree. 4 | * @return an integer 5 | */ 6 | public int maxPathSum2(TreeNode root) { 7 | if (root == null) { 8 | return Integer.MIN_VALUE; 9 | } 10 | 11 | int left = maxPathSum2(root.left); 12 | int right = maxPathSum2(root.right); 13 | return root.val + Math.max(0, Math.max(left, right)); 14 | } 15 | } -------------------------------------------------------------------------------- /java/BinaryTreePathSum2.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param root the root of binary tree. 4 | * @return an integer 5 | */ 6 | public int maxPathSum2(TreeNode root) { 7 | if (root == null) { 8 | return Integer.MIN_VALUE; 9 | } 10 | 11 | int left = maxPathSum2(root.left); 12 | int right = maxPathSum2(root.right); 13 | return root.val + Math.max(0, Math.max(left, right)); 14 | } 15 | } -------------------------------------------------------------------------------- /java/BinaryTreePostorderTraversal.java: -------------------------------------------------------------------------------- 1 | //Recursive 2 | public ArrayList postorderTraversal(TreeNode root) { 3 | ArrayList result = new ArrayList(); 4 | 5 | if (root == null) { 6 | return result; 7 | } 8 | 9 | result.addAll(postorderTraversal(root.left)); 10 | result.addAll(postorderTraversal(root.right)); 11 | result.add(root.val); 12 | 13 | return result; 14 | } 15 | 16 | //Iterative 17 | public ArrayList postorderTraversal(TreeNode root) { 18 | ArrayList result = new ArrayList(); 19 | Stack stack = new Stack(); 20 | TreeNode prev = null; // previously traversed node 21 | TreeNode curr = root; 22 | 23 | if (root == null) { 24 | return result; 25 | } 26 | 27 | stack.push(root); 28 | while (!stack.empty()) { 29 | curr = stack.peek(); 30 | if (prev == null || prev.left == curr || prev.right == curr) { // traverse down the tree 31 | if (curr.left != null) { 32 | stack.push(curr.left); 33 | } else if (curr.right != null) { 34 | stack.push(curr.right); 35 | } 36 | } else if (curr.left == prev) { // traverse up the tree from the left 37 | if (curr.right != null) { 38 | stack.push(curr.right); 39 | } 40 | } else { // traverse up the tree from the right 41 | result.add(curr.val); 42 | stack.pop(); 43 | } 44 | prev = curr; 45 | } 46 | 47 | return result; 48 | } -------------------------------------------------------------------------------- /java/BurstBalloons.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /* 3 | * @param nums: A list of integer 4 | * @return: An integer, maximum coins 5 | */ 6 | public int maxCoins(int[] nums) { 7 | if(nums == null || nums.length == 0) return 0; 8 | int n = nums.length; 9 | //开数组到n+2是为了保证k-1 k+1不溢出 10 | int[][] dp = new int[n+2][n+2]; 11 | for(int i=1;i<=n;i++){ 12 | int left = i-2 >= 0?nums[i-2]:1; 13 | int right = i < n?nums[i]:1; 14 | dp[i][i] = left*nums[i-1]*right; 15 | } 16 | for(int len = 2;len<=n;len++){ 17 | for(int i=1;i+len-1<=n;i++){ 18 | int j = i+len-1; 19 | int left = i-2 >= 0?nums[i-2]:1; 20 | int right = j < n?nums[j]:1; 21 | dp[i][j] = Integer.MIN_VALUE; 22 | for(int k=i;k<=j;k++){ 23 | dp[i][j] = Math.max(dp[i][j], dp[i][k-1]+dp[k+1][j]+left*right*nums[k-1]); 24 | } 25 | } 26 | } 27 | return dp[1][n]; 28 | } 29 | } -------------------------------------------------------------------------------- /java/ClimbingStairs.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int climbStairs(int n) { 3 | if (n <= 1) { 4 | return n; 5 | } 6 | int last = 1, lastlast = 1; 7 | int now = 0; 8 | for (int i = 2; i <= n; i++) { 9 | now = last + lastlast; 10 | lastlast = last; 11 | last = now; 12 | } 13 | return now; 14 | } 15 | } 16 | 17 | // v2 记忆化搜索 18 | public class Solution { 19 | /** 20 | * @param n: An integer 21 | * @return: An integer 22 | */ 23 | int[] result = null; 24 | 25 | void f(int X) { 26 | if (result[X] != -1) return; 27 | if (X == 0 || X == 1) { 28 | result[X] = 1; 29 | return; 30 | } 31 | 32 | f(X - 1); 33 | f(X - 2); 34 | result[X] = result[X - 1] + result[X - 2]; 35 | } 36 | 37 | public int climbStairs(int n) { 38 | if (n == 0) { 39 | return 0; 40 | } 41 | 42 | result = new int[n + 1]; 43 | for (int i = 0; i <= n; ++i) { 44 | result[i] = -1; 45 | } 46 | 47 | f(n); 48 | return result[n]; 49 | } 50 | } -------------------------------------------------------------------------------- /java/ClimbingStairs2.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param n an integer 4 | * @return an integer 5 | */ 6 | public int climbStairs2(int n) { 7 | int[] f = new int[n+1]; 8 | f[0] = 1; 9 | for (int i = 0; i <= n; i++) 10 | for (int j = 1; j <= 3; j++) { 11 | if (i >= j) { 12 | f[i] += f[i-j]; 13 | } 14 | } 15 | return f[n]; 16 | } 17 | } -------------------------------------------------------------------------------- /java/CoinsinaLine.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/java/CoinsinaLine.java -------------------------------------------------------------------------------- /java/CombinationSum4.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int combinationSum4(vector& nums, int target) { 4 | int n = nums.size(); 5 | int f[target+1]; 6 | memset(f, 0, sizeof(f)); 7 | 8 | f[0] = 1; 9 | for (int i = 1; i <= target; i++) { 10 | for (int j = 0; j < n; j++) 11 | if (i - nums[j] >= 0){ 12 | f[i] += f[i-nums[j]]; 13 | } 14 | } 15 | return f[target]; 16 | } 17 | }; -------------------------------------------------------------------------------- /java/ConnectingGraph.java: -------------------------------------------------------------------------------- 1 | public class ConnectingGraph { 2 | /* 3 | * @param n: An integer 4 | */ 5 | private int[] father = null; 6 | private int find(int x) { 7 | if (father[x] == x) { 8 | return x; 9 | } 10 | return father[x] = find(father[x]); 11 | } 12 | public ConnectingGraph(int n) { 13 | // do intialization if necessary 14 | father = new int[n + 1]; 15 | for (int i = 1; i <= n; ++i) { 16 | father[i] = i; 17 | } 18 | } 19 | 20 | /* 21 | * @param a: An integer 22 | * @param b: An integer 23 | * @return: nothing 24 | */ 25 | public void connect(int a, int b) { 26 | // write your code here 27 | int root_a = find(a); 28 | int root_b = find(b); 29 | if (root_a != root_b) { 30 | father[root_a] = root_b; 31 | } 32 | } 33 | 34 | /* 35 | * @param a: An integer 36 | * @param b: An integer 37 | * @return: A boolean 38 | */ 39 | public boolean query(int a, int b) { 40 | // write your code here 41 | return find(a) == find(b); 42 | } 43 | } 44 | 45 | -------------------------------------------------------------------------------- /java/ConnectingGraph2.java: -------------------------------------------------------------------------------- 1 | public class ConnectingGraph2 { 2 | private int[] father = null; 3 | private int[] size = null; 4 | private int find(int x) { 5 | if (father[x] == x) { 6 | return x; 7 | } 8 | return father[x] = find(father[x]); 9 | } 10 | /* 11 | * @param n: An integer 12 | */ 13 | public ConnectingGraph2(int n) { 14 | // do intialization if necessary 15 | father = new int[n + 1]; 16 | size = new int[n + 1]; 17 | for (int i = 1; i <= n; i++) { 18 | father[i] = i; 19 | size[i] = 1; 20 | } 21 | } 22 | 23 | /* 24 | * @param a: An integer 25 | * @param b: An integer 26 | * @return: nothing 27 | */ 28 | public void connect(int a, int b) { 29 | // write your code here 30 | int root_a = find(a); 31 | int root_b = find(b); 32 | if (root_a != root_b) { 33 | father[root_a] = root_b; 34 | size[root_b] += size[root_a]; 35 | } 36 | } 37 | 38 | /* 39 | * @param a: An integer 40 | * @return: An integer 41 | */ 42 | public int query(int a) { 43 | // write your code here 44 | return size[find(a)]; 45 | } 46 | } 47 | 48 | -------------------------------------------------------------------------------- /java/ConnectingGraph3.java: -------------------------------------------------------------------------------- 1 | public class ConnectingGraph3 { 2 | private int[] father = null; 3 | private int count; 4 | 5 | private int find(int x) { 6 | if (father[x] == x) { 7 | return x; 8 | } 9 | return father[x] = find(father[x]); 10 | } 11 | /* 12 | * @param n: An integer 13 | */public ConnectingGraph3(int n) { 14 | // do intialization if necessary 15 | father = new int[n + 1]; 16 | count = n; 17 | for (int i = 1; i <= n; ++i) { 18 | father[i] = i; 19 | } 20 | } 21 | 22 | /* 23 | * @param a: An integer 24 | * @param b: An integer 25 | * @return: nothing 26 | */ 27 | public void connect(int a, int b) { 28 | // write your code here 29 | int root_a = find(a); 30 | int root_b = find(b); 31 | if (root_a != root_b) { 32 | father[root_a] = root_b; 33 | count --; 34 | } 35 | } 36 | 37 | /* 38 | * @return: An integer 39 | */ 40 | public int query() { 41 | // write your code here 42 | return count; 43 | } 44 | } 45 | 46 | -------------------------------------------------------------------------------- /java/ContinuousSubarraySum.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param A an integer array 4 | * @return A list of integers includes the index of the first number and the index of the last number 5 | */ 6 | public ArrayList continuousSubarraySum(int[] A) { 7 | // Write your code here 8 | ArrayList result = new ArrayList(); 9 | result.add(0); 10 | result.add(0); 11 | 12 | int len = A.length; 13 | int start = 0, end = 0; 14 | int sum = 0; 15 | int ans = -0x7fffffff; 16 | for (int i = 0; i < len; ++i) { 17 | if (sum < 0) { 18 | sum = A[i]; 19 | start = end = i; 20 | } else { 21 | sum += A[i]; 22 | end = i; 23 | } 24 | if (sum >= ans) { 25 | ans = sum; 26 | result.set(0, start); 27 | result.set(1, end); 28 | } 29 | } 30 | return result; 31 | } 32 | } -------------------------------------------------------------------------------- /java/ConvertBinarySearchTreetoDoublyLinkedList.java: -------------------------------------------------------------------------------- 1 | class ResultType { 2 | DoublyListNode first, last; 3 | 4 | public ResultType(DoublyListNode first, DoublyListNode last) { 5 | this.first = first; 6 | this.last = last; 7 | } 8 | } 9 | 10 | public class Solution { 11 | /** 12 | * @param root: The root of tree 13 | * @return: the head of doubly list node 14 | */ 15 | public DoublyListNode bstToDoublyList(TreeNode root) { 16 | if (root == null) { 17 | return null; 18 | } 19 | 20 | ResultType result = helper(root); 21 | return result.first; 22 | } 23 | 24 | public ResultType helper(TreeNode root) { 25 | if (root == null) { 26 | return null; 27 | } 28 | 29 | ResultType left = helper(root.left); 30 | ResultType right = helper(root.right); 31 | DoublyListNode node = new DoublyListNode(root.val); 32 | 33 | ResultType result = new ResultType(null, null); 34 | 35 | if (left == null) { 36 | result.first = node; 37 | } else { 38 | result.first = left.first; 39 | left.last.next = node; 40 | node.prev = left.last; 41 | } 42 | 43 | if (right == null) { 44 | result.last = node; 45 | } else { 46 | result.last = right.last; 47 | right.first.prev = node; 48 | node.next = right.first; 49 | } 50 | 51 | return result; 52 | } 53 | } -------------------------------------------------------------------------------- /java/CountofSmallerNumberBeforeItself.java: -------------------------------------------------------------------------------- 1 | class BITree { 2 | public int[] bit; 3 | 4 | public BITree(int range) { 5 | bit = new int[range + 1]; 6 | } 7 | 8 | public void increase(int index, int delta) { 9 | for (int i = index + 1; i < bit.length; i = i + lowbit(i)) { 10 | bit[i] += delta; 11 | } 12 | } 13 | 14 | public int getPrefixSum(int index) { 15 | int sum = 0; 16 | for (int i = index + 1; i >= 1; i = i - lowbit(i)) { 17 | sum += bit[i]; 18 | } 19 | return sum; 20 | } 21 | 22 | private int lowbit(int x) { 23 | return x & (-x); 24 | } 25 | } 26 | 27 | public class Solution { 28 | /** 29 | * @param A: an integer array 30 | * @return: A list of integers includes the index of the first number and the index of the last number 31 | */ 32 | public List countOfSmallerNumberII(int[] A) { 33 | BITree bitree = new BITree(10000); 34 | List results = new ArrayList<>(); 35 | for (int i = 0; i < A.length; i++) { 36 | results.add(bitree.getPrefixSum(A[i] - 1)); 37 | bitree.increase(A[i], 1); 38 | } 39 | 40 | return results; 41 | } 42 | 43 | } -------------------------------------------------------------------------------- /java/CourseSchedule.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param numCourses a total of n courses 4 | * @param prerequisites a list of prerequisite pairs 5 | * @return true if can finish all courses or false 6 | */ 7 | public boolean canFinish(int numCourses, int[][] prerequisites) { 8 | // Write your code here 9 | List[] edges = new ArrayList[numCourses]; 10 | int[] degree = new int[numCourses]; 11 | 12 | for (int i = 0;i < numCourses; i++) 13 | edges[i] = new ArrayList(); 14 | 15 | for (int i = 0; i < prerequisites.length; i++) { 16 | degree[prerequisites[i][0]] ++ ; 17 | edges[prerequisites[i][1]].add(prerequisites[i][0]); 18 | } 19 | 20 | Queue queue = new LinkedList(); 21 | for(int i = 0; i < degree.length; i++){ 22 | if (degree[i] == 0) { 23 | queue.add(i); 24 | } 25 | } 26 | 27 | int count = 0; 28 | while(!queue.isEmpty()){ 29 | int course = (int)queue.poll(); 30 | count ++; 31 | int n = edges[course].size(); 32 | for(int i = 0; i < n; i++){ 33 | int pointer = (int)edges[course].get(i); 34 | degree[pointer]--; 35 | if (degree[pointer] == 0) { 36 | queue.add(pointer); 37 | } 38 | } 39 | } 40 | 41 | return count == numCourses; 42 | } 43 | } -------------------------------------------------------------------------------- /java/CourseSchedule2.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param numCourses a total of n courses 4 | * @param prerequisites a list of prerequisite pairs 5 | * @return the course order 6 | */ 7 | public int[] findOrder(int numCourses, int[][] prerequisites) { 8 | // Write your code here 9 | List[] edges = new ArrayList[numCourses]; 10 | int[] degree = new int[numCourses]; 11 | 12 | for (int i = 0;i < numCourses; i++) 13 | edges[i] = new ArrayList(); 14 | 15 | for (int i = 0; i < prerequisites.length; i++) { 16 | degree[prerequisites[i][0]] ++ ; 17 | edges[prerequisites[i][1]].add(prerequisites[i][0]); 18 | } 19 | 20 | Queue queue = new LinkedList(); 21 | for(int i = 0; i < degree.length; i++){ 22 | if (degree[i] == 0) { 23 | queue.add(i); 24 | } 25 | } 26 | 27 | int count = 0; 28 | int[] order = new int[numCourses]; 29 | while(!queue.isEmpty()){ 30 | int course = (int)queue.poll(); 31 | order[count] = course; 32 | count ++; 33 | int n = edges[course].size(); 34 | for(int i = n - 1; i >= 0 ; i--){ 35 | int pointer = (int)edges[course].get(i); 36 | degree[pointer]--; 37 | if (degree[pointer] == 0) { 38 | queue.add(pointer); 39 | } 40 | } 41 | } 42 | 43 | if (count == numCourses) 44 | return order; 45 | 46 | return new int[0]; 47 | } 48 | } -------------------------------------------------------------------------------- /java/DataStreamMedian.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param nums: A list of integers. 4 | * @return: the median of numbers 5 | */ 6 | private PriorityQueue maxHeap, minHeap; 7 | private int numOfElements = 0; 8 | 9 | public int[] medianII(int[] nums) { 10 | // write your code here 11 | Comparator revCmp = new Comparator() { 12 | @Override 13 | public int compare(Integer left, Integer right) { 14 | return right.compareTo(left); 15 | } 16 | }; 17 | int cnt = nums.length; 18 | maxHeap = new PriorityQueue(cnt, revCmp); 19 | minHeap = new PriorityQueue(cnt); 20 | int[] ans = new int[cnt]; 21 | for (int i = 0; i < cnt; ++i) { 22 | addNumber(nums[i]); 23 | ans[i] = getMedian(); 24 | } 25 | return ans; 26 | } 27 | void addNumber(int value) { 28 | maxHeap.add(value); 29 | if (numOfElements%2 == 0) { 30 | if (minHeap.isEmpty()) { 31 | numOfElements++; 32 | return; 33 | } 34 | else if (maxHeap.peek() > minHeap.peek()) { 35 | Integer maxHeapRoot = maxHeap.poll(); 36 | Integer minHeapRoot = minHeap.poll(); 37 | maxHeap.add(minHeapRoot); 38 | minHeap.add(maxHeapRoot); 39 | } 40 | } 41 | else { 42 | minHeap.add(maxHeap.poll()); 43 | } 44 | numOfElements++; 45 | } 46 | int getMedian() { 47 | return maxHeap.peek(); 48 | } 49 | } -------------------------------------------------------------------------------- /java/DistinctSubsequences.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int numDistinct(String S, String T) { 3 | if (S == null || T == null) { 4 | return 0; 5 | } 6 | 7 | int[][] nums = new int[S.length() + 1][T.length() + 1]; 8 | 9 | for (int i = 0; i <= S.length(); i++) { 10 | nums[i][0] = 1; 11 | } 12 | for (int i = 1; i <= S.length(); i++) { 13 | for (int j = 1; j <= T.length(); j++) { 14 | nums[i][j] = nums[i - 1][j]; 15 | if (S.charAt(i - 1) == T.charAt(j - 1)) { 16 | nums[i][j] += nums[i - 1][j - 1]; 17 | } 18 | } 19 | } 20 | return nums[S.length()][T.length()]; 21 | } 22 | } -------------------------------------------------------------------------------- /java/DivideTwoIntegers.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param dividend the dividend 4 | * @param divisor the divisor 5 | * @return the result 6 | */ 7 | public int divide(int dividend, int divisor) { 8 | if (divisor == 0) { 9 | return dividend >= 0? Integer.MAX_VALUE : Integer.MIN_VALUE; 10 | } 11 | 12 | if (dividend == 0) { 13 | return 0; 14 | } 15 | 16 | if (dividend == Integer.MIN_VALUE && divisor == -1) { 17 | return Integer.MAX_VALUE; 18 | } 19 | 20 | boolean isNegative = (dividend < 0 && divisor > 0) || 21 | (dividend > 0 && divisor < 0); 22 | 23 | long a = Math.abs((long)dividend); 24 | long b = Math.abs((long)divisor); 25 | int result = 0; 26 | while(a >= b){ 27 | int shift = 0; 28 | while(a >= (b << shift)){ 29 | shift++; 30 | } 31 | a -= b << (shift - 1); 32 | result += 1 << (shift - 1); 33 | } 34 | return isNegative? -result: result; 35 | } 36 | } -------------------------------------------------------------------------------- /java/DropEggs2.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param m the number of eggs 4 | * @param n the umber of floors 5 | * @return the number of drops in the worst case 6 | */ 7 | public int dropEggs2(int m, int n) { 8 | // Write your code here 9 | int[][] dp = new int[m + 1][n + 1]; 10 | 11 | for (int i = 1; i <= m; ++i) { 12 | dp[i][1] = 1; 13 | dp[i][0] = 0; 14 | } 15 | 16 | for (int j = 1; j <= n; ++j) 17 | dp[1][j] = j; 18 | 19 | for (int i = 2; i <= m; ++i) { 20 | for (int j = 2; j <= n; ++j) { 21 | dp[i][j] = Integer.MAX_VALUE; 22 | for (int k = 1; k <= j; ++k) { 23 | dp[i][j] = Math.min(dp[i][j], 24 | 1 + Math.max(dp[i - 1][k - 1], dp[i][j - k])); 25 | } 26 | } 27 | } 28 | 29 | return dp[m][n]; 30 | } 31 | } -------------------------------------------------------------------------------- /java/FastPower.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param a: A 32bit integer 4 | * @param b: A 32bit integer 5 | * @param n: A 32bit integer 6 | * @return: An integer 7 | */ 8 | public int fastPower(int a, int b, int n) { 9 | long ans = 1, tmp = a; 10 | 11 | while (n != 0) { 12 | if (n % 2 == 1) { 13 | ans = (ans * tmp) % b; 14 | } 15 | tmp = (tmp * tmp) % b; 16 | n = n / 2; 17 | } 18 | 19 | return (int) ans % b; 20 | } 21 | } -------------------------------------------------------------------------------- /java/Flatten2DVector.java: -------------------------------------------------------------------------------- 1 | public class Vector2D implements Iterator { 2 | 3 | private Iterator> i; 4 | private Iterator j; 5 | 6 | public Vector2D(List> vec2d) { 7 | // Initialize your data structure here 8 | i = vec2d.iterator(); 9 | j = null; 10 | } 11 | 12 | @Override 13 | public Integer next() { 14 | // Write your code here 15 | if (!hasNext()) { 16 | return null; 17 | } 18 | return j.next(); 19 | } 20 | 21 | @Override 22 | public boolean hasNext() { 23 | // Write your code here 24 | while ((j == null || !j.hasNext()) && i.hasNext()) 25 | j = i.next().iterator(); 26 | return j != null && j.hasNext(); 27 | } 28 | 29 | @Override 30 | public void remove() {} 31 | } 32 | -------------------------------------------------------------------------------- /java/FlattenBinaryTreetoLinkedList.java: -------------------------------------------------------------------------------- 1 | // Version 1: Traverse 2 | public class Solution { 3 | private TreeNode lastNode = null; 4 | 5 | public void flatten(TreeNode root) { 6 | if (root == null) { 7 | return; 8 | } 9 | 10 | if (lastNode != null) { 11 | lastNode.left = null; 12 | lastNode.right = root; 13 | } 14 | 15 | lastNode = root; 16 | TreeNode right = root.right; 17 | flatten(root.left); 18 | flatten(right); 19 | } 20 | } 21 | 22 | // version 2: Divide & Conquer 23 | public class Solution { 24 | /** 25 | * @param root: a TreeNode, the root of the binary tree 26 | * @return: nothing 27 | */ 28 | public void flatten(TreeNode root) { 29 | helper(root); 30 | } 31 | 32 | // flatten root and return the last node 33 | private TreeNode helper(TreeNode root) { 34 | if (root == null) { 35 | return null; 36 | } 37 | 38 | TreeNode leftLast = helper(root.left); 39 | TreeNode rightLast = helper(root.right); 40 | 41 | // connect leftLast to root.right 42 | if (leftLast != null) { 43 | leftLast.right = root.right; 44 | root.right = root.left; 45 | root.left = null; 46 | } 47 | 48 | if (rightLast != null) { 49 | return rightLast; 50 | } 51 | 52 | if (leftLast != null) { 53 | return leftLast; 54 | } 55 | 56 | return root; 57 | } 58 | } -------------------------------------------------------------------------------- /java/FlattenNestedListIterator.java: -------------------------------------------------------------------------------- 1 | import java.util.Iterator; 2 | 3 | public class NestedIterator implements Iterator { 4 | 5 | private Stack stack; 6 | 7 | private void pushListToStack(List nestedList) { 8 | Stack temp = new Stack<>(); 9 | for (NestedInteger nested : nestedList) { 10 | temp.push(nested); 11 | } 12 | 13 | while (!temp.isEmpty()) { 14 | stack.push(temp.pop()); 15 | } 16 | } 17 | 18 | public NestedIterator(List nestedList) { 19 | stack = new Stack<>(); 20 | pushListToStack(nestedList); 21 | } 22 | 23 | // @return {int} the next element in the iteration 24 | @Override 25 | public Integer next() { 26 | if (!hasNext()) { 27 | return null; 28 | } 29 | return stack.pop().getInteger(); 30 | } 31 | 32 | // @return {boolean} true if the iteration has more element or false 33 | @Override 34 | public boolean hasNext() { 35 | while (!stack.isEmpty() && !stack.peek().isInteger()) { 36 | pushListToStack(stack.pop().getList()); 37 | } 38 | 39 | return !stack.isEmpty(); 40 | } 41 | 42 | @Override 43 | public void remove() {} 44 | } -------------------------------------------------------------------------------- /java/FrogJump.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param stones a list of stones' positions in sorted ascending order 4 | * @return true if the frog is able to cross the river or false 5 | */ 6 | public boolean canCross(int[] stones) { 7 | // Write your code here 8 | HashMap> dp = 9 | new HashMap>(stones.length); 10 | for (int i = 0; i < stones.length; i++) { 11 | dp.put(stones[i], new HashSet() ); 12 | } 13 | dp.get(0).add(0); 14 | 15 | for (int i = 0; i < stones.length - 1; ++i) { 16 | int stone = stones[i]; 17 | for (int k : dp.get(stone)) { 18 | // k - 1 19 | if (k - 1 > 0 && dp.containsKey(stone + k - 1)) 20 | dp.get(stone + k - 1).add(k - 1); 21 | // k 22 | if (dp.containsKey(stone + k)) 23 | dp.get(stone + k).add(k); 24 | // k + 1 25 | if (dp.containsKey(stone + k + 1)) 26 | dp.get(stone + k + 1).add(k + 1); 27 | } 28 | } 29 | 30 | return !dp.get(stones[stones.length - 1]).isEmpty(); 31 | } 32 | } -------------------------------------------------------------------------------- /java/HashFunction.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /* 3 | * @param key: A string you should hash 4 | * @param HASH_SIZE: An integer 5 | * @return: An integer 6 | */ 7 | public int hashCode(char[] key, int HASH_SIZE) { 8 | // write your code here 9 | long result = 0; 10 | for (int i = 0; i < key.length; i++) { 11 | result = (result * 33 + (int)(key[i])) % HASH_SIZE; 12 | } 13 | return (int)result; 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /java/HighFive.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param results a list of 4 | * @return find the average of 5 highest scores for each person 5 | * Map (student_id, average_score) 6 | */ 7 | public Map highFive(Record[] results) { 8 | Map answer = new HashMap(); 9 | Map> hash = new HashMap>(); 10 | 11 | for (Record r : results) { 12 | if (!hash.containsKey(r.id)){ 13 | hash.put(r.id, new PriorityQueue()); 14 | } 15 | PriorityQueue pq=hash.get(r.id); 16 | if (pq.size() < 5) { 17 | pq.add(r.score); 18 | } else { 19 | if (pq.peek() < r.score){ 20 | pq.poll(); 21 | pq.add(r.score); 22 | } 23 | } 24 | } 25 | 26 | for (Map.Entry> entry : hash.entrySet()) { 27 | int id = entry.getKey(); 28 | PriorityQueue scores = entry.getValue(); 29 | double average = 0; 30 | for (int i = 0; i < 5; ++i) 31 | average += scores.poll(); 32 | average /= 5.0; 33 | answer.put(id, average); 34 | } 35 | return answer; 36 | } 37 | } -------------------------------------------------------------------------------- /java/HouseRobber.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param A: An array of non-negative integers. 4 | * return: The maximum amount of money you can rob tonight 5 | */ 6 | //---方法一--- 7 | public long houseRobber(int[] A) { 8 | // write your code here 9 | int n = A.length; 10 | if(n == 0) 11 | return 0; 12 | long []res = new long[n+1]; 13 | 14 | 15 | res[0] = 0; 16 | res[1] = A[0]; 17 | for(int i = 2; i <= n; i++) { 18 | res[i] = Math.max(res[i-1], res[i-2] + A[i-1]); 19 | } 20 | return res[n]; 21 | } 22 | //---方法二--- 23 | public long houseRobber(int[] A) { 24 | // write your code here 25 | int n = A.length; 26 | if(n == 0) 27 | return 0; 28 | long []res = new long[2]; 29 | 30 | 31 | res[0] = 0; 32 | res[1] = A[0]; 33 | for(int i = 2; i <= n; i++) { 34 | res[i%2] = Math.max(res[(i-1)%2], res[(i-2)%2] + A[i-1]); 35 | } 36 | return res[n%2]; 37 | } 38 | } -------------------------------------------------------------------------------- /java/HouseRobber2.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int houseRobber2(int[] nums) { 3 | if (nums.length == 0) { 4 | return 0; 5 | } 6 | if (nums.length == 1) { 7 | return nums[0]; 8 | } 9 | return Math.max(robber1(nums, 0, nums.length - 2), robber1(nums, 1, nums.length - 1)); 10 | } 11 | public int robber1(int[] nums, int st, int ed) { 12 | int []res = new int[2]; 13 | if(st == ed) 14 | return nums[ed]; 15 | if(st+1 == ed) 16 | return Math.max(nums[st], nums[ed]); 17 | res[st%2] = nums[st]; 18 | res[(st+1)%2] = Math.max(nums[st], nums[st+1]); 19 | 20 | for(int i = st+2; i <= ed; i++) { 21 | res[i%2] = Math.max(res[(i-1)%2], res[(i-2)%2] + nums[i]); 22 | 23 | } 24 | return res[ed%2]; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /java/ImplementQueuebyTwoStacks.java: -------------------------------------------------------------------------------- 1 | public class MyQueue { 2 | private Stack stack1; 3 | private Stack stack2; 4 | 5 | public MyQueue() { 6 | // do initialization if necessary 7 | stack1 = new Stack(); 8 | stack2 = new Stack(); 9 | } 10 | 11 | private void stack2ToStack1(){ 12 | while(! stack2.isEmpty()){ 13 | stack1.push(stack2.pop()); 14 | } 15 | } 16 | 17 | public void push(int element) { 18 | // write your code here 19 | stack2.push(element); 20 | } 21 | 22 | public int pop() { 23 | // write your code here 24 | if(stack1.empty() == true){ 25 | this.stack2ToStack1(); 26 | } 27 | return stack1.pop(); 28 | } 29 | 30 | public int top() { 31 | // write your code here 32 | if(stack1.empty() == true){ 33 | this.stack2ToStack1(); 34 | } 35 | return stack1.peek(); 36 | } 37 | } -------------------------------------------------------------------------------- /java/ImplementStackbyTwoQueues.java: -------------------------------------------------------------------------------- 1 | class Stack { 2 | private Queue queue1; 3 | private Queue queue2; 4 | 5 | public Stack() { 6 | queue1 = new LinkedList(); 7 | queue2 = new LinkedList(); 8 | } 9 | 10 | private void moveItems() { 11 | while (queue1.size() != 1) { 12 | queue2.offer(queue1.poll()); 13 | } 14 | } 15 | 16 | private void swapQueues() { 17 | Queue temp = queue1; 18 | queue1 = queue2; 19 | queue2 = temp; 20 | } 21 | 22 | /** 23 | * push a new item into the stack 24 | */ 25 | public void push(int value) { 26 | queue1.offer(value); 27 | } 28 | 29 | /** 30 | * return the top of the stack 31 | */ 32 | public int top() { 33 | moveItems(); 34 | int item = queue1.poll(); 35 | swapQueues(); 36 | queue1.offer(item); 37 | return item; 38 | } 39 | 40 | /** 41 | * pop the top of the stack and return it 42 | */ 43 | public void pop() { 44 | moveItems(); 45 | queue1.poll(); 46 | swapQueues(); 47 | } 48 | 49 | /** 50 | * check the stack is empty or not. 51 | */ 52 | public boolean isEmpty() { 53 | return queue1.isEmpty(); 54 | } -------------------------------------------------------------------------------- /java/InorderSuccessorinBinarySearchTree.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public TreeNode inorderSuccessor(TreeNode root, TreeNode p) { 3 | TreeNode successor = null; 4 | while (root != null && root != p) { 5 | if (root.val > p.val) { 6 | successor = root; 7 | root = root.left; 8 | } else { 9 | root = root.right; 10 | } 11 | } 12 | 13 | if (root == null) { 14 | return null; 15 | } 16 | 17 | if (root.right == null) { 18 | return successor; 19 | } 20 | 21 | root = root.right; 22 | while (root.left != null) { 23 | root = root.left; 24 | } 25 | 26 | return root; 27 | } 28 | } 29 | 30 | // version: 高频题班 31 | public class Solution { 32 | public TreeNode inorderSuccessor(TreeNode root, TreeNode p) { 33 | // write your code here 34 | if (root == null || p == null) { 35 | return null; 36 | } 37 | 38 | if (root.val <= p.val) { 39 | return inorderSuccessor(root.right, p); 40 | } else { 41 | TreeNode left = inorderSuccessor(root.left, p); 42 | return (left != null) ? left : root; 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /java/InterleavingString.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean isInterleave(String s1, String s2, String s3) { 3 | if (s1.length() + s2.length() != s3.length()) { 4 | return false; 5 | } 6 | 7 | boolean [][] interleaved = new boolean[s1.length() + 1][s2.length() + 1]; 8 | interleaved[0][0] = true; 9 | 10 | for (int i = 1; i <= s1.length(); i++) { 11 | if(s3.charAt(i - 1) == s1.charAt(i - 1) && interleaved[i - 1][0]) 12 | interleaved[i][0] = true; 13 | } 14 | 15 | for (int j = 1; j <= s2.length(); j++) { 16 | if(s3.charAt(j - 1) == s2.charAt(j - 1) && interleaved[0][j - 1]) 17 | interleaved[0][j] = true; 18 | } 19 | 20 | for (int i = 1; i <= s1.length(); i++) { 21 | for (int j = 1; j <= s2.length(); j++) { 22 | if(((s3.charAt(i + j - 1) == s1.charAt(i - 1) && interleaved[i - 1][j])) 23 | || ((s3.charAt(i + j - 1)) == s2.charAt(j - 1) && interleaved[i][j - 1])) 24 | interleaved[i][j] = true; 25 | } 26 | } 27 | 28 | return interleaved[s1.length()][s2.length()]; 29 | } 30 | } -------------------------------------------------------------------------------- /java/JumpGame.java: -------------------------------------------------------------------------------- 1 | // version 1: Dynamic Programming 2 | // 这个方法,复杂度是 O(n^2) 可能会超时,但是依然需要掌握。 3 | public class Solution { 4 | public boolean canJump(int[] A) { 5 | boolean[] can = new boolean[A.length]; 6 | can[0] = true; 7 | 8 | for (int i = 1; i < A.length; i++) { 9 | for (int j = 0; j < i; j++) { 10 | if (can[j] && j + A[j] >= i) { 11 | can[i] = true; 12 | break; 13 | } 14 | } 15 | } 16 | 17 | return can[A.length - 1]; 18 | } 19 | } 20 | 21 | 22 | // version 2: Greedy 23 | public class Solution { 24 | public boolean canJump(int[] A) { 25 | // think it as merging n intervals 26 | if (A == null || A.length == 0) { 27 | return false; 28 | } 29 | int farthest = A[0]; 30 | for (int i = 1; i < A.length; i++) { 31 | if (i <= farthest && A[i] + i >= farthest) { 32 | farthest = A[i] + i; 33 | } 34 | } 35 | return farthest >= A.length - 1; 36 | } 37 | } -------------------------------------------------------------------------------- /java/JumpGame2.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param grid a chessboard included 0 and 1 4 | * @return the shortest path 5 | */ 6 | public int shortestPath2(boolean[][] grid) { 7 | // Write your code here 8 | int n = grid.length; 9 | if (n == 0) 10 | return -1; 11 | int m = grid[0].length; 12 | if (m == 0) 13 | return -1; 14 | 15 | int[][] f = new int[n][m]; 16 | for (int i = 0; i < n; ++i) 17 | for (int j = 0; j < m; ++j) 18 | f[i][j] = Integer.MAX_VALUE; 19 | 20 | f[0][0] = 0; 21 | for (int j = 1; j < m; ++j) 22 | for (int i = 0; i < n; ++i) 23 | if (!grid[i][j]) { 24 | if (i >= 1 && j >= 2 && f[i - 1][j - 2] != Integer.MAX_VALUE) 25 | f[i][j] = Math.min(f[i][j], f[i - 1][j - 2] + 1); 26 | if (i + 1 < n && j >= 2 && f[i + 1][j - 2] != Integer.MAX_VALUE) 27 | f[i][j] = Math.min(f[i][j], f[i + 1][j - 2] + 1); 28 | if (i >= 2 && j >= 1 && f[i - 2][j - 1] != Integer.MAX_VALUE) 29 | f[i][j] = Math.min(f[i][j], f[i - 2][j - 1] + 1); 30 | if (i + 2 < n && j >= 1 && f[i + 2][j - 1] != Integer.MAX_VALUE) 31 | f[i][j] = Math.min(f[i][j], f[i + 2][j - 1] + 1); 32 | } 33 | 34 | if (f[n - 1][m - 1] == Integer.MAX_VALUE) 35 | return -1; 36 | 37 | return f[n - 1][m - 1]; 38 | } 39 | } -------------------------------------------------------------------------------- /java/KClosestPoints.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a point. 3 | * class Point { 4 | * int x; 5 | * int y; 6 | * Point() { x = 0; y = 0; } 7 | * Point(int a, int b) { x = a; y = b; } 8 | * } 9 | */ 10 | public class Solution { 11 | /** 12 | * @param points a list of points 13 | * @param origin a point 14 | * @param k an integer 15 | * @return the k closest points 16 | */ 17 | private Point global_origin = null; 18 | public Point[] kClosest(Point[] points, Point origin, int k) { 19 | // Write your code here 20 | global_origin = origin; 21 | PriorityQueue pq = new PriorityQueue (k, new Comparator () { 22 | @Override 23 | public int compare(Point a, Point b) { 24 | int diff = getDistance(b, global_origin) - getDistance(a, global_origin); 25 | if (diff == 0) 26 | diff = b.x - a.x; 27 | if (diff == 0) 28 | diff = b.y - a.y; 29 | return diff; 30 | } 31 | }); 32 | 33 | for (int i = 0; i < points.length; i++) { 34 | pq.offer(points[i]); 35 | if (pq.size() > k) 36 | pq.poll(); 37 | } 38 | 39 | k = pq.size(); 40 | Point[] ret = new Point[k]; 41 | while (!pq.isEmpty()) 42 | ret[--k] = pq.poll(); 43 | return ret; 44 | } 45 | 46 | private int getDistance(Point a, Point b) { 47 | return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); 48 | } 49 | } -------------------------------------------------------------------------------- /java/KnightShortestPath2.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param grid a chessboard included 0 and 1 4 | * @return the shortest path 5 | */ 6 | public int shortestPath2(boolean[][] grid) { 7 | // Write your code here 8 | int n = grid.length; 9 | if (n == 0) 10 | return -1; 11 | int m = grid[0].length; 12 | if (m == 0) 13 | return -1; 14 | 15 | int[][] f = new int[n][m]; 16 | for (int i = 0; i < n; ++i) 17 | for (int j = 0; j < m; ++j) 18 | f[i][j] = Integer.MAX_VALUE; 19 | 20 | f[0][0] = 0; 21 | for (int j = 1; j < m; ++j) 22 | for (int i = 0; i < n; ++i) 23 | if (!grid[i][j]) { 24 | if (i >= 1 && j >= 2 && f[i - 1][j - 2] != Integer.MAX_VALUE) 25 | f[i][j] = Math.min(f[i][j], f[i - 1][j - 2] + 1); 26 | if (i + 1 < n && j >= 2 && f[i + 1][j - 2] != Integer.MAX_VALUE) 27 | f[i][j] = Math.min(f[i][j], f[i + 1][j - 2] + 1); 28 | if (i >= 2 && j >= 1 && f[i - 2][j - 1] != Integer.MAX_VALUE) 29 | f[i][j] = Math.min(f[i][j], f[i - 2][j - 1] + 1); 30 | if (i + 2 < n && j >= 1 && f[i + 2][j - 1] != Integer.MAX_VALUE) 31 | f[i][j] = Math.min(f[i][j], f[i + 2][j - 1] + 1); 32 | } 33 | 34 | if (f[n - 1][m - 1] == Integer.MAX_VALUE) 35 | return -1; 36 | 37 | return f[n - 1][m - 1]; 38 | } 39 | } -------------------------------------------------------------------------------- /java/KthLargestElement.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | /* 3 | * @param k : description of k 4 | * @param nums : array of nums 5 | * @return: description of return 6 | */ 7 | public int kthLargestElement(int k, int[] nums) { 8 | // write your code here 9 | return quickSelect(nums, 0, nums.length - 1, k); 10 | } 11 | 12 | private int quickSelect(int[] nums, int start, int end, int k) { 13 | int left = start, right = end; 14 | int pivot = nums[(start + end) / 2]; 15 | 16 | while (left <= right) { 17 | while (left <= right && nums[left] > pivot) { 18 | left++; 19 | } 20 | while (left <= right && nums[right] < pivot) { 21 | right--; 22 | } 23 | 24 | if (left <= right) { 25 | int temp = nums[left]; 26 | nums[left] = nums[right]; 27 | nums[right] = temp; 28 | 29 | left++; 30 | right--; 31 | } 32 | } 33 | 34 | if (start + k - 1 <= right) { 35 | return quickSelect(nums, start, right, k); 36 | } 37 | 38 | if (start + k - 1 >= left) { 39 | return quickSelect(nums, left, end, k - (left - start)); 40 | } 41 | 42 | return nums[right + 1]; 43 | } 44 | } 45 | 46 | -------------------------------------------------------------------------------- /java/KthSmallestNumberInSortedMatrix.java: -------------------------------------------------------------------------------- 1 | class Pair { 2 | public int x, y, val; 3 | public Pair(int x, int y, int val) { 4 | this.x = x; 5 | this.y = y; 6 | this.val = val; 7 | } 8 | } 9 | class PairComparator implements Comparator { 10 | public int compare(Pair a, Pair b) { 11 | return a.val - b.val; 12 | } 13 | } 14 | public class Solution { 15 | /** 16 | * @param matrix: a matrix of integers 17 | * @param k: An integer 18 | * @return: the kth smallest number in the matrix 19 | */ 20 | 21 | public int kthSmallest(int[][] matrix, int k) { 22 | // write your code here 23 | int[] dx = new int[]{0, 1}; 24 | int[] dy = new int[]{1, 0}; 25 | int n = matrix.length; 26 | int m = matrix[0].length; 27 | boolean[][] hash = new boolean[n][m]; 28 | PriorityQueue minHeap = new PriorityQueue(k, new PairComparator()); 29 | minHeap.add(new Pair(0, 0, matrix[0][0])); 30 | 31 | for(int i = 0; i < k - 1; i ++){ 32 | Pair cur = minHeap.poll(); 33 | for(int j = 0; j < 2; j ++){ 34 | int next_x = cur.x + dx[j]; 35 | int next_y = cur.y + dy[j]; 36 | Pair next_Pair = new Pair(next_x, next_y, 0); 37 | if(next_x < n && next_y < m && !hash[next_x][next_y]){ 38 | hash[next_x][next_y] = true; 39 | next_Pair.val = matrix[next_x][next_y]; 40 | minHeap.add(next_Pair); 41 | } 42 | } 43 | } 44 | return minHeap.peek().val; 45 | } 46 | } 47 | 48 | -------------------------------------------------------------------------------- /java/KthSmallestNumbersinUnsortedArray.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | /* 3 | * @param k an integer 4 | * @param nums an integer array 5 | * @return kth smallest element 6 | */ 7 | public int kthSmallest(int k, int[] nums) { 8 | // write your code here 9 | return quickSelect(nums, 0, nums.length - 1, k - 1); 10 | } 11 | 12 | public int quickSelect(int[] A, int start, int end , int k) { 13 | 14 | if (start == end) 15 | return A[start]; 16 | 17 | int left = start, right = end; 18 | int pivot = A[(start + end) / 2]; 19 | 20 | while (left <= right) { 21 | while (left <= right && A[left] < pivot) { 22 | left++; 23 | } 24 | 25 | while (left <= right && A[right] > pivot) { 26 | right--; 27 | } 28 | if (left <= right) { 29 | int temp = A[left]; 30 | A[left] = A[right]; 31 | A[right] = temp; 32 | 33 | left++; 34 | right--; 35 | } 36 | } 37 | 38 | if (right >= k && start <= right) 39 | return quickSelect(A, start, right, k); 40 | else if (left <= k && left <= end) 41 | return quickSelect(A, left, end, k); 42 | else 43 | return A[k]; 44 | } 45 | }; -------------------------------------------------------------------------------- /java/LargestDivisibleSubset.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public List largestDivisibleSubset(int[] nums) { 3 | Arrays.sort(nums); 4 | int[] f = new int[nums.length]; 5 | int[] pre = new int[nums.length]; 6 | for (int i = 0; i < nums.length; i++) { 7 | f[i] = 1; 8 | pre[i] = i; 9 | for (int j = 0; j < i; j++) { 10 | if (nums[i] % nums[j] == 0 && f[i] < f[j] + 1) { 11 | f[i] = f[j] + 1; 12 | pre[i] = j; 13 | } 14 | } 15 | } 16 | 17 | List ans = new ArrayList(); 18 | if (nums.length == 0) { 19 | return ans; 20 | } 21 | int max = 0; 22 | int max_i = 0; 23 | for (int i = 0; i < nums.length; i++) { 24 | if (f[i] > max) { 25 | max = f[i]; 26 | max_i = i; 27 | } 28 | } 29 | ans.add(nums[max_i]); 30 | while (max_i != pre[max_i]) { 31 | max_i = pre[max_i]; 32 | ans.add(nums[max_i]); 33 | } 34 | Collections.reverse(ans); 35 | return ans; 36 | } 37 | } -------------------------------------------------------------------------------- /java/LargestRectangleinHistogram.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int largestRectangleArea(int[] height) { 3 | if (height == null || height.length == 0) { 4 | return 0; 5 | } 6 | 7 | Stack stack = new Stack(); 8 | int max = 0; 9 | for (int i = 0; i <= height.length; i++) { 10 | int curt = (i == height.length) ? -1 : height[i]; 11 | while (!stack.isEmpty() && curt <= height[stack.peek()]) { 12 | int h = height[stack.pop()]; 13 | int w = stack.isEmpty() ? i : i - stack.peek() - 1; 14 | max = Math.max(max, h * w); 15 | } 16 | stack.push(i); 17 | } 18 | 19 | return max; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java/LongestCommonSubsequence.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param A, B: Two strings. 4 | * @return: The length of longest common subsequence of A and B. 5 | */ 6 | public int longestCommonSubsequence(String A, String B) { 7 | int n = A.length(); 8 | int m = B.length(); 9 | int f[][] = new int[n + 1][m + 1]; 10 | for(int i = 1; i <= n; i++){ 11 | for(int j = 1; j <= m; j++){ 12 | f[i][j] = Math.max(f[i - 1][j], f[i][j - 1]); 13 | if(A.charAt(i - 1) == B.charAt(j - 1)) 14 | f[i][j] = f[i - 1][j - 1] + 1; 15 | } 16 | } 17 | return f[n][m]; 18 | } 19 | } -------------------------------------------------------------------------------- /java/LongestIncreasingContinuousSubsequence2.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param A an integer matrix 4 | * @return an integer 5 | */ 6 | int [][]dp; 7 | int [][]flag ; 8 | int n ,m; 9 | public int longestIncreasingContinuousSubsequenceII(int[][] A) { 10 | // Write your code here 11 | if(A.length == 0) 12 | return 0; 13 | n = A.length; 14 | m = A[0].length; 15 | int ans= 0; 16 | dp = new int[n][m]; 17 | flag = new int[n][m]; 18 | 19 | for(int i = 0; i < n; i++) { 20 | for(int j = 0; j < m; j++) { 21 | dp[i][j] = search(i, j, A); 22 | ans = Math.max(ans, dp[i][j]); 23 | } 24 | } 25 | return ans; 26 | } 27 | int []dx = {1,-1,0,0}; 28 | int []dy = {0,0,1,-1}; 29 | 30 | int search(int x, int y, int[][] A) { 31 | if(flag[x][y] != 0) 32 | return dp[x][y]; 33 | 34 | int ans = 1; 35 | int nx , ny; 36 | for(int i = 0; i < 4; i++) { 37 | nx = x + dx[i]; 38 | ny = y + dy[i]; 39 | if(0<= nx && nx < n && 0<= ny && ny < m ) { 40 | if( A[x][y] > A[nx][ny]) { 41 | ans = Math.max(ans, search( nx, ny, A) + 1); 42 | } 43 | } 44 | } 45 | flag[x][y] = 1; 46 | dp[x][y] = ans; 47 | return ans; 48 | } 49 | } -------------------------------------------------------------------------------- /java/LongestRepeatingSubsequence.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param str a string 4 | * @return the length of the longest repeating subsequence 5 | */ 6 | public int longestRepeatingSubsequence(String str) { 7 | // Write your code here 8 | int n = str.length(); 9 | 10 | int[][] dp = new int[n + 1][n + 1]; 11 | 12 | for (int i = 0; i <= n; ++i) 13 | for (int j = 0; j <= n; ++j) 14 | dp[i][j] = 0; 15 | 16 | for (int i = 1; i <= n; ++i) { 17 | for (int j = 1; j <= n; ++j) { 18 | if (str.charAt(i - 1) == str.charAt(j - 1) && i != j) 19 | dp[i][j] = dp[i - 1][j - 1] + 1; 20 | else 21 | dp[i][j] = Math.max(dp[i][j - 1], dp[i - 1][j]); 22 | } 23 | } 24 | return dp[n][n]; 25 | } 26 | } -------------------------------------------------------------------------------- /java/LongestSubstringWithAtMostKDistinctCharacters.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param s: A string 4 | * @param k: An integer 5 | * @return: An integer 6 | */ 7 | public int lengthOfLongestSubstringKDistinct(String s, int k) { 8 | // write your code here 9 | int maxLen = 0; 10 | 11 | // Key: letter; value: the number of occurrences. 12 | Map map = new HashMap(); 13 | int i, j = 0; 14 | char c; 15 | for (i = 0; i < s.length(); i++) { 16 | while (j < s.length()) { 17 | c = s.charAt(j); 18 | if (map.containsKey(c)) { 19 | map.put(c, map.get(c) + 1); 20 | } else { 21 | if(map.size() ==k) 22 | break; 23 | map.put(c, 1); 24 | } 25 | j++; 26 | } 27 | 28 | maxLen = Math.max(maxLen, j - i); 29 | c = s.charAt(i); 30 | if(map.containsKey(c)){ 31 | int count = map.get(c); 32 | if (count > 1) { 33 | map.put(c, count - 1); 34 | } else { 35 | map.remove(c); 36 | } 37 | } 38 | } 39 | return maxLen; 40 | } 41 | } 42 | 43 | -------------------------------------------------------------------------------- /java/LongestSubstringWithoutRepeatingCharacters.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param s: a string 4 | * @return: an integer 5 | */ 6 | public int lengthOfLongestSubstring(String s) { 7 | // write your code here 8 | int[] map = new int[256]; 9 | int i, j = 0; 10 | int res = 0; 11 | for (i = 0; i < s.length(); i++) { 12 | while (j < s.length() && map[s.charAt(j)] == 0) { 13 | map[s.charAt(j)] = 1; 14 | res = Math.max(res, j-i + 1); 15 | j++; 16 | } 17 | map[s.charAt(i)] = 0; 18 | } 19 | return res; 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /java/LowestCommonAncestor.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public TreeNode lowestCommonAncestor(TreeNode root, TreeNode node1, TreeNode node2) { 3 | if (root == null || root == node1 || root == node2) { 4 | return root; 5 | } 6 | 7 | // Divide 8 | TreeNode left = lowestCommonAncestor(root.left, node1, node2); 9 | TreeNode right = lowestCommonAncestor(root.right, node1, node2); 10 | 11 | // Conquer 12 | if (left != null && right != null) { 13 | return root; 14 | } 15 | if (left != null) { 16 | return left; 17 | } 18 | if (right != null) { 19 | return right; 20 | } 21 | return null; 22 | } 23 | } -------------------------------------------------------------------------------- /java/LowestCommonAncestor2.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param root: The root of the tree 4 | * @param A, B: Two node in the tree 5 | * @return: The lowest common ancestor of A and B 6 | */ 7 | public ParentTreeNode lowestCommonAncestorII(ParentTreeNode root, 8 | ParentTreeNode A, 9 | ParentTreeNode B) { 10 | ArrayList pathA = getPath2Root(A); 11 | ArrayList pathB = getPath2Root(B); 12 | 13 | int indexA = pathA.size() - 1; 14 | int indexB = pathB.size() - 1; 15 | 16 | ParentTreeNode lowestAncestor = null; 17 | while (indexA >= 0 && indexB >= 0) { 18 | if (pathA.get(indexA) != pathB.get(indexB)) { 19 | break; 20 | } 21 | lowestAncestor = pathA.get(indexA); 22 | indexA--; 23 | indexB--; 24 | } 25 | 26 | return lowestAncestor; 27 | } 28 | 29 | private ArrayList getPath2Root(ParentTreeNode node) { 30 | ArrayList path = new ArrayList<>(); 31 | while (node != null) { 32 | path.add(node); 33 | node = node.parent; 34 | } 35 | return path; 36 | } 37 | } -------------------------------------------------------------------------------- /java/MaximalSquare.java: -------------------------------------------------------------------------------- 1 | 2 | public class Solution { 3 | /** 4 | * @param matrix: a matrix of 0 and 1 5 | * @return: an integer 6 | */ 7 | public int maxSquare(int[][] matrix) { 8 | // write your code here 9 | int ans = 0; 10 | int n = matrix.length; 11 | int m; 12 | if(n > 0) 13 | m = matrix[0].length; 14 | else 15 | return ans; 16 | int [][]res = new int [n][m]; 17 | for(int i = 0; i < n; i++){ 18 | res[i][0] = matrix[i][0]; 19 | ans = Math.max(res[i][0] , ans); 20 | for(int j = 1; j < m; j++) { 21 | if(i > 0) { 22 | if(matrix[i][j] > 0) { 23 | res[i][j] = Math.min(res[i - 1][j],Math.min(res[i][j-1], res[i-1][j-1])) + 1; 24 | } else { 25 | res[i][j] = 0; 26 | } 27 | 28 | } 29 | else { 30 | res[i][j] = matrix[i][j]; 31 | } 32 | ans = Math.max(res[i][j], ans); 33 | } 34 | } 35 | return ans*ans; 36 | } 37 | } -------------------------------------------------------------------------------- /java/MaximalSquare2.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param matrix a matrix of 0 and 1 4 | * @return an integer 5 | */ 6 | public int maxSquare2(int[][] matrix) { 7 | // write your code here 8 | int n = matrix.length; 9 | if (n == 0) 10 | return 0; 11 | 12 | int m = matrix[0].length; 13 | if (m == 0) 14 | return 0; 15 | 16 | int[][] f = new int[n][m]; 17 | int[][] u = new int[n][m]; 18 | int[][] l = new int[n][m]; 19 | 20 | int length = 0; 21 | for (int i = 0; i < n; ++i) 22 | for (int j = 0; j < m; ++j) { 23 | if (matrix[i][j] == 0) { 24 | f[i][j] = 0; 25 | u[i][j] = l[i][j] = 1; 26 | if (i > 0) 27 | u[i][j] = u[i - 1][j] + 1; 28 | if (j > 0) 29 | l[i][j] = l[i][j - 1] + 1; 30 | } else { 31 | u[i][j] = l[i][j] = 0; 32 | if (i > 0 && j > 0) 33 | f[i][j] = Math.min(f[i - 1][j - 1], Math.min(u[i - 1][j], l[i][j - 1])) + 1; 34 | else 35 | f[i][j] = 1; 36 | } 37 | length = Math.max(length, f[i][j]); 38 | } 39 | return length * length; 40 | } 41 | } -------------------------------------------------------------------------------- /java/MaximumAverageSubarray.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param nums: an array 4 | * @param k: an integer 5 | * @return: the maximum average value 6 | */ 7 | public double findMaxAverage(int[] nums, int k) { 8 | // Write your code here 9 | int Max=Integer.MIN_VALUE; 10 | int sum=0,start=0,end=k-1; 11 | while(endMax) 16 | Max=sum; 17 | start++; 18 | end++; 19 | } 20 | return (double)Max/k; 21 | } 22 | } -------------------------------------------------------------------------------- /java/MaximumAverageSubarray2.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param nums an array with positive and negative numbers 4 | * @param k an integer 5 | * @return the maximum average 6 | */ 7 | public double maxAverage(int[] nums, int k) { 8 | // Write your code here 9 | double l = -1e12; 10 | double r = 1e12; 11 | double eps = 1e-6; 12 | 13 | while (l + eps < r) { 14 | double mid = l + (r - l) / 2; 15 | 16 | if (check(nums, mid, k)) { 17 | l = mid; 18 | } else { 19 | r = mid; 20 | } 21 | } 22 | return l; 23 | } 24 | 25 | boolean check(int nums[], double avg, int k) { 26 | double[] sum = new double[nums.length + 1]; 27 | double[] min_pre = new double[nums.length + 1]; 28 | 29 | for (int i = 1; i <= nums.length; i++) { 30 | sum[i] = sum[i - 1] + (nums[i - 1] - avg); 31 | min_pre[i] = Math.min(min_pre[i - 1], sum[i]); 32 | 33 | if (i >= k && sum[i] - min_pre[i - k] >= 0) { 34 | return true; 35 | } 36 | } 37 | return false; 38 | } 39 | } -------------------------------------------------------------------------------- /java/MaximumDepthofBinaryTree.java: -------------------------------------------------------------------------------- 1 | // Version 1: Divide Conquer 2 | public class Solution { 3 | public int maxDepth(TreeNode root) { 4 | if (root == null) { 5 | return 0; 6 | } 7 | 8 | int left = maxDepth(root.left); 9 | int right = maxDepth(root.right); 10 | return Math.max(left, right) + 1; 11 | } 12 | } 13 | 14 | // version 2: Traverse 15 | /** 16 | * Definition of TreeNode: 17 | * public class TreeNode { 18 | * public int val; 19 | * public TreeNode left, right; 20 | * public TreeNode(int val) { 21 | * this.val = val; 22 | * this.left = this.right = null; 23 | * } 24 | * } 25 | */ 26 | public class Solution { 27 | /** 28 | * @param root: The root of binary tree. 29 | * @return: An integer. 30 | */ 31 | private int depth; 32 | 33 | public int maxDepth(TreeNode root) { 34 | depth = 0; 35 | helper(root, 1); 36 | 37 | return depth; 38 | } 39 | 40 | private void helper(TreeNode node, int curtDepth) { 41 | if (node == null) { 42 | return; 43 | } 44 | 45 | if (curtDepth > depth) { 46 | depth = curtDepth; 47 | } 48 | 49 | helper(node.left, curtDepth + 1); 50 | helper(node.right, curtDepth + 1); 51 | } 52 | } -------------------------------------------------------------------------------- /java/MaximumProductSubarray.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param nums: an array of integers 4 | * @return: an integer 5 | */ 6 | public int maxProduct(List nums) { 7 | int[] max = new int[nums.size()]; 8 | int[] min = new int[nums.size()]; 9 | 10 | min[0] = max[0] = nums.get(0); 11 | int result = nums.get(0); 12 | for (int i = 1; i < nums.size(); i++) { 13 | min[i] = max[i] = nums.get(i); 14 | if (nums.get(i) > 0) { 15 | max[i] = Math.max(max[i], max[i - 1] * nums.get(i)); 16 | min[i] = Math.min(min[i], min[i - 1] * nums.get(i)); 17 | } else if (nums.get(i) < 0) { 18 | max[i] = Math.max(max[i], min[i - 1] * nums.get(i)); 19 | min[i] = Math.min(min[i], max[i - 1] * nums.get(i)); 20 | } 21 | 22 | result = Math.max(result, max[i]); 23 | } 24 | 25 | return result; 26 | } 27 | } -------------------------------------------------------------------------------- /java/MaximumSubarray.java: -------------------------------------------------------------------------------- 1 | // Version 1: Greedy 2 | 3 | public class Solution { 4 | public int maxSubArray(int[] A) { 5 | if (A == null || A.length == 0){ 6 | return 0; 7 | } 8 | 9 | int max = Integer.MIN_VALUE, sum = 0; 10 | for (int i = 0; i < A.length; i++) { 11 | sum += A[i]; 12 | max = Math.max(max, sum); 13 | sum = Math.max(sum, 0); 14 | } 15 | 16 | return max; 17 | } 18 | } 19 | 20 | // Version 2: Prefix Sum 21 | 22 | public class Solution { 23 | public int maxSubArray(int[] A) { 24 | if (A == null || A.length == 0){ 25 | return 0; 26 | } 27 | 28 | int max = Integer.MIN_VALUE, sum = 0, minSum = 0; 29 | for (int i = 0; i < A.length; i++) { 30 | sum += A[i]; 31 | max = Math.max(max, sum - minSum); 32 | minSum = Math.min(minSum, sum); 33 | } 34 | 35 | return max; 36 | } 37 | } -------------------------------------------------------------------------------- /java/MaximumSubarray2.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param nums: A list of integers 4 | * @return: An integer denotes the sum of max two non-overlapping subarrays 5 | */ 6 | public int maxTwoSubArrays(ArrayList nums) { 7 | // write your code 8 | int size = nums.size(); 9 | int[] left = new int[size]; 10 | int[] right = new int[size]; 11 | int sum = 0; 12 | int minSum = 0; 13 | int max = Integer.MIN_VALUE; 14 | for(int i = 0; i < size; i++){ 15 | sum += nums.get(i); 16 | max = Math.max(max, sum - minSum); 17 | minSum = Math.min(sum, minSum); 18 | left[i] = max; 19 | } 20 | sum = 0; 21 | minSum = 0; 22 | max = Integer.MIN_VALUE; 23 | for(int i = size - 1; i >= 0; i--){ 24 | sum += nums.get(i); 25 | max = Math.max(max, sum - minSum); 26 | minSum = Math.min(sum, minSum); 27 | right[i] = max; 28 | } 29 | max = Integer.MIN_VALUE; 30 | for(int i = 0; i < size - 1; i++){ 31 | max = Math.max(max, left[i] + right[i + 1]); 32 | } 33 | return max; 34 | } 35 | } -------------------------------------------------------------------------------- /java/MaximumSubarray4.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param nums an array of integers 4 | * @param k an integer 5 | * @return the largest sum 6 | */ 7 | public int maxSubarray4(int[] nums, int k) { 8 | // Write your code here 9 | int n = nums.length; 10 | if (n < k) 11 | return 0; 12 | 13 | int result = 0; 14 | for (int i = 0; i < k; ++i) 15 | result += nums[i]; 16 | 17 | int[] sum = new int[n + 1]; 18 | sum[0] = 0; 19 | 20 | int min_prefix = 0; 21 | for (int i = 1; i <= n; ++i) { 22 | sum[i] = sum[i - 1] + nums[i - 1]; 23 | if (i >= k && sum[i] - min_prefix > result) { 24 | result = Math.max(result, sum[i] - min_prefix); 25 | } 26 | if (i >= k) { 27 | min_prefix = Math.min(min_prefix, sum[i - k + 1]); 28 | } 29 | } 30 | return result; 31 | } 32 | } -------------------------------------------------------------------------------- /java/MaximumSubarray5.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param nums an array of integers 4 | * @param k1 an integer 5 | * @param k2 an integer 6 | * @return the largest sum 7 | */ 8 | public int maxSubarray5(int[] nums, int k1, int k2) { 9 | // Write your code here 10 | int n = nums.length; 11 | if (n < k1) 12 | return 0; 13 | 14 | int result = Integer.MIN_VALUE; 15 | 16 | int[] sum = new int[n + 1]; 17 | sum[0] = 0; 18 | LinkedList queue = new LinkedList(); 19 | 20 | for (int i = 1; i <= n; ++i) { 21 | sum[i] = sum[i - 1] + nums[i - 1]; 22 | 23 | if (!queue.isEmpty() && queue.getFirst() < i - k2) { 24 | queue.removeFirst(); 25 | } 26 | if (i >= k1) { 27 | while (!queue.isEmpty() && sum[queue.getLast()] > sum[i - k1]) { 28 | queue.removeLast(); 29 | } 30 | queue.add(i - k1); 31 | } 32 | 33 | // [i - k2, i - k1] 34 | if (!queue.isEmpty() && sum[i] - sum[queue.getFirst()] > result) { 35 | result = Math.max(result, sum[i] - sum[queue.getFirst()]); 36 | } 37 | 38 | 39 | } 40 | return result; 41 | } 42 | } -------------------------------------------------------------------------------- /java/MergeSortedArray.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | /** 3 | * @param A: sorted integer array A which has m elements, 4 | * but size of A is m+n 5 | * @param B: sorted integer array B which has n elements 6 | * @return: void 7 | */ 8 | public void mergeSortedArray(int[] A, int m, int[] B, int n) { 9 | int i = m-1, j = n-1, index = m + n - 1; 10 | while (i >= 0 && j >= 0) { 11 | if (A[i] > B[j]) { 12 | A[index--] = A[i--]; 13 | } else { 14 | A[index--] = B[j--]; 15 | } 16 | } 17 | while (i >= 0) { 18 | A[index--] = A[i--]; 19 | } 20 | while (j >= 0) { 21 | A[index--] = B[j--]; 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /java/MergeTwoSortedArrays.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param A: sorted integer array A 4 | * @param B: sorted integer array B 5 | * @return: A new sorted integer array 6 | */ 7 | public int[] mergeSortedArray(int[] A, int[] B) { 8 | // write your code here 9 | int len = A.length + B.length; 10 | int[] result = new int[len]; 11 | if (A == null || A.length == 0) { 12 | return B; 13 | } 14 | 15 | if (B == null || B.length == 0) { 16 | return A; 17 | } 18 | int i = 0, j = 0, index = 0; 19 | while (i < A.length && j < B.length) { 20 | if (A[i] <= B[j]) { 21 | result[index++] = A[i++]; 22 | } else { 23 | result[index++] = B[j++]; 24 | } 25 | } 26 | 27 | while (i < A.length) { 28 | result[index++] = A[i++]; 29 | } 30 | 31 | while (j < B.length) { 32 | result[index++] = B[j++]; 33 | } 34 | 35 | return result; 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /java/MiddleofLinkedList.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param head: the head of linked list. 4 | * @return: a middle node of the linked list 5 | */ 6 | public ListNode middleNode(ListNode head) { 7 | if (head == null || head.next == null) { 8 | return head; 9 | } 10 | 11 | ListNode slow = head, fast = head.next; 12 | while (fast != null && fast.next != null) { 13 | slow = slow.next; 14 | fast = fast.next.next; 15 | } 16 | 17 | return slow; 18 | } 19 | } -------------------------------------------------------------------------------- /java/MinStack.java: -------------------------------------------------------------------------------- 1 | // version 1: 2 | public class MinStack { 3 | private Stack stack; 4 | private Stack minStack; 5 | 6 | public MinStack() { 7 | stack = new Stack(); 8 | minStack = new Stack(); 9 | } 10 | 11 | public void push(int number) { 12 | stack.push(number); 13 | if (minStack.isEmpty()) { 14 | minStack.push(number); 15 | } else { 16 | minStack.push(Math.min(number, minStack.peek())); 17 | } 18 | } 19 | 20 | public int pop() { 21 | minStack.pop(); 22 | return stack.pop(); 23 | } 24 | 25 | public int min() { 26 | return minStack.peek(); 27 | } 28 | } 29 | 30 | // version 2, save more space. but space complexity doesn't change. 31 | public class MinStack { 32 | private Stack stack; 33 | private Stack minStack; 34 | 35 | public MinStack() { 36 | stack = new Stack(); 37 | minStack = new Stack(); 38 | } 39 | 40 | public void push(int number) { 41 | stack.push(number); 42 | if (minStack.empty() == true) 43 | minStack.push(number); 44 | else { 45 | // 这里考虑的相等的情况也会继续push 46 | if (minStack.peek() >= number) 47 | minStack.push(number); 48 | } 49 | } 50 | 51 | public int pop() { 52 | if (stack.peek().equals(minStack.peek()) ) 53 | minStack.pop(); 54 | return stack.pop(); 55 | } 56 | 57 | public int min() { 58 | return minStack.peek(); 59 | } 60 | } -------------------------------------------------------------------------------- /java/MinimumAdjustmentCost.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param A: An integer array. 4 | * @param target: An integer. 5 | */ 6 | public int MinAdjustmentCost(ArrayList A, int target) { 7 | // write your code here 8 | int n = A.size(); 9 | int[][] f = new int[n + 1][101]; 10 | for (int i = 0; i <= n ; ++i) 11 | for (int j = 0; j <=100; ++j) 12 | f[i][j] = Integer.MAX_VALUE; 13 | for (int i = 0; i <= 100; ++i) 14 | f[0][i] = 0; 15 | for (int i = 1; i <=n; ++i) 16 | for (int j = 0; j <= 100;++j) 17 | if (f[i-1][j] != Integer.MAX_VALUE) 18 | for (int k = 0; k <= 100; ++k) 19 | if (Math.abs(j-k) <= target) 20 | if (f[i][k] > f[i-1][j] + Math.abs(A.get(i-1)-k)) 21 | f[i][k] = f[i-1][j] + Math.abs(A.get(i-1)-k); 22 | int ans = Integer.MAX_VALUE; 23 | for (int i = 0; i <= 100; ++i) 24 | if (f[n][i] < ans) 25 | ans = f[n][i]; 26 | return ans; 27 | } 28 | } -------------------------------------------------------------------------------- /java/MinimumDepthofBinaryTreewithMaximumAverage.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/java/MinimumDepthofBinaryTreewithMaximumAverage.java -------------------------------------------------------------------------------- /java/MoveZeroes.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param nums an integer array 4 | * @return nothing, do this in-place 5 | */ 6 | public void moveZeroes(int[] nums) { 7 | // Write your code here 8 | int left = 0, right = 0; 9 | while (right < nums.length) { 10 | if (nums[right] != 0) { 11 | int temp = nums[left]; 12 | nums[left] = nums[right]; 13 | nums[right] = temp; 14 | left++; 15 | } 16 | right++; 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /java/NestedListWeightSum2.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/java/NestedListWeightSum2.java -------------------------------------------------------------------------------- /java/NumberOfIslands2.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/java/NumberOfIslands2.java -------------------------------------------------------------------------------- /java/PancakeSorting.java: -------------------------------------------------------------------------------- 1 | /** 2 | * public class FlipTool { 3 | * static public void flip(int[] arr, int i) { 4 | * ... 5 | * } 6 | * } 7 | */ 8 | public class Solution { 9 | /** 10 | * @param array: an integer array 11 | * @return: nothing 12 | */ 13 | public void pancakeSort(int[] array) { 14 | // Write your code here 15 | int n = array.length; 16 | while(n > 1){ 17 | for(int i = 0; i < n - 1; i++){ 18 | if(array[0] < array[i + 1]){ 19 | FlipTool.flip(array, i + 1); 20 | } 21 | } 22 | FlipTool.flip(array, n - 1); 23 | n--; 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /java/PartitionList.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public ListNode partition(ListNode head, int x) { 3 | if (head == null) { 4 | return null; 5 | } 6 | 7 | ListNode leftDummy = new ListNode(0); 8 | ListNode rightDummy = new ListNode(0); 9 | ListNode left = leftDummy, right = rightDummy; 10 | 11 | while (head != null) { 12 | if (head.val < x) { 13 | left.next = head; 14 | left = head; 15 | } else { 16 | right.next = head; 17 | right = head; 18 | } 19 | head = head.next; 20 | } 21 | 22 | right.next = null; 23 | left.next = rightDummy.next; 24 | return leftDummy.next; 25 | } 26 | } -------------------------------------------------------------------------------- /java/PermutationIndex.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param A an integer array 4 | * @return a long integer 5 | */ 6 | long fac(int numerator) { 7 | long now = 1; 8 | for (int i = 1; i <= numerator; i++) { 9 | now *= (long) i; 10 | } 11 | return now; 12 | } 13 | 14 | long generateNum(HashMap hash) { 15 | long denominator = 1; 16 | int sum = 0; 17 | for (int val : hash.values()) { 18 | if(val == 0 ) 19 | continue; 20 | denominator *= fac(val); 21 | sum += val; 22 | } 23 | if(sum==0) { 24 | return sum; 25 | } 26 | return fac(sum) / denominator; 27 | } 28 | 29 | public long permutationIndex(int[] A) { 30 | HashMap hash = new HashMap(); 31 | for (int i = 0; i < A.length; i++) { 32 | if (hash.containsKey(A[i])) 33 | hash.put(A[i], hash.get(A[i]) + 1); 34 | else { 35 | hash.put(A[i], 1); 36 | } 37 | } 38 | long ans = 0; 39 | for (int i = 0; i < A.length; i++) { 40 | for (int j = i + 1; j < A.length; j++) { 41 | if (A[j] < A[i]) { 42 | hash.put(A[j], hash.get(A[j]) - 1); 43 | ans += generateNum(hash); 44 | hash.put(A[j], hash.get(A[j]) + 1); 45 | } 46 | } 47 | hash.put(A[i], hash.get(A[i])-1); 48 | } 49 | return ans+1; 50 | } 51 | } -------------------------------------------------------------------------------- /java/PostOfficeProblem.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param A an integer array 4 | * @param k an integer 5 | * @return an integer 6 | */ 7 | int [][]init(int []A) 8 | { 9 | int n = A.length; 10 | int [][]dis = new int [n+1][n+1]; 11 | for(int i = 1; i <= n; i++) { 12 | for(int j = i+1 ;j <= n;++j) 13 | { 14 | int mid = (i+j) /2; 15 | for(int k = i;k <= j;++k) 16 | dis[i][j] += Math.abs(A[k - 1] - A[mid - 1]); 17 | } 18 | } 19 | return dis; 20 | } 21 | 22 | public int postOffice(int[] A, int k) { 23 | // Write your code here 24 | int n = A.length; 25 | Arrays.sort(A); 26 | 27 | int [][]dis = init(A); 28 | int [][]dp = new int[n + 1][k + 1]; 29 | if(n == 0 || k >= A.length) 30 | return 0; 31 | int ans = Integer.MAX_VALUE; 32 | for(int i = 0;i <= n;++i) { 33 | dp[i][1] = dis[1][i]; 34 | 35 | } 36 | 37 | 38 | for(int nk = 2; nk <= k; nk++) { 39 | 40 | for(int i = nk; i <= n; i++) { 41 | dp[i][nk] = Integer.MAX_VALUE; 42 | for(int j = 0; j < i; j++) { 43 | if(dp[i][nk] == Integer.MAX_VALUE || dp[i][nk] > dp[j][nk-1] + dis[j+1][i]) 44 | dp[i][nk] = dp[j][nk-1] + dis[j+1][i]; 45 | } 46 | } 47 | } 48 | return dp[n][k]; 49 | } 50 | } -------------------------------------------------------------------------------- /java/Pow.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param x the base number 4 | * @param n the power number 5 | * @return the result 6 | */ 7 | public double myPow(double x, int n) { 8 | // Write your code here 9 | boolean isNegative = false; 10 | if (n < 0) { 11 | x = 1 / x; 12 | isNegative = true; 13 | n = -(n + 1); // Avoid overflow when n == MIN_VALUE 14 | } 15 | 16 | double ans = 1, tmp = x; 17 | 18 | while (n != 0) { 19 | if (n % 2 == 1) { 20 | ans *= tmp; 21 | } 22 | tmp *= tmp; 23 | n /= 2; 24 | } 25 | 26 | if (isNegative) { 27 | ans *= x; 28 | } 29 | return ans; 30 | } 31 | } -------------------------------------------------------------------------------- /java/PreviousPermutation.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class Solution { 4 | /** 5 | * @param nums: A list of integers 6 | * @return: A list of integers that's previous permuation 7 | */ 8 | public void swapItem(ArrayList nums, int i, int j) { 9 | Integer tmp = nums.get(i); 10 | nums.set(i, nums.get(j)); 11 | nums.set(j, tmp); 12 | } 13 | public void swapList(ArrayList nums, int i, int j) { 14 | while ( i < j) { 15 | swapItem(nums, i, j); 16 | i ++; j --; 17 | } 18 | } 19 | public ArrayList previousPermuation(ArrayList nums) { 20 | int len = nums.size(); 21 | if ( len <= 1) 22 | return nums; 23 | int i = len - 1; 24 | while ( i > 0 && nums.get(i) >= nums.get(i-1) ) 25 | i --; 26 | swapList(nums, i, len - 1); 27 | if ( i != 0) { 28 | int j = i; 29 | while ( nums.get(j) >= nums.get(i-1) ) j++; 30 | swapItem(nums, j, i-1); 31 | } 32 | 33 | return nums; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /java/PreviousPermutation2.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/java/PreviousPermutation2.java -------------------------------------------------------------------------------- /java/Rehashing.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for ListNode 3 | * public class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode(int x) { 7 | * val = x; 8 | * next = null; 9 | * } 10 | * } 11 | */ 12 | public class Solution { 13 | /** 14 | * @param hashTable: A list of The first node of linked list 15 | * @return: A list of The first node of linked list which have twice size 16 | */ 17 | public ListNode[] rehashing(ListNode[] hashTable) { 18 | // write your code here 19 | if (hashTable.length <= 0) { 20 | return hashTable; 21 | } 22 | int newcapacity = 2 * hashTable.length; 23 | ListNode[] newTable = new ListNode[newcapacity]; 24 | for (int i = 0; i < hashTable.length; i++) { 25 | while (hashTable[i] != null) { 26 | int newindex 27 | = (hashTable[i].val % newcapacity + newcapacity) % newcapacity; 28 | if (newTable[newindex] == null) { 29 | newTable[newindex] = new ListNode(hashTable[i].val); 30 | // newTable[newindex].next = null; 31 | } else { 32 | ListNode dummy = newTable[newindex]; 33 | while (dummy.next != null) { 34 | dummy = dummy.next; 35 | } 36 | dummy.next = new ListNode(hashTable[i].val); 37 | } 38 | hashTable[i] = hashTable[i].next; 39 | } 40 | } 41 | return newTable; 42 | } 43 | } -------------------------------------------------------------------------------- /java/RemoveDuplicatesfromSortedArray.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int removeDuplicates(int[] nums) { 3 | if (nums == null || nums.length == 0) { 4 | return -1; 5 | } 6 | int i = 0; 7 | for (int n = 0; n < nums.length; n++) { 8 | if (nums[n] != nums[i]) { 9 | nums[++i] = nums[n]; 10 | } 11 | } 12 | return i + 1; 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /java/RemoveSubstrings.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param s a string 4 | * @param dict a set of n substrings 5 | * @return the minimum length 6 | */ 7 | public int minLength(String s, Set dict) { 8 | // Write your code here 9 | Queue que = new LinkedList(); 10 | Set hash = new HashSet(); 11 | 12 | int min = s.length(); 13 | que.offer(s); 14 | hash.add(s); 15 | 16 | while (!que.isEmpty()) { 17 | s = que.poll(); 18 | for (String sub : dict) { 19 | int found = s.indexOf(sub); 20 | while (found != -1) { 21 | String new_s = s.substring(0, found) + 22 | s.substring(found + sub.length(), s.length()); 23 | if (!hash.contains(new_s)) { 24 | if (new_s.length() < min) 25 | min = new_s.length(); 26 | que.offer(new_s); 27 | hash.add(new_s); 28 | } 29 | found = s.indexOf(sub, found + 1); 30 | } 31 | } 32 | } 33 | return min; 34 | } 35 | } -------------------------------------------------------------------------------- /java/SearchGraphNodes.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param graph a list of Undirected graph node 4 | * @param values a hash mapping, 5 | * @param node an Undirected graph node 6 | * @param target an integer 7 | * @return the a node 8 | */ 9 | public UndirectedGraphNode searchNode(ArrayList graph, 10 | Map values, 11 | UndirectedGraphNode node, 12 | int target) { 13 | // Write your code here 14 | Queue queue = new LinkedList(); 15 | Set hash = new HashSet(); 16 | 17 | queue.offer(node); 18 | hash.add(node); 19 | 20 | while (!queue.isEmpty()) { 21 | UndirectedGraphNode head = queue.poll(); 22 | if (values.get(head) == target) { 23 | return head; 24 | } 25 | for (UndirectedGraphNode nei : head.neighbors) { 26 | if (!hash.contains(nei)){ 27 | queue.offer(nei); 28 | hash.add(nei); 29 | } 30 | } 31 | } 32 | return null; 33 | } 34 | } -------------------------------------------------------------------------------- /java/SearchinRotatedSortedArray2.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // 这个问题在面试中不会让实现完整程序 3 | // 只需要举出能够最坏情况的数据是 [1,1,1,1... 1] 里有一个0即可。 4 | // 在这种情况下是无法使用二分法的,复杂度是O(n) 5 | // 因此写个for循环最坏也是O(n),那就写个for循环就好了 6 | // 如果你觉得,不是每个情况都是最坏情况,你想用二分法解决不是最坏情况的情况,那你就写一个二分吧。 7 | // 反正面试考的不是你在这个题上会不会用二分法。这个题的考点是你想不想得到最坏情况。 8 | public boolean search(int[] A, int target) { 9 | for (int i = 0; i < A.length; i ++) { 10 | if (A[i] == target) { 11 | return true; 12 | } 13 | } 14 | return false; 15 | } 16 | } -------------------------------------------------------------------------------- /java/SlidingWindowMatrixMaximum.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param matrix an integer array of n * m matrix 4 | * @param k an integer 5 | * @return the maximum number 6 | */ 7 | public int maxSlidingWindow2(int[][] matrix, int k) { 8 | // Write your code here 9 | int n = matrix.length; 10 | if (n == 0 || n < k) 11 | return 0; 12 | int m = matrix[0].length; 13 | if (m == 0 || m < k) 14 | return 0; 15 | 16 | int[][] sum = new int[n + 1][m + 1]; 17 | for (int i = 0; i <= n; ++i) sum[i][0] = 0; 18 | for (int i = 0; i <= m; ++i) sum[0][i] = 0; 19 | 20 | for (int i = 1; i <= n; ++i) 21 | for (int j = 1; j <= m; ++j) 22 | sum[i][j] = matrix[i - 1][j - 1] + 23 | sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1]; 24 | 25 | int max_value = Integer.MIN_VALUE; 26 | for (int i = k; i <= n; ++i) 27 | for (int j = k; j <= m; ++j) { 28 | int value = sum[i][j] - sum[i - k][j] - 29 | sum[i][j - k] + sum[i - k][j - k]; 30 | 31 | if (value > max_value) 32 | max_value = value; 33 | } 34 | return max_value; 35 | } 36 | } -------------------------------------------------------------------------------- /java/SlidingWindowMaximum.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param matrix an integer array of n * m matrix 4 | * @param k an integer 5 | * @return the maximum number 6 | */ 7 | public int maxSlidingWindow2(int[][] matrix, int k) { 8 | // Write your code here 9 | int n = matrix.length; 10 | if (n == 0 || n < k) 11 | return 0; 12 | int m = matrix[0].length; 13 | if (m == 0 || m < k) 14 | return 0; 15 | 16 | int[][] sum = new int[n + 1][m + 1]; 17 | for (int i = 0; i <= n; ++i) sum[i][0] = 0; 18 | for (int i = 0; i <= m; ++i) sum[0][i] = 0; 19 | 20 | for (int i = 1; i <= n; ++i) 21 | for (int j = 1; j <= m; ++j) 22 | sum[i][j] = matrix[i - 1][j - 1] + 23 | sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1]; 24 | 25 | int max_value = Integer.MIN_VALUE; 26 | for (int i = k; i <= n; ++i) 27 | for (int j = k; j <= m; ++j) { 28 | int value = sum[i][j] - sum[i - k][j] - 29 | sum[i][j - k] + sum[i - k][j - k]; 30 | 31 | if (value > max_value) 32 | max_value = value; 33 | } 34 | return max_value; 35 | } 36 | } -------------------------------------------------------------------------------- /java/SortIntegers2.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param A an integer array 4 | * @return void 5 | */ 6 | public void sortIntegers2(int[] A) { 7 | quickSort(A, 0, A.length - 1); 8 | } 9 | 10 | private void quickSort(int[] A, int start, int end) { 11 | if (start >= end) { 12 | return; 13 | } 14 | 15 | int left = start, right = end; 16 | // key point 1: pivot is the value, not the index 17 | int pivot = A[(start + end) / 2]; 18 | 19 | // key point 2: every time you compare left & right, it should be 20 | // left <= right not left < right 21 | while (left <= right) { 22 | while (left <= right && A[left] < pivot) { 23 | left++; 24 | } 25 | while (left <= right && A[right] > pivot) { 26 | right--; 27 | } 28 | if (left <= right) { 29 | int temp = A[left]; 30 | A[left] = A[right]; 31 | A[right] = temp; 32 | 33 | left++; 34 | right--; 35 | } 36 | } 37 | 38 | quickSort(A, start, right); 39 | quickSort(A, left, end); 40 | } 41 | } -------------------------------------------------------------------------------- /java/Sqrt.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | /** 3 | * @param x: An integer 4 | * @return: The sqrt of x 5 | */ 6 | public int sqrt(int x) { 7 | // find the last number which square of it <= x 8 | long start = 1, end = x; 9 | while (start + 1 < end) { 10 | long mid = start + (end - start) / 2; 11 | if (mid * mid <= x) { 12 | start = mid; 13 | } else { 14 | end = mid; 15 | } 16 | } 17 | 18 | if (end * end <= x) { 19 | return (int) end; 20 | } 21 | return (int) start; 22 | } 23 | } -------------------------------------------------------------------------------- /java/Sqrt2.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | public double sqrt(double x) { 4 | double start = 0, end = Math.max(1, x); // note: end is not x 5 | double eps = 1e-10; 6 | while(start + eps < end) { 7 | double mid = (start + end) / 2; 8 | if(mid * mid > x) { 9 | end = mid; 10 | } else { 11 | start = mid; 12 | } 13 | } 14 | return start; 15 | } 16 | } -------------------------------------------------------------------------------- /java/StoneGame2.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param A an integer array 4 | * @return an integer 5 | */ 6 | public int stoneGame2(int[] A) { 7 | // Write your code here 8 | int n = A.length; 9 | if (n <= 1) 10 | return 0; 11 | 12 | int[][] dp = new int[2 * n][2 * n]; 13 | 14 | int[] sum = new int[2 * n + 1]; 15 | 16 | for (int i = 1; i <= 2 * n; ++i) { 17 | sum[i] = sum[i - 1] + A[(i - 1) % n]; 18 | } 19 | 20 | for (int i = 0; i < 2 * n; ++i) { 21 | dp[i][i] = 0; 22 | } 23 | 24 | for(int len = 2; len <= 2 * n; ++len) 25 | for(int i= 0;i < 2 * n && i + len - 1 < 2 * n; ++i) { 26 | int j = i + len - 1; 27 | dp[i][j] = Integer.MAX_VALUE; 28 | for (int k = i; k < j; ++k) { 29 | if (dp[i][k] + dp[k+1][j] + sum[j + 1] - sum[i] < dp[i][j]) 30 | dp[i][j] = dp[i][k] + dp[k+1][j] + sum[j + 1] - sum[i]; 31 | } 32 | } 33 | 34 | int ans = Integer.MAX_VALUE; 35 | for (int i = 0; i < n; ++i) 36 | if (dp[i][i + n - 1] < ans) 37 | ans = dp[i][i + n - 1]; 38 | return ans; 39 | 40 | } 41 | } -------------------------------------------------------------------------------- /java/StringPermutation.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param A a string 4 | * @param B a string 5 | * @return a boolean 6 | */ 7 | public boolean stringPermutation(String A, String B) { 8 | // Write your code here 9 | int[] cnt = new int[1000]; 10 | for (int i = 0; i < A.length(); ++i) 11 | cnt[(int)A.charAt(i)] += 1; 12 | for (int i = 0; i < B.length(); ++i) 13 | cnt[(int)B.charAt(i)] -= 1; 14 | for (int i = 0; i < 1000; ++i) 15 | if (cnt[i] != 0) 16 | return false; 17 | return true; 18 | } 19 | } -------------------------------------------------------------------------------- /java/StringPermutation2.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param str a string 4 | * @return all permutations 5 | */ 6 | public List stringPermutation2(String str) { 7 | // Write your code here 8 | List result = new ArrayList(); 9 | char[] s = str.toCharArray(); 10 | Arrays.sort(s); 11 | result.add(String.valueOf(s)); 12 | while ((s = nextPermutation(s)) != null) { 13 | result.add(String.valueOf(s)); 14 | } 15 | return result; 16 | } 17 | 18 | public char[] nextPermutation(char[] nums) { 19 | int index = -1; 20 | for(int i = nums.length -1; i > 0; i--){ 21 | if(nums[i] > nums[i-1]){ 22 | index = i-1; 23 | break; 24 | } 25 | } 26 | if(index == -1){ 27 | return null; 28 | } 29 | for(int i = nums.length -1; i > index; i--){ 30 | if(nums[i] > nums[index]){ 31 | char temp = nums[i]; 32 | nums[i] = nums[index]; 33 | nums[index] = temp; 34 | break; 35 | } 36 | } 37 | reverse(nums,index+1,nums.length-1); 38 | return nums; 39 | 40 | } 41 | 42 | public void reverse(char[] num, int start, int end) { 43 | for (int i = start, j = end; i < j; i++, j--) { 44 | char temp = num[i]; 45 | num[i] = num[j]; 46 | num[j] = temp; 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /java/SubarraySum.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param nums: A list of integers 4 | * @return: A list of integers includes the index of the first number 5 | * and the index of the last number 6 | */ 7 | public ArrayList subarraySum(int[] nums) { 8 | // write your code here 9 | 10 | int len = nums.length; 11 | 12 | ArrayList ans = new ArrayList(); 13 | HashMap map = new HashMap(); 14 | 15 | map.put(0, -1); 16 | 17 | int sum = 0; 18 | for (int i = 0; i < len; i++) { 19 | sum += nums[i]; 20 | 21 | if (map.containsKey(sum)) { 22 | ans.add(map.get(sum) + 1); 23 | ans.add(i); 24 | return ans; 25 | } 26 | 27 | map.put(sum, i); 28 | } 29 | 30 | return ans; 31 | } 32 | } -------------------------------------------------------------------------------- /java/SubarraySum2.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param A an integer array 4 | * @param start an integer 5 | * @param end an integer 6 | * @return the number of possible answer 7 | */ 8 | int find(int[] A, int len, int value) { 9 | if (A[len-1] < value ) 10 | return len; 11 | 12 | int l = 0, r = len-1, ans = 0; 13 | while (l <= r) { 14 | int mid = (l + r) / 2; 15 | if (value <= A[mid]) { 16 | ans = mid; 17 | r = mid - 1; 18 | } else 19 | l = mid + 1; 20 | } 21 | return ans; 22 | } 23 | 24 | public int subarraySumII(int[] A, int start, int end) { 25 | // Write your code here 26 | int len = A.length; 27 | for (int i = 1; i = start && A[i] <= end) 33 | cnt ++; 34 | int l = A[i] - end; 35 | int r = A[i] - start; 36 | cnt += find(A, len, r+1) - find(A, len, l); 37 | } 38 | return cnt; 39 | } 40 | } -------------------------------------------------------------------------------- /java/SubmatrixSum.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param matrix an integer matrix 4 | * @return the coordinate of the left-up and right-down number 5 | */ 6 | public int[][] submatrixSum(int[][] matrix) { 7 | int[][] result = new int[2][2]; 8 | int M = matrix.length; 9 | if (M == 0) return result; 10 | int N = matrix[0].length; 11 | if (N == 0) return result; 12 | // pre-compute: sum[i][j] = sum of submatrix [(0, 0), (i, j)] 13 | int[][] sum = new int[M+1][N+1]; 14 | for (int j=0; j<=N; ++j) sum[0][j] = 0; 15 | for (int i=1; i<=M; ++i) sum[i][0] = 0; 16 | for (int i=0; i map = new HashMap(); 23 | for (int j=0; j<=N; ++j) { 24 | int diff = sum[h][j] - sum[l][j]; 25 | if (map.containsKey(diff)) { 26 | int k = map.get(diff); 27 | result[0][0] = l; result[0][1] = k; 28 | result[1][0] = h-1; result[1][1] = j-1; 29 | return result; 30 | } else { 31 | map.put(diff, j); 32 | } 33 | } 34 | } 35 | } 36 | return result; 37 | } 38 | } -------------------------------------------------------------------------------- /java/TheSkylineProblem.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/java/TheSkylineProblem.java -------------------------------------------------------------------------------- /java/TopkLargestNumbers2.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | private int maxSize; 3 | private Queue minheap; 4 | public Solution(int k) { 5 | minheap = new PriorityQueue<>(); 6 | maxSize = k; 7 | } 8 | 9 | public void add(int num) { 10 | if (minheap.size() < maxSize) { 11 | minheap.offer(num); 12 | return; 13 | } 14 | 15 | if (num > minheap.peek()) { 16 | minheap.poll(); 17 | minheap.offer(num); 18 | } 19 | } 20 | 21 | public List topk() { 22 | Iterator it = minheap.iterator(); 23 | List result = new ArrayList(); 24 | while (it.hasNext()) { 25 | result.add((Integer) it.next()); 26 | } 27 | Collections.sort(result, Collections.reverseOrder()); 28 | return result; 29 | } 30 | } -------------------------------------------------------------------------------- /java/TriangleCount.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param S: A list of integers 4 | * @return: An integer 5 | */ 6 | public int triangleCount(int S[]) { 7 | // write your code here 8 | int left = 0, right = S.length - 1; 9 | int ans = 0; 10 | Arrays.sort(S); 11 | for(int i = 0; i < S.length; i++) { 12 | left = 0; 13 | right = i - 1; 14 | while(left < right) { 15 | if(S[left] + S[right] > S[i]) { 16 | ans = ans + (right - left); 17 | right --; 18 | } else { 19 | left ++; 20 | } 21 | } 22 | } 23 | return ans; 24 | } 25 | } 26 | 27 | // 九章硅谷求职算法集训营版本 28 | public class Solution { 29 | /** 30 | * @param S: A list of integers 31 | * @return: An integer 32 | */ 33 | public int triangleCount(int A[]) { 34 | // write your code here 35 | 36 | int w, i, j, res = 0; 37 | Arrays.sort(A); 38 | int n = A.length; 39 | for (w = 2; w < n; ++w) { 40 | j = w - 1; 41 | for (i = 0; i < w; ++i) { 42 | while (j >= 0 && A[i] + A[j] > A[w]) { 43 | --j; 44 | } 45 | 46 | res += w - Math.max(i + 1, j + 1); 47 | } 48 | } 49 | 50 | return res; 51 | } 52 | } -------------------------------------------------------------------------------- /java/TwoSum-Greaterthantarget.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param nums: an array of integer 4 | * @param target: an integer 5 | * @return: an integer 6 | */ 7 | public int twoSum2(int[] nums, int target) { 8 | if (nums == null || nums.length < 2) { 9 | return 0; 10 | } 11 | 12 | Arrays.sort(nums); 13 | 14 | int left = 0, right = nums.length - 1; 15 | int count = 0; 16 | while (left < right) { 17 | if (nums[left] + nums[right] <= target) { 18 | left++; 19 | } else { 20 | count += right - left; 21 | right--; 22 | } 23 | } 24 | 25 | return count; 26 | } 27 | } -------------------------------------------------------------------------------- /java/TwoSum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] twoSum(int[] nums, int target) { 3 | HashMap map = new HashMap<>(); 4 | 5 | for (int i = 0; i < nums.length; i++) { 6 | if (map.get(nums[i]) != null) { 7 | int[] result = {map.get(nums[i]), i}; 8 | return result; 9 | } 10 | map.put(target - nums[i], i); 11 | } 12 | 13 | int[] result = {}; 14 | return result; 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /java/TwoSum3-DataStructureDesign.java: -------------------------------------------------------------------------------- 1 | public class TwoSum { 2 | /* 3 | * @param number: An integer 4 | * @return: nothing 5 | */ 6 | ArrayList nums = new ArrayList<>(); 7 | 8 | public void add(int number) { 9 | // write your code here 10 | nums.add(number); 11 | } 12 | 13 | /* 14 | * @param value: An integer 15 | * @return: Find if there exists any pair of numbers which sum is equal to the value. 16 | */ 17 | public boolean find(int value) { 18 | // write your code here 19 | Collections.sort(nums); 20 | for (int i = 0, j = nums.size() - 1; i < j;) { 21 | if (nums.get(i) + nums.get(j) == value) { 22 | return true; 23 | } else if (nums.get(i) + nums.get(j) < value) { 24 | i++; 25 | } else { 26 | j--; 27 | } 28 | } 29 | return false; 30 | } 31 | } -------------------------------------------------------------------------------- /java/UglyNumber.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param num an integer 4 | * @return true if num is an ugly number or false 5 | */ 6 | public boolean isUgly(int num) { 7 | if (num <= 0) return false; 8 | if (num == 1) return true; 9 | 10 | while (num >= 2 && num % 2 == 0) num /= 2; 11 | while (num >= 3 && num % 3 == 0) num /= 3; 12 | while (num >= 5 && num % 5 == 0) num /= 5; 13 | 14 | return num == 1; 15 | } 16 | } -------------------------------------------------------------------------------- /java/UniquePaths.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int uniquePaths(int m, int n) { 3 | if (m == 0 || n == 0) { 4 | return 1; 5 | } 6 | 7 | int[][] sum = new int[m][n]; 8 | for (int i = 0; i < m; i++) { 9 | sum[i][0] = 1; 10 | } 11 | for (int i = 0; i < n; i++) { 12 | sum[0][i] = 1; 13 | } 14 | for (int i = 1; i < m; i++) { 15 | for (int j = 1; j < n; j++) { 16 | sum[i][j] = sum[i - 1][j] + sum[i][j - 1]; 17 | } 18 | } 19 | return sum[m - 1][n - 1]; 20 | } 21 | } 22 | 23 | // 方法二 24 | public class Solution { 25 | /** 26 | * @param n, m: positive integer (1 <= n ,m <= 100) 27 | * @return an integer 28 | */ 29 | public int uniquePaths(int m, int n) { 30 | int[][] f = new int[m][n]; 31 | int i, j; 32 | for (i = 0; i < m; ++i) { 33 | for (j = 0; j < n; ++j) { 34 | if (i == 0 || j == 0) { 35 | f[i][j] = 1; 36 | } 37 | else { 38 | f[i][j] = f[i-1][j] + f[i][j-1]; 39 | } 40 | } 41 | } 42 | 43 | return f[m-1][n-1]; 44 | } 45 | } -------------------------------------------------------------------------------- /java/ValidateBinarySearchTree.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | private int lastVal = Integer.MIN_VALUE; 3 | private boolean firstNode = true; 4 | public boolean isValidBST(TreeNode root) { 5 | if (root == null) { 6 | return true; 7 | } 8 | if (!isValidBST(root.left)) { 9 | return false; 10 | } 11 | if (!firstNode && lastVal >= root.val) { 12 | return false; 13 | } 14 | firstNode = false; 15 | lastVal = root.val; 16 | if (!isValidBST(root.right)) { 17 | return false; 18 | } 19 | return true; 20 | } 21 | } -------------------------------------------------------------------------------- /java/WindowSum.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param nums a list of integers. 4 | * @return the sum of the element inside the window at each moving. 5 | */ 6 | public int[] winSum(int[] nums, int k) { 7 | // write your code here 8 | if (nums == null || nums.length < k || k <= 0) 9 | return new int[0]; 10 | 11 | int[] sums = new int[nums.length - k + 1]; 12 | for (int i = 0; i < k; i++) 13 | sums[0] += nums[i]; 14 | for (int i = 1; i < sums.length; i++) { 15 | sums[i] = sums[i - 1] - nums[i - 1] + nums[i + k-1]; 16 | } 17 | return sums; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/WoodCut.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param L: Given n pieces of wood with length L[i] 4 | * @param k: An integer 5 | * return: The maximum length of the small pieces. 6 | */ 7 | public int woodCut(int[] L, int k) { 8 | 9 | int l = 1; 10 | int r = 0; 11 | for (int item : L) { 12 | r = Math.max(r, item); 13 | } 14 | 15 | while (l + 1 < r) { 16 | int mid = l + (r - l) / 2; 17 | if (count(L, mid) >= k) { 18 | l = mid; 19 | } else { 20 | r = mid; 21 | } 22 | } 23 | 24 | if (count(L, r) >= k) { 25 | return r; 26 | } 27 | 28 | if (count(L, l) >= k) { 29 | return l; 30 | } 31 | return 0; 32 | } 33 | 34 | private int count(int[] L, int len) { 35 | int sum = 0; 36 | for (int item : L) { 37 | sum += item / len; 38 | } 39 | return sum; 40 | } 41 | } -------------------------------------------------------------------------------- /java/WordBreak.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | private int getMaxLength(Set dict) { 3 | int maxLength = 0; 4 | for (String word : dict) { 5 | maxLength = Math.max(maxLength, word.length()); 6 | } 7 | return maxLength; 8 | } 9 | 10 | public boolean wordBreak(String s, Set dict) { 11 | if (s == null || s.length() == 0) { 12 | return true; 13 | } 14 | 15 | int maxLength = getMaxLength(dict); 16 | boolean[] canSegment = new boolean[s.length() + 1]; 17 | 18 | canSegment[0] = true; 19 | for (int i = 1; i <= s.length(); i++) { 20 | canSegment[i] = false; 21 | for (int lastWordLength = 1; 22 | lastWordLength <= maxLength && lastWordLength <= i; 23 | lastWordLength++) { 24 | if (!canSegment[i - lastWordLength]) { 25 | continue; 26 | } 27 | String word = s.substring(i - lastWordLength, i); 28 | if (dict.contains(word)) { 29 | canSegment[i] = true; 30 | break; 31 | } 32 | } 33 | } 34 | 35 | return canSegment[s.length()]; 36 | } 37 | } -------------------------------------------------------------------------------- /java/WordBreak2.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public ArrayList wordBreak(String s, Set dict) { 3 | // Note: The Solution object is instantiated only once and is reused by each test case. 4 | Map> memo = new HashMap>(); 5 | return wordBreakHelper(s, dict, memo); 6 | } 7 | 8 | public ArrayList wordBreakHelper(String s, 9 | Set dict, 10 | Map> memo){ 11 | if (memo.containsKey(s)) { 12 | return memo.get(s); 13 | } 14 | 15 | ArrayList results = new ArrayList(); 16 | 17 | if (s.length() == 0) { 18 | return results; 19 | } 20 | 21 | if (dict.contains(s)) { 22 | results.add(s); 23 | } 24 | 25 | for (int len = 1; len < s.length(); ++len){ 26 | String word = s.substring(0, len); 27 | if (!dict.contains(word)) { 28 | continue; 29 | } 30 | 31 | String suffix = s.substring(len); 32 | ArrayList segmentations = wordBreakHelper(suffix, dict, memo); 33 | 34 | for (String segmentation: segmentations){ 35 | results.add(word + " " + segmentation); 36 | } 37 | } 38 | 39 | memo.put(s, results); 40 | return results; 41 | } 42 | } -------------------------------------------------------------------------------- /java/WordSearch.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // recursion 3 | public boolean exist(char[][] board, String word) { 4 | if(board == null || board.length == 0) 5 | return false; 6 | if(word.length() == 0) 7 | return true; 8 | 9 | for(int i = 0; i< board.length; i++){ 10 | for(int j=0; j< board[0].length; j++){ 11 | if(board[i][j] == word.charAt(0)){ 12 | 13 | boolean rst = find(board, i, j, word, 0); 14 | if(rst) 15 | return true; 16 | } 17 | } 18 | } 19 | return false; 20 | } 21 | 22 | private boolean find(char[][] board, int i, int j, String word, int start){ 23 | if(start == word.length()) 24 | return true; 25 | 26 | if (i < 0 || i>= board.length || 27 | j < 0 || j >= board[0].length || board[i][j] != word.charAt(start)){ 28 | return false; 29 | } 30 | 31 | board[i][j] = '#'; // should remember to mark it 32 | boolean rst = find(board, i-1, j, word, start+1) 33 | || find(board, i, j-1, word, start+1) 34 | || find(board, i+1, j, word, start+1) 35 | || find(board, i, j+1, word, start+1); 36 | board[i][j] = word.charAt(start); 37 | return rst; 38 | } 39 | } -------------------------------------------------------------------------------- /java/ZigzagIterator.java: -------------------------------------------------------------------------------- 1 | public class ZigzagIterator { 2 | 3 | public Iterator it1; 4 | public Iterator it2; 5 | public int turns; 6 | 7 | /** 8 | * @param v1 v2 two 1d vectors 9 | */ 10 | public ZigzagIterator(List v1, List v2) { 11 | // initialize your data structure here. 12 | this.it1 = v1.iterator(); 13 | this.it2 = v2.iterator(); 14 | turns = 0; 15 | } 16 | 17 | public int next() { 18 | // Write your code here 19 | turns++; 20 | if((turns % 2 == 1 && it1.hasNext()) || (!it2.hasNext())) { 21 | return it1.next(); 22 | } else if((turns % 2 == 0 && it2.hasNext()) || (!it1.hasNext())) { 23 | return it2.next(); 24 | } 25 | return -1; 26 | } 27 | 28 | public boolean hasNext() { 29 | // Write your code here 30 | return it1.hasNext() || it2.hasNext(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /java/ZigzagIterator2.java: -------------------------------------------------------------------------------- 1 | public class ZigzagIterator2 { 2 | 3 | public List> its; 4 | public int turns; 5 | 6 | /** 7 | * @param vecs a list of 1d vectors 8 | */ 9 | public ZigzagIterator2(List> vecs) { 10 | // initialize your data structure here. 11 | this.its = new ArrayList>(); 12 | for (List vec : vecs) { 13 | if (vec.size() > 0) 14 | its.add(vec.iterator()); 15 | } 16 | turns = 0; 17 | } 18 | 19 | public int next() { 20 | // Write your code here 21 | int elem = its.get(turns).next(); 22 | if (its.get(turns).hasNext()) 23 | turns = (turns + 1) % its.size(); 24 | else { 25 | its.remove(turns); 26 | if (its.size() > 0) 27 | turns %= its.size(); 28 | } 29 | return elem; 30 | } 31 | 32 | public boolean hasNext() { 33 | // Write your code here 34 | return its.size() > 0; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /java/addTwoNumbers.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode(int x) { val = x; } 7 | * } 8 | */ 9 | class Solution { 10 | public ListNode addTwoNumbers(ListNode l1, ListNode l2) { 11 | return addNumber(l1, l2, 0); 12 | } 13 | 14 | public static ListNode addNumber(ListNode l1, ListNode l2, int carry) { 15 | if(l1 == null && l2 == null) { 16 | return carry == 0? null : new ListNode(carry); 17 | } 18 | if(l1 == null && l2 != null) { 19 | l1 = new ListNode(0); 20 | } 21 | if(l2 == null && l1 != null) { 22 | l2 = new ListNode(0); 23 | } 24 | 25 | int sum = l1.val + l2.val + carry; 26 | ListNode current = new ListNode(sum % 10); 27 | carry = sum / 10; 28 | current.next = addNumber(l1.next, l2.next, carry); 29 | return current; 30 | } 31 | } 32 | 33 | -------------------------------------------------------------------------------- /java/binaryTreeLevelOrderTraversal2.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode(int x) { val = x; } 8 | * } 9 | */ 10 | class Solution { 11 | public List> levelOrderBottom(TreeNode root) { 12 | List> result = new ArrayList<>(); 13 | if(root == null) { 14 | return result; 15 | } 16 | 17 | Queue queue = new LinkedList(); 18 | queue.offer(root); 19 | 20 | while(!queue.isEmpty()) { 21 | ArrayList level = new ArrayList<>(); 22 | int size = queue.size(); 23 | for(int i = 0; i < size; i++) { 24 | TreeNode node = queue.poll(); 25 | if(node.left != null) { 26 | queue.offer(node.left); 27 | } 28 | if(node.right != null) { 29 | queue.offer(node.right); 30 | } 31 | level.add(node.val); 32 | } 33 | result.add(level); 34 | } 35 | Collections.reverse(result); 36 | return result; 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /java/binaryTreePaths.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition of TreeNode: 3 | * public class TreeNode { 4 | * public int val; 5 | * public TreeNode left, right; 6 | * public TreeNode(int val) { 7 | * this.val = val; 8 | * this.left = this.right = null; 9 | * } 10 | * } 11 | */ 12 | 13 | 14 | public class Solution { 15 | /* 16 | * @param root: the root of the binary tree 17 | * @return: all root-to-leaf paths 18 | */ 19 | public List binaryTreePaths(TreeNode root) { 20 | // write your code here 21 | List paths = new ArrayList<>(); 22 | if (root == null) { 23 | return paths; 24 | } 25 | 26 | List leftPaths = binaryTreePaths(root.left); 27 | List rightPaths = binaryTreePaths(root.right); 28 | for (String path : leftPaths) { 29 | paths.add(root.val + "->" + path); 30 | } 31 | for (String path : rightPaths) { 32 | paths.add(root.val + "->" + path); 33 | } 34 | 35 | if (paths.size() == 0) { 36 | paths.add("" + root.val); 37 | } 38 | 39 | return paths; 40 | } 41 | } 42 | 43 | -------------------------------------------------------------------------------- /java/closetNumberInSortedArray.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /* 3 | * @param A: an integer array sorted in ascending order 4 | * @param target: An integer 5 | * @return: an integer 6 | */ 7 | public int closestNumber(int[] A, int target) { 8 | // write your code here 9 | if(A == null || A.length == 0) { 10 | return -1; 11 | } 12 | int str = 0; 13 | int end = A.length - 1; 14 | 15 | while(str + 1 < end) { 16 | int mid = str + (end - str) / 2; 17 | if(A[mid] == target) { 18 | return mid; 19 | }else if(A[mid] < target) { 20 | str = mid; 21 | }else if(A[mid] > target) { 22 | end = mid; 23 | } 24 | } 25 | if (target <= A[str]) { 26 | return str; 27 | } else if (A[end] <= target) { 28 | return end; 29 | } else { 30 | if (target - A[str] > A[end] - target) { 31 | return end; 32 | } else { 33 | return str; 34 | } 35 | } 36 | } 37 | } 38 | 39 | -------------------------------------------------------------------------------- /java/combinationSum2.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /* 3 | * @param num: Given the candidate numbers 4 | * @param target: Given the target number 5 | * @return: All the combinations that sum to target 6 | */ 7 | public List> combinationSum2(int[] num, int target) { 8 | // write your code here 9 | List> result = new ArrayList<>(); 10 | if (num == null || num.length == 0) { 11 | return result; 12 | } 13 | 14 | Arrays.sort(num); 15 | List combination = new ArrayList(); 16 | helper(num, combination, 0, target, result); 17 | return result; 18 | } 19 | 20 | private void helper(int[] num, 21 | List combination, 22 | int startIndex, 23 | int target, 24 | List> result) { 25 | if (target == 0) { 26 | result.add(new ArrayList<>(combination)); 27 | return; 28 | } 29 | for (int i = startIndex; i < num.length; i++) { 30 | if (i != startIndex && num[i] == num[i - 1]) { 31 | continue; 32 | } 33 | if (num[i] > target) { 34 | break; 35 | } 36 | combination.add(num[i]); 37 | helper(num, combination, i + 1, target - num[i], result); 38 | combination.remove(combination.size() - 1); 39 | } 40 | } 41 | } 42 | 43 | -------------------------------------------------------------------------------- /java/combinations.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List> combine(int n, int k) { 3 | List> result = new ArrayList<>(); 4 | if(k == 0) { 5 | return result; 6 | } 7 | List combine = new ArrayList<>(); 8 | helper(n, combine, 1, k, result); 9 | return result; 10 | } 11 | 12 | private void helper(int n, 13 | List combine, 14 | int startIndex, 15 | int k, 16 | List> result) { 17 | if(combine.size() == k) { 18 | result.add(new ArrayList(combine)); 19 | return; 20 | } 21 | 22 | for (int i = startIndex; i <= n; i++) { 23 | combine.add(i); 24 | helper(n, combine, i + 1, k, result); 25 | combine.remove(combine.size() - 1); 26 | } 27 | } 28 | } 29 | 30 | -------------------------------------------------------------------------------- /java/dropEggs.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /* 3 | * @param n: An integer 4 | * @return: The sum of a and b 5 | */ 6 | public int dropEggs(int n) { 7 | // write your code here 8 | long ans = 0; 9 | for(int i = 1; ; i++) { 10 | ans += (long)i; 11 | if(ans >= (long)n) { 12 | return i; 13 | } 14 | } 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /java/findMinimumInRotatedSortedArray.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /* 3 | * @param nums: a rotated sorted array 4 | * @return: the minimum number in the array 5 | */ 6 | public int findMin(int[] nums) { 7 | // write your code here 8 | if (nums == null || nums.length == 0) { 9 | return -1; 10 | } 11 | 12 | if(nums.length == 1) { 13 | return nums[0]; 14 | } 15 | 16 | int start = 0, end = nums.length - 1; 17 | int target = nums[nums.length - 1]; 18 | 19 | while (start + 1 < end) { 20 | int mid = start + (end - start) / 2; 21 | if (nums[mid] <= target) { 22 | end = mid; 23 | } else { 24 | start = mid; 25 | } 26 | } 27 | if (nums[start] <= target) { 28 | return nums[start]; 29 | } else { 30 | return nums[end]; 31 | } 32 | } 33 | } 34 | 35 | -------------------------------------------------------------------------------- /java/findPeakElement.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /* 3 | * @param A: An integers array. 4 | * @return: return any of peek positions. 5 | */ 6 | public int findPeak(int[] A) { 7 | // write your code here 8 | int[] nums = A; 9 | int start = 1; 10 | int end = nums.length - 2; 11 | 12 | while(start + 1 < end) { 13 | int mid = start + (end - start) / 2; 14 | if(nums[mid] < nums[mid - 1]) { 15 | end = mid; 16 | } else if(nums[mid] < nums[mid + 1]) { 17 | start = mid; 18 | } else { 19 | end = mid; 20 | } 21 | } 22 | if(nums[start] < nums[end]) { 23 | return end; 24 | } else { 25 | return start; 26 | } 27 | } 28 | } 29 | 30 | -------------------------------------------------------------------------------- /java/findTheDuplicateNumber.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findDuplicate(int[] nums) { 3 | if (nums == null || nums.length < 2) { 4 | return -1; 5 | } 6 | 7 | Arrays.sort(nums); 8 | int index = 0; 9 | for (int i = 1; i < nums.length; i++) { 10 | if(nums[index++] == nums[i]) { 11 | return nums[i]; 12 | } 13 | } 14 | return -1; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java/firstBadVersion.java: -------------------------------------------------------------------------------- 1 | /** 2 | * public class SVNRepo { 3 | * public static boolean isBadVersion(int k); 4 | * } 5 | * you can use SVNRepo.isBadVersion(k) to judge whether 6 | * the kth code version is bad or not. 7 | */ 8 | 9 | public class Solution { 10 | /* 11 | * @param n: An integer 12 | * @return: An integer which is the first bad version. 13 | */ 14 | public int findFirstBadVersion(int n) { 15 | // write your code here 16 | int start = 0; 17 | int end = n; 18 | while(start + 1 < end) { 19 | int mid = start + (end - start) / 2; 20 | if(SVNRepo.isBadVersion(mid)){ 21 | end = mid; 22 | }else{ 23 | start = mid; 24 | } 25 | } 26 | 27 | if(SVNRepo.isBadVersion(start)) { 28 | return start; 29 | } 30 | return end; 31 | } 32 | } 33 | 34 | -------------------------------------------------------------------------------- /java/firstPositionOfTarget.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | /** 3 | * @param nums: The integer array. 4 | * @param target: Target to find. 5 | * @return: The first position of target. Position starts from 0. 6 | */ 7 | public int binarySearch(int[] nums, int target) { 8 | //write your code here 9 | if(nums == null || nums.length == 0) { 10 | return -1; 11 | } 12 | 13 | int start = 0; 14 | int end = nums.length - 1; 15 | 16 | while(start + 1 < end) { 17 | int mid = start + (end - start) / 2; 18 | if(nums[mid] == target) { 19 | end = mid; 20 | }else if(nums[mid] < target) { 21 | start = mid; 22 | }else{ 23 | end = mid; 24 | } 25 | } 26 | 27 | if (nums[start] == target) { 28 | return start; 29 | } 30 | if (nums[end] == target) { 31 | return end; 32 | } 33 | return -1; 34 | } 35 | } 36 | 37 | -------------------------------------------------------------------------------- /java/insertInToACyclicSortedList.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param node a list node in the list 4 | * @param x an integer 5 | * @return the inserted new list node 6 | */ 7 | public ListNode insert(ListNode node, int x) { 8 | // Write your code here 9 | if (node == null) { 10 | node = new ListNode(x); 11 | node.next = node; 12 | return node; 13 | } 14 | 15 | ListNode p = node; 16 | ListNode prev = null; 17 | do { 18 | prev = p; 19 | p = p.next; 20 | if (x <= p.val && x >= prev.val) { 21 | break; 22 | } 23 | if ((prev.val > p.val) && (x < p.val || x > prev.val)) { 24 | break; 25 | } 26 | } while (p != node); 27 | 28 | ListNode newNode = new ListNode(x); 29 | newNode.next = p; 30 | prev.next = newNode; 31 | return newNode; 32 | } 33 | } -------------------------------------------------------------------------------- /java/intersectionOfTwoArrays2.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param nums1 an integer array 4 | * @param nums2 an integer array 5 | * @return an integer array 6 | */ 7 | public int[] intersection(int[] nums1, int[] nums2) { 8 | // Write your code here 9 | Map map = new HashMap(); 10 | for(int i = 0; i < nums1.length; ++i) { 11 | if (map.containsKey(nums1[i])) 12 | map.put(nums1[i], map.get(nums1[i]) + 1); 13 | else 14 | map.put(nums1[i], 1); 15 | } 16 | 17 | List results = new ArrayList(); 18 | 19 | for (int i = 0; i < nums2.length; ++i) 20 | if (map.containsKey(nums2[i]) && 21 | map.get(nums2[i]) > 0) { 22 | results.add(nums2[i]); 23 | map.put(nums2[i], map.get(nums2[i]) - 1); 24 | } 25 | 26 | int result[] = new int[results.size()]; 27 | for(int i = 0; i < results.size(); ++i) 28 | result[i] = results.get(i); 29 | 30 | return result; 31 | } 32 | } -------------------------------------------------------------------------------- /java/kSum.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param A: an integer array. 4 | * @param k: a positive integer (k <= length(A)) 5 | * @param target: a integer 6 | * @return an integer 7 | */ 8 | public int kSum(int A[], int k, int target) { 9 | int n = A.length; 10 | int[][][] f = new int[n + 1][k + 1][target + 1]; 11 | for (int i = 0; i < n + 1; i++) { 12 | f[i][0][0] = 1; 13 | } 14 | for (int i = 1; i <= n; i++) { 15 | for (int j = 1; j <= k && j <= i; j++) { 16 | for (int t = 1; t <= target; t++) { 17 | f[i][j][t] = 0; 18 | if (t >= A[i - 1]) { 19 | f[i][j][t] = f[i - 1][j - 1][t - A[i - 1]]; 20 | } 21 | f[i][j][t] += f[i - 1][j][t]; 22 | } // for t 23 | } // for j 24 | } // for i 25 | return f[n][k][target]; 26 | } 27 | } -------------------------------------------------------------------------------- /java/kSum2.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param A: an integer array. 4 | * @param k: a positive integer (k <= length(A)) 5 | * @param target: a integer 6 | * @return an integer 7 | */ 8 | List > ans; 9 | public void dfs(int A[], int K, int target, int index, List tans) 10 | { 11 | 12 | if(K == 0 && target == 0) { 13 | ans.add(new ArrayList(tans)); 14 | return ; 15 | } 16 | if(K < 0 || target < 0 || index < 0) 17 | return ; 18 | dfs(A, K, target, index - 1, tans); 19 | tans.add(A[index]); 20 | dfs(A, K - 1, target - A[index], index - 1, tans); 21 | tans.remove(tans.size() - 1); 22 | 23 | } 24 | 25 | public List> kSumII(int A[], int K, int target) { 26 | ans = new ArrayList>(); 27 | List tans = new ArrayList(); 28 | dfs(A, K, target, A.length - 1, tans); 29 | return ans; 30 | } 31 | } -------------------------------------------------------------------------------- /java/lastPositionofTarget.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /* 3 | * @param nums: An integer array sorted in ascending order 4 | * @param target: An integer 5 | * @return: An integer 6 | */ 7 | public int lastPosition(int[] nums, int target) { 8 | // write your code here 9 | if(nums == null || nums.length == 0) { 10 | return -1; 11 | } 12 | 13 | int start = 0; 14 | int len = nums.length; 15 | int end = len - 1; 16 | 17 | while(start + 1 < end) { 18 | int mid = start + (end - start) / 2; 19 | if(nums[mid] == target) { 20 | while(mid != len - 1 && nums[mid] == nums[mid + 1]) { 21 | mid+=1; 22 | } 23 | return mid; 24 | }else if(nums[mid] < target) { 25 | start = mid; 26 | }else if(target < nums[mid]) { 27 | end = mid; 28 | } 29 | } 30 | return -1; 31 | } 32 | } 33 | 34 | -------------------------------------------------------------------------------- /java/linkedListCycle.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public Boolean hasCycle(ListNode head) { 3 | if (head == null || head.next == null) { 4 | return false; 5 | } 6 | 7 | ListNode fast, slow; 8 | fast = head.next; 9 | slow = head; 10 | while (fast != slow) { 11 | if(fast==null || fast.next==null) 12 | return false; 13 | fast = fast.next.next; 14 | slow = slow.next; 15 | } 16 | return true; 17 | } 18 | } -------------------------------------------------------------------------------- /java/linkedListCycle2.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public ListNode detectCycle(ListNode head) { 3 | if (head == null || head.next==null) { 4 | return null; 5 | } 6 | 7 | ListNode fast, slow; 8 | fast = head.next; 9 | slow = head; 10 | while (fast != slow) { 11 | if(fast==null || fast.next==null) 12 | return null; 13 | fast = fast.next.next; 14 | slow = slow.next; 15 | } 16 | 17 | while (head != slow.next) { 18 | head = head.next; 19 | slow = slow.next; 20 | } 21 | return head; 22 | } 23 | } 24 | 25 | -------------------------------------------------------------------------------- /java/longestPalindrome.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /** 3 | * @param s: a string which consists of lowercase or uppercase letters 4 | * @return: the length of the longest palindromes that can be built 5 | */ 6 | public int longestPalindrome(String s) { 7 | // write your code here 8 | if (s == null || s.length() == 0) { 9 | return 0; 10 | } 11 | Set set = new HashSet<>(); 12 | for (char c : s.toCharArray()) { 13 | if (set.contains(c)) set.remove(c); 14 | else set.add(c); 15 | } 16 | 17 | int remove = set.size(); 18 | if (remove > 0) 19 | remove -= 1; 20 | return s.length() - remove; 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /java/maximumNumberInMountainSequence.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /* 3 | * @param nums: a mountain sequence which increase firstly and then decrease 4 | * @return: then mountain top 5 | */ 6 | public int mountainSequence(int[] nums) { 7 | // write your code here 8 | if(nums == null || nums.length == 0) { 9 | return -1; 10 | } 11 | 12 | int start = 0; 13 | int end = nums.length - 1; 14 | 15 | while(start + 1 < end) { 16 | int mid = start + (end - start) / 2; 17 | if(nums[mid - 1] < nums[mid] ) { 18 | if(nums[mid] < nums[mid - 1]) { 19 | return nums[mid]; 20 | }else{ 21 | start = mid; 22 | } 23 | }else{ 24 | end = mid; 25 | } 26 | } 27 | 28 | if(nums[start] > nums[end]) { 29 | return nums[start]; 30 | }else{ 31 | return nums[end]; 32 | } 33 | } 34 | } 35 | 36 | -------------------------------------------------------------------------------- /java/mergeTwoSortedLists.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode(int x) { val = x; } 7 | * } 8 | */ 9 | class Solution { 10 | public ListNode mergeTwoLists(ListNode l1, ListNode l2) { 11 | if (l1 == null) return l2; 12 | if (l2 == null) return l1; 13 | 14 | ListNode head = new ListNode(-1); 15 | ListNode tmp = head; 16 | 17 | while(l1 != null && l2 != null) { 18 | if(l1.val <= l2.val) { 19 | tmp.next = l1; 20 | l1 = l1.next; 21 | } else { 22 | tmp.next = l2; 23 | l2 = l2.next; 24 | } 25 | tmp = tmp.next; 26 | } 27 | 28 | if(l1 != null) { 29 | tmp.next = l1; 30 | } 31 | if(l2 != null) { 32 | tmp.next = l2; 33 | } 34 | 35 | return head.next; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /java/minimumDepthOfBinaryTree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode(int x) { val = x; } 8 | * } 9 | */ 10 | class Solution { 11 | public int minDepth(TreeNode root) { 12 | if (root == null) { 13 | return 0; 14 | } 15 | return getMin(root); 16 | } 17 | 18 | public int getMin(TreeNode root){ 19 | if (root == null) { 20 | return Integer.MAX_VALUE; 21 | } 22 | 23 | if (root.left == null && root.right == null) { 24 | return 1; 25 | } 26 | 27 | return Math.min(getMin(root.left), getMin(root.right)) + 1; 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /java/minimumSizeSubarraySum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minSubArrayLen(int s, int[] nums) { 3 | if (nums == null || nums.length == 0) { 4 | return 0; 5 | } 6 | 7 | int j = 0; 8 | int prefixSum = 0; 9 | int result = Integer.MAX_VALUE; 10 | for (int i = 0; i < nums.length; i++) { 11 | while (j < nums.length && prefixSum < s) { 12 | prefixSum += nums[j]; 13 | j++; 14 | } 15 | if (prefixSum >= s) { 16 | result = Math.min(result, j - i); 17 | } 18 | prefixSum -= nums[i]; 19 | } 20 | if (result == Integer.MAX_VALUE) { 21 | result = 0; 22 | } 23 | return result; 24 | } 25 | } 26 | 27 | -------------------------------------------------------------------------------- /java/minimumSubtree.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition of TreeNode: 3 | * public class TreeNode { 4 | * public int val; 5 | * public TreeNode left, right; 6 | * public TreeNode(int val) { 7 | * this.val = val; 8 | * this.left = this.right = null; 9 | * } 10 | * } 11 | */ 12 | 13 | 14 | public class Solution { 15 | /* 16 | * @param root: the root of binary tree 17 | * @return: the root of the minimum subtree 18 | */ 19 | private TreeNode subtree = null; 20 | private int subtreeSum = Integer.MAX_VALUE; 21 | 22 | public TreeNode findSubtree(TreeNode root) { 23 | // write your code here 24 | helper(root); 25 | return subtree; 26 | } 27 | 28 | private int helper(TreeNode root) { 29 | if(root == null) { 30 | return 0; 31 | } 32 | 33 | int sum = helper(root.left) + helper(root.right) + root.val; 34 | if (sum <= subtreeSum) { 35 | subtreeSum = sum; 36 | subtree = root; 37 | } 38 | return sum; 39 | } 40 | } 41 | 42 | -------------------------------------------------------------------------------- /java/palindromePartitioning.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /* 3 | * @param s: A string 4 | * @return: A list of lists of string 5 | */ 6 | public List> partition(String s) { 7 | // write your code here 8 | List> results = new ArrayList<>(); 9 | if (s == null || s.length() == 0) { 10 | return results; 11 | } 12 | 13 | List partition = new ArrayList(); 14 | helper(s, 0, partition, results); 15 | 16 | return results; 17 | } 18 | 19 | private void helper(String s, 20 | int startIndex, 21 | List partition, 22 | List> results) { 23 | if (startIndex == s.length()) { 24 | results.add(new ArrayList(partition)); 25 | return; 26 | } 27 | 28 | for (int i = startIndex; i < s.length(); i++) { 29 | String subString = s.substring(startIndex, i + 1); 30 | if (!isPalindrome(subString)) { 31 | continue; 32 | } 33 | partition.add(subString); 34 | helper(s, i + 1, partition, results); 35 | partition.remove(partition.size() - 1); 36 | } 37 | } 38 | 39 | private boolean isPalindrome(String s) { 40 | for (int i = 0, j = s.length() - 1; i < j; i++, j--) { 41 | if (s.charAt(i) != s.charAt(j)) { 42 | return false; 43 | } 44 | } 45 | return true; 46 | } 47 | } 48 | 49 | -------------------------------------------------------------------------------- /java/partitionArray.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /* 3 | * @param nums: The integer array you should partition 4 | * @param k: An integer 5 | * @return: The index after partition 6 | */ 7 | public int partitionArray(int[] nums, int k) { 8 | // write your code here 9 | if (nums == null || nums.length == 0) { 10 | return 0; 11 | } 12 | 13 | int l = 0; 14 | int r = nums.length - 1; 15 | 16 | while (l <= r) { 17 | while (l <= r && nums[l] < k) { 18 | l++; 19 | } 20 | 21 | while (l <= r && nums[r] >= k) { 22 | r--; 23 | } 24 | 25 | if (l <= r) { 26 | int temp = nums[l]; 27 | nums[l] = nums[r]; 28 | nums[r] = temp; 29 | 30 | l++; 31 | r--; 32 | } 33 | } 34 | return l; 35 | } 36 | } 37 | 38 | -------------------------------------------------------------------------------- /java/permutations.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /* 3 | * @param nums: A list of integers. 4 | * @return: A list of permutations. 5 | */ 6 | public List> permute(int[] nums) { 7 | List> results = new ArrayList<>(); 8 | if (nums == null) { 9 | return results; 10 | } 11 | 12 | dfs(nums, new boolean[nums.length], new ArrayList(), results); 13 | 14 | return results; 15 | } 16 | 17 | private void dfs(int[] nums, 18 | boolean[] visited, 19 | List permutation, 20 | List> results) { 21 | if (nums.length == permutation.size()) { 22 | results.add(new ArrayList(permutation)); 23 | return; 24 | } 25 | 26 | for (int i = 0; i < nums.length; i++) { 27 | if (visited[i]) { 28 | continue; 29 | } 30 | 31 | permutation.add(nums[i]); 32 | visited[i] = true; 33 | dfs(nums, visited, permutation, results); 34 | visited[i] = false; 35 | permutation.remove(permutation.size() - 1); 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /java/permutations2.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /* 3 | * @param : A list of integers 4 | * @return: A list of unique permutations 5 | */ 6 | public List> permuteUnique(int[] nums) { 7 | // write your code here 8 | List> result = new ArrayList<>(); 9 | if (nums == null) { 10 | return result; 11 | } 12 | 13 | Arrays.sort(nums); 14 | ArrayList permute = new ArrayList(); 15 | int[] visited = new int[nums.length]; 16 | for (int i = 0; i < visited.length; i++){ 17 | visited[i] = 0; 18 | } 19 | helper(nums, permute, visited, result); 20 | return result; 21 | } 22 | 23 | private void helper(int[] nums, 24 | ArrayList permute, 25 | int[] visited, 26 | List> result) { 27 | if(permute.size() == nums.length) { 28 | result.add(new ArrayList<>(permute)); 29 | return; 30 | } 31 | for(int i = 0; i < nums.length; i++) { 32 | if (visited[i] == 1 || ( i != 0 && nums[i] == nums[i - 1] 33 | && visited[i-1] == 0)){ 34 | continue; 35 | } 36 | visited[i] = 1; 37 | permute.add(nums[i]); 38 | helper(nums, permute, visited, result); 39 | permute.remove(permute.size() - 1); 40 | visited[i] = 0; 41 | } 42 | } 43 | } 44 | 45 | -------------------------------------------------------------------------------- /java/removeDuplicateNumbersInArray.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /* 3 | * @param nums: an array of integers 4 | * @return: the number of unique integers 5 | */ 6 | public int deduplication(int[] nums) { 7 | // write your code here 8 | if (nums == null || nums.length == 0) { 9 | return 0; 10 | } 11 | 12 | HashMap map = new HashMap<>(); 13 | int index = 0; 14 | for (int i = 0; i < nums.length; i++) { 15 | if (map.get(nums[i]) != null) { 16 | continue; 17 | } 18 | nums[index++] = nums[i]; 19 | map.put(nums[i], 1); 20 | } 21 | 22 | return index; 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /java/searchInABigSortedArray.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /* 3 | * @param reader: An instance of ArrayReader. 4 | * @param target: An integer 5 | * @return: An integer which is the first index of target. 6 | */ 7 | public int searchBigSortedArray(ArrayReader reader, int target) { 8 | // write your code here 9 | int index = 1; 10 | while(reader.get(index - 1) < target) { 11 | index = index * 2; 12 | } 13 | 14 | int start = 0; 15 | int end = index; 16 | 17 | while(start + 1 < end) { 18 | int mid = start + (end - start) / 2; 19 | if(reader.get(mid) < target) { 20 | start = mid; 21 | }else{ 22 | end = mid; 23 | } 24 | } 25 | 26 | if(reader.get(start) == target) { 27 | return start; 28 | } 29 | 30 | if(reader.get(end) == target) { 31 | return end; 32 | } 33 | 34 | return -1; 35 | } 36 | } 37 | 38 | -------------------------------------------------------------------------------- /java/searchInRotatedSortedArray.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /* 3 | * @param A: an integer rotated sorted array 4 | * @param target: an integer to be searched 5 | * @return: an integer 6 | */ 7 | public int search(int[] A, int target) { 8 | // write your code here 9 | if (A == null || A.length == 0) { 10 | return -1; 11 | } 12 | 13 | int start = 0; 14 | int end = A.length - 1; 15 | int mid; 16 | 17 | while (start + 1 < end) { 18 | mid = start + (end - start) / 2; 19 | if (A[mid] == target) { 20 | return mid; 21 | } 22 | if (A[start] < A[mid]) { 23 | if (A[start] <= target && target <= A[mid]) { 24 | end = mid; 25 | } else { 26 | start = mid; 27 | } 28 | } else { 29 | if (A[mid] <= target && target <= A[end]) { 30 | start = mid; 31 | } else { 32 | end = mid; 33 | } 34 | } 35 | } 36 | 37 | if (A[start] == target) { 38 | return start; 39 | } 40 | if (A[end] == target) { 41 | return end; 42 | } 43 | return -1; 44 | } 45 | } 46 | 47 | -------------------------------------------------------------------------------- /java/searcha2DMatrix.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /* 3 | * @param matrix: matrix, a list of lists of integers 4 | * @param target: An integer 5 | * @return: a boolean, indicate whether matrix contains target 6 | */ 7 | public boolean searchMatrix(int[][] matrix, int target) { 8 | // write your code here 9 | if(matrix = null) { 10 | return false; 11 | } 12 | 13 | int j = 0; 14 | for(int i = 0; i < matrix.length; i++) { 15 | if(matrix[i][j] == target) { 16 | return true; 17 | } 18 | if() 19 | } 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /java/sortColors.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /* 3 | * @param nums: A list of integer which is 0, 1 or 2 4 | * @return: nothing 5 | */ 6 | public void sortColors(int[] nums) { 7 | // write your code here 8 | if (nums == null || nums.length == 0) { 9 | return; 10 | } 11 | 12 | int l = 0; 13 | int r = nums.length - 1; 14 | int i = 0; 15 | while (i <= r) { 16 | if (nums[i] == 0) { 17 | swap(nums, l, i); 18 | l++; 19 | i++; 20 | } else if (nums[i] == 1) { 21 | i++; 22 | } else { 23 | swap(nums, r, i); 24 | r--; 25 | } 26 | } 27 | } 28 | 29 | private void swap(int[] nums, int i, int j) { 30 | int tmp = nums[i]; 31 | nums[i] = nums[j]; 32 | nums[j] = tmp; 33 | } 34 | } 35 | 36 | -------------------------------------------------------------------------------- /java/strStr.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int strStr(String source, String target) { 3 | if (source == null || target == null || target.length() > source.length()) { 4 | return -1; 5 | } 6 | if (target.length() == 0) { 7 | return 0; 8 | } 9 | for (int i = 0; i <= source.length() - target.length(); i++) { 10 | if(source.substring(i, i + target.length()).equals(target)) { 11 | return i; 12 | } 13 | } 14 | return -1; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java/subsets.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | /* 4 | * @param nums: A set of numbers 5 | * @return: A list of lists 6 | */ 7 | public List> subsets(int[] nums) { 8 | // write your code here 9 | List> res = new ArrayList<>(); 10 | if(nums == null) { 11 | return res; 12 | } 13 | 14 | if (nums.length == 0) { 15 | res.add(new ArrayList()); 16 | return res; 17 | } 18 | 19 | Arrays.sort(nums); 20 | int star = 0; 21 | List subset = new ArrayList<>(); 22 | Backtraking(subset, nums, star, res); 23 | return res; 24 | } 25 | 26 | private void Backtraking(List subset, 27 | int[] nums, 28 | int star, 29 | List> res) { 30 | // deep copy 31 | res.add(new ArrayList<>(subset)); 32 | for(int i = star; i < nums.length; i++) { 33 | subset.add(nums[i]); 34 | Backtraking(subset, nums, i + 1, res); 35 | subset.remove(subset.size() - 1); 36 | } 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /java/subtreeWithMaximumAverage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition of TreeNode: 3 | * public class TreeNode { 4 | * public int val; 5 | * public TreeNode left, right; 6 | * public TreeNode(int val) { 7 | * this.val = val; 8 | * this.left = this.right = null; 9 | * } 10 | * } 11 | */ 12 | 13 | 14 | public class Solution { 15 | /* 16 | * @param root: the root of binary tree 17 | * @return: the root of the maximum average of subtree 18 | */ 19 | public class ResultType { 20 | public int sum, size; 21 | public ResultType(int sum, int size) { 22 | this.sum = sum; 23 | this.size = size; 24 | } 25 | } 26 | 27 | private TreeNode subtree = null; 28 | private ResultType subtreeResult = null; 29 | 30 | public TreeNode findSubtree2(TreeNode root) { 31 | // write your code here 32 | helper(root); 33 | return subtree; 34 | } 35 | 36 | private ResultType helper(TreeNode root) { 37 | if(root == null) { 38 | return new ResultType(0, 0); 39 | } 40 | ResultType left = helper(root.left); 41 | ResultType right = helper(root.right); 42 | 43 | ResultType result = new ResultType( 44 | left.sum + right.sum + root.val, 45 | left.size + right.size + 1 46 | ); 47 | 48 | if (subtree == null || 49 | subtreeResult.sum * result.size < result.sum * subtreeResult.size 50 | ) { 51 | subtree = root; 52 | subtreeResult = result; 53 | } 54 | return result; 55 | } 56 | } 57 | 58 | -------------------------------------------------------------------------------- /java/totalOccurrenceOfTarget.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /* 3 | * @param A: A an integer array sorted in ascending order 4 | * @param target: An integer 5 | * @return: An integer 6 | */ 7 | public int totalOccurrence(int[] A, int target) { 8 | // write your code here 9 | int n = A.length; 10 | if (n == 0) { 11 | return 0; 12 | } 13 | if (A[n-1] < target || A[0] > target) { 14 | return 0; 15 | } 16 | 17 | int l = 0, r = n - 1; 18 | int start = 0; 19 | while (l <= r) { 20 | int mid = l + (r - l) / 2; 21 | if (A[mid] >= target) { 22 | start = mid; 23 | r = mid - 1; 24 | } else 25 | l = mid + 1; 26 | } 27 | if (A[start] != target) 28 | return 0; 29 | 30 | int end = n - 1; 31 | l = 0; r = n - 1; 32 | while (l <= r) { 33 | int mid = l + (r - l) / 2; 34 | if (A[mid] <= target) { 35 | end = mid; 36 | l = mid + 1; 37 | } else 38 | r = mid - 1; 39 | } 40 | return end - start + 1; 41 | } 42 | } 43 | 44 | -------------------------------------------------------------------------------- /java/twoSum-ClosestToTarget.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /* 3 | * @param nums: an integer array 4 | * @param target: An integer 5 | * @return: the difference between the sum and the target 6 | */ 7 | public int twoSumClosest(int[] nums, int target) { 8 | // write your code here 9 | if (nums == null || nums.length < 2) { 10 | return -1; 11 | } 12 | Arrays.sort(nums); 13 | int l = 0; 14 | int r = nums.length - 1; 15 | int diff = Integer.MAX_VALUE; 16 | while (l < r) { 17 | if (nums[l] + nums[r] < target ) { 18 | diff = Math.min(diff, target - nums[l] - nums[r]); 19 | l++; 20 | } else { 21 | diff = Math.min(diff, nums[l] + nums[r] - target); 22 | r--; 23 | } 24 | } 25 | 26 | return diff; 27 | } 28 | } 29 | 30 | -------------------------------------------------------------------------------- /java/twoSum-LessThanOrEqualToTarget.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /* 3 | * @param nums: an array of integer 4 | * @param target: an integer 5 | * @return: an integer 6 | */ 7 | public int twoSum5(int[] nums, int target) { 8 | // write your code here 9 | if (nums == null || nums.length == 0) { 10 | return 0; 11 | } 12 | int l = 0; 13 | int r = nums.length - 1; 14 | int count = 0; 15 | Arrays.sort(nums); 16 | 17 | while (l < r) { 18 | if (nums[l] + nums[r] <= target) { 19 | count += r - l; 20 | l++; 21 | } else { 22 | r--; 23 | } 24 | } 25 | 26 | return count; 27 | } 28 | } 29 | 30 | -------------------------------------------------------------------------------- /java/twoSum-UniquePairs.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /* 3 | * @param nums: an array of integer 4 | * @param target: An integer 5 | * @return: An integer 6 | */ 7 | public int twoSum6(int[] nums, int target) { 8 | // write your code here 9 | if (nums == null || nums.length == 0) { 10 | return 0; 11 | } 12 | 13 | Arrays.sort(nums); 14 | int count = 0; 15 | int l = 0; 16 | int len = nums.length - 1; 17 | int r = len; 18 | 19 | while (l < r) { 20 | if (nums[l] + nums[r] == target) { 21 | while(l != len && nums[l] == nums[l + 1]) { 22 | l++; 23 | } 24 | while(r != 0 && nums[r] == nums[r - 1]) { 25 | r--; 26 | } 27 | count++; 28 | l++; 29 | r--; 30 | } else if (nums[l] + nums[r] < target) { 31 | l++; 32 | } else { 33 | r--; 34 | } 35 | } 36 | 37 | return count; 38 | } 39 | } 40 | 41 | -------------------------------------------------------------------------------- /java/twoSumDataStructureDesign.java: -------------------------------------------------------------------------------- 1 | public class TwoSum { 2 | private List list = null; 3 | private Map map = null; 4 | public TwoSum() { 5 | list = new ArrayList(); 6 | map = new HashMap(); 7 | } 8 | /* 9 | * @param number: An integer 10 | * @return: nothing 11 | */ 12 | public void add(int number) { 13 | // write your code here 14 | if (map.containsKey(number)) { 15 | map.put(number, map.get(number) + 1); 16 | } else { 17 | map.put(number, 1); 18 | list.add(number); 19 | } 20 | } 21 | 22 | /* 23 | * @param value: An integer 24 | * @return: Find if there exists any pair of numbers which sum is equal to the value. 25 | */ 26 | public boolean find(int value) { 27 | // write your code here 28 | for (int i = 0; i < list.size(); i++) { 29 | int num1 = list.get(i), num2 = value - num1; 30 | if ((num1 == num2 && map.get(num1) > 1) || 31 | (num1 != num2 && map.containsKey(num2))) 32 | return true; 33 | } 34 | return false; 35 | } 36 | } 37 | 38 | -------------------------------------------------------------------------------- /java/validPalindrome.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | /* 3 | * @param s: A string 4 | * @return: Whether the string is a valid palindrome 5 | */ 6 | public boolean isPalindrome(String s) { 7 | // write your code here 8 | if (s == null || s.length() == 0) { 9 | return true; 10 | } 11 | 12 | int front = 0; 13 | int end = s.length() - 1; 14 | while (front < end) { 15 | while (front < s.length() && !isvalid(s.charAt(front))){ // nead to check range of a/b 16 | front++; 17 | } 18 | 19 | if (front == s.length()) { // for emtpy string “.,,,” 20 | return true; 21 | } 22 | 23 | while (end >= 0 && ! isvalid(s.charAt(end))) { // same here, need to check border of a,b 24 | end--; 25 | } 26 | 27 | if (Character.toLowerCase(s.charAt(front)) != Character.toLowerCase(s.charAt(end))) { 28 | break; 29 | } else { 30 | front++; 31 | end--; 32 | } 33 | } 34 | 35 | return end <= front; 36 | } 37 | 38 | private boolean isvalid (char c) { 39 | return Character.isLetter(c) || Character.isDigit(c); 40 | } 41 | } 42 | 43 | -------------------------------------------------------------------------------- /java/validPalindrome2.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean validPalindrome(String s) { 3 | int left = 0, right = s.length() - 1; 4 | 5 | while (left < right) { 6 | if (s.charAt(left) != s.charAt(right)) { 7 | break; 8 | } 9 | left++; 10 | right--; 11 | } 12 | 13 | if (left >= right) { 14 | return true; 15 | } 16 | 17 | return isSubPalindrome(s, left + 1, right) || isSubPalindrome(s, left, right - 1); 18 | } 19 | 20 | private boolean isSubPalindrome(String s, int left, int right) { 21 | while (left < right) { 22 | if (s.charAt(left) != s.charAt(right)) { 23 | return false; 24 | } 25 | left++; 26 | right--; 27 | } 28 | 29 | return true; 30 | } 31 | } -------------------------------------------------------------------------------- /menu.md: -------------------------------------------------------------------------------- 1 | [长崎游记](https://github.com/cheungYX/algorithm/blob/master/travel/nagazaki_20180603.md)
2 | [武汉旅行轶事](https://github.com/cheungYX/algorithm/blob/master/travel/wuhan_20181003.md)
3 | [2019南非跨年游记](https://github.com/cheungYX/algorithm/blob/master/travel/south_african_2018.md)
4 | [以色列游记:残垣断壁里的凤凰涅槃](https://github.com/cheungYX/algorithm/blob/master/travel/israel_2017.md)
5 | [冲绳游记1](https://github.com/cheungYX/algorithm/blob/master/travel/okinawa_20190119.md)
6 | [冲绳游记2](https://github.com/cheungYX/algorithm/blob/master/travel/okinawa_20190301.md)
7 | [石垣島游记](https://github.com/cheungYX/algorithm/blob/master/travel/ishigaki_20190405.md)
8 | [北海道紋別游记](https://github.com/cheungYX/algorithm/blob/master/travel/monbetsu_20190221.md)
9 | [台湾垦丁游记](https://github.com/cheungYX/algorithm/blob/master/travel/kending_20190711.md)
10 | [奄美大岛潜水](https://github.com/cheungYX/algorithm/blob/master/travel/amami_20190830.md)
11 | [神津岛潜水](https://github.com/cheungYX/algorithm/blob/master/travel/kozu_20190920.md)
12 | [夏威夷游记](https://github.com/cheungYX/algorithm/blob/master/travel/hawaii_20191010.md)
13 | [冰岛游记](https://github.com/cheungYX/algorithm/blob/master/travel/iceland_20191227.md)
14 | [ANA里程攻略](https://github.com/cheungYX/algorithm/blob/master/cheung/ana.md)
15 | [东京购房攻略](https://github.com/cheungYX/algorithm/blob/master/cheung/ff.md)
16 | [保险攻略](https://github.com/cheungYX/algorithm/blob/master/cheung/hokken.md)
17 | [职业规划篇](https://github.com/cheungYX/algorithm/blob/master/cheung/work_root.md)
18 | [理财](https://github.com/cheungYX/algorithm/blob/master/cheung/money.md) 19 | -------------------------------------------------------------------------------- /ruby/longest_palindrome.rb: -------------------------------------------------------------------------------- 1 | # @param {String} s 2 | # @return {Integer} 3 | def longest_palindrome(s) 4 | return 0 if s.nil? || s.size == 0 5 | map = {} 6 | s.chars.map do |v| 7 | if map.has_key?(v) 8 | map.delete(v) 9 | else 10 | map[v] = 1 11 | end 12 | end 13 | cnt = map.size 14 | cnt -= 1 if map.size > 0 15 | return s.size - cnt 16 | end 17 | 18 | -------------------------------------------------------------------------------- /ruby/merge_two_sorted_lists.rb: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode 3 | # attr_accessor :val, :next 4 | # def initialize(val) 5 | # @val = val 6 | # @next = nil 7 | # end 8 | # end 9 | 10 | # @param {ListNode} l1 11 | # @param {ListNode} l2 12 | # @return {ListNode} 13 | def merge_two_lists(l1, l2) 14 | return l2 if l1.nil? 15 | return l1 if l2.nil? 16 | 17 | head = ListNode.new(-1) 18 | tmp = ListNode.new(-1) 19 | 20 | head = tmp 21 | 22 | while (l1 != nil && l2 != nil) 23 | if l1.val <= l2.val 24 | tmp.next = l1 25 | l1 = l1.next 26 | else 27 | tmp.next = l2 28 | l2 = l2.next 29 | end 30 | tmp = tmp.next 31 | end 32 | 33 | tmp.next = l1 if l1 != nil 34 | tmp.next = l2 if l2 != nil 35 | head.next 36 | end 37 | 38 | -------------------------------------------------------------------------------- /ruby/reverseVowelsOfAString.rb: -------------------------------------------------------------------------------- 1 | def reverse_vowels(s) 2 | vowel = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'] 3 | l = 0 4 | r = s.size - 1 5 | while l < r 6 | if vowel.include?(s[l]) 7 | if vowel.include?(s[r]) 8 | swap(s, l, r) 9 | l += 1 10 | r -= 1 11 | else 12 | r -= 1 13 | end 14 | else 15 | l += 1 16 | end 17 | end 18 | s 19 | end 20 | 21 | def swap(s, l, r) 22 | tmp = s[l] 23 | s[l] = s[r] 24 | s[r] = tmp 25 | end 26 | 27 | -------------------------------------------------------------------------------- /ruby/str_str.rb: -------------------------------------------------------------------------------- 1 | def strstr(source, target) 2 | return -1 if source.to_s.empty? || target.to_s.empty? 3 | for i in 0..source.size - 1 4 | return i if source[i..i + target.size - 1] == target 5 | end 6 | return -1 7 | end 8 | -------------------------------------------------------------------------------- /ruby/two_sum.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer[]} nums 2 | # @param {Integer} target 3 | # @return {Integer[]} 4 | def two_sum(nums, target) 5 | numbers_hash = {} 6 | nums.each_with_index do |number, index| 7 | numbers_hash[index] = number 8 | hash_key = ((numbers_hash.select { |k,v| v == (target - number) }.keys) - [index]) 9 | hash_key.empty? ? false : (return [hash_key, index].flatten) 10 | end 11 | return [] 12 | end 13 | -------------------------------------------------------------------------------- /ruby/valid_palindrome.rb: -------------------------------------------------------------------------------- 1 | def is_palindrome(s) 2 | return true if s.to_s.empty? 3 | s = s.downcase.scan(/\w/) 4 | return s == s.reverse 5 | end 6 | 7 | -------------------------------------------------------------------------------- /tmp/12311566973056.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/12311566973056.jpg -------------------------------------------------------------------------------- /tmp/12321566973057.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/12321566973057.jpg -------------------------------------------------------------------------------- /tmp/12331566973058.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/12331566973058.jpg -------------------------------------------------------------------------------- /tmp/12341566973059.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/12341566973059.jpg -------------------------------------------------------------------------------- /tmp/12351566973060.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/12351566973060.jpg -------------------------------------------------------------------------------- /tmp/12361566973061.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/12361566973061.jpg -------------------------------------------------------------------------------- /tmp/12371566973062.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/12371566973062.jpg -------------------------------------------------------------------------------- /tmp/12391566973651.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/12391566973651.jpg -------------------------------------------------------------------------------- /tmp/12401566973652.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/12401566973652.jpg -------------------------------------------------------------------------------- /tmp/12411566973653.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/12411566973653.jpg -------------------------------------------------------------------------------- /tmp/12451566973663.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/12451566973663.jpg -------------------------------------------------------------------------------- /tmp/12461566973666.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/12461566973666.jpg -------------------------------------------------------------------------------- /tmp/12471566973668.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/12471566973668.jpg -------------------------------------------------------------------------------- /tmp/13401569471602.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/13401569471602.jpg -------------------------------------------------------------------------------- /tmp/13421569471605.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/13421569471605.jpg -------------------------------------------------------------------------------- /tmp/13431569471606.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/13431569471606.jpg -------------------------------------------------------------------------------- /tmp/13441569471620.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/13441569471620.jpg -------------------------------------------------------------------------------- /tmp/13451569471623.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/13451569471623.jpg -------------------------------------------------------------------------------- /tmp/13471569471629.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/13471569471629.jpg -------------------------------------------------------------------------------- /tmp/13481569471639.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/13481569471639.jpg -------------------------------------------------------------------------------- /tmp/150740.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/150740.jpg -------------------------------------------------------------------------------- /tmp/150759.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/150759.jpg -------------------------------------------------------------------------------- /tmp/150807.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/150807.jpg -------------------------------------------------------------------------------- /tmp/150835.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/150835.jpg -------------------------------------------------------------------------------- /tmp/150847.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/150847.jpg -------------------------------------------------------------------------------- /tmp/20190221001.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/20190221001.jpg -------------------------------------------------------------------------------- /tmp/20190221002.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/20190221002.jpg -------------------------------------------------------------------------------- /tmp/20190221003.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/20190221003.jpg -------------------------------------------------------------------------------- /tmp/20190221004.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/20190221004.jpg -------------------------------------------------------------------------------- /tmp/20190221005.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/20190221005.jpg -------------------------------------------------------------------------------- /tmp/20190221006.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/20190221006.jpg -------------------------------------------------------------------------------- /tmp/20190221007.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/20190221007.jpg -------------------------------------------------------------------------------- /tmp/20190221008.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/20190221008.jpg -------------------------------------------------------------------------------- /tmp/20190221009.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/20190221009.jpg -------------------------------------------------------------------------------- /tmp/20190221010.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/20190221010.jpg -------------------------------------------------------------------------------- /tmp/20190221011.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/20190221011.jpg -------------------------------------------------------------------------------- /tmp/20190221012.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/20190221012.jpg -------------------------------------------------------------------------------- /tmp/20190301_203532.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/20190301_203532.jpg -------------------------------------------------------------------------------- /tmp/20190302_141115.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/20190302_141115.jpg -------------------------------------------------------------------------------- /tmp/20190303_115043.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/20190303_115043.jpg -------------------------------------------------------------------------------- /tmp/20190303_121425.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/20190303_121425.jpg -------------------------------------------------------------------------------- /tmp/20190303_135842.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/20190303_135842.jpg -------------------------------------------------------------------------------- /tmp/20190303_153546.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/20190303_153546.jpg -------------------------------------------------------------------------------- /tmp/20190303_164212.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/20190303_164212.jpg -------------------------------------------------------------------------------- /tmp/441567669203.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/441567669203.jpg -------------------------------------------------------------------------------- /tmp/5721550414890.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/5721550414890.jpg -------------------------------------------------------------------------------- /tmp/5731550414891.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/5731550414891.jpg -------------------------------------------------------------------------------- /tmp/5741550414892.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/5741550414892.jpg -------------------------------------------------------------------------------- /tmp/5751550414893.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/5751550414893.jpg -------------------------------------------------------------------------------- /tmp/5761550414894_.pic_hd.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/5761550414894_.pic_hd.jpg -------------------------------------------------------------------------------- /tmp/5771550414895.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/5771550414895.jpg -------------------------------------------------------------------------------- /tmp/5791550414897_.pic_hd.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/5791550414897_.pic_hd.jpg -------------------------------------------------------------------------------- /tmp/5801550414898.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/5801550414898.jpg -------------------------------------------------------------------------------- /tmp/5821550414927.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/5821550414927.jpg -------------------------------------------------------------------------------- /tmp/5831550414929_.pic_hd.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/5831550414929_.pic_hd.jpg -------------------------------------------------------------------------------- /tmp/5841550415359.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/5841550415359.jpg -------------------------------------------------------------------------------- /tmp/5871550417469.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/5871550417469.jpg -------------------------------------------------------------------------------- /tmp/6211551016325.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/6211551016325.jpg -------------------------------------------------------------------------------- /tmp/6231551437558.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/6231551437558.jpg -------------------------------------------------------------------------------- /tmp/6241551437561.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/6241551437561.jpg -------------------------------------------------------------------------------- /tmp/6711553250282.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/6711553250282.jpg -------------------------------------------------------------------------------- /tmp/6721553250719.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/6721553250719.jpg -------------------------------------------------------------------------------- /tmp/6751553250722.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/6751553250722.jpg -------------------------------------------------------------------------------- /tmp/6761553250723.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/6761553250723.jpg -------------------------------------------------------------------------------- /tmp/6771553250724.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/6771553250724.jpg -------------------------------------------------------------------------------- /tmp/6781553250725.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/6781553250725.jpg -------------------------------------------------------------------------------- /tmp/6791553250726.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/6791553250726.jpg -------------------------------------------------------------------------------- /tmp/6811553250728.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/6811553250728.jpg -------------------------------------------------------------------------------- /tmp/6821553250931.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/6821553250931.jpg -------------------------------------------------------------------------------- /tmp/6831553251677.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/6831553251677.jpg -------------------------------------------------------------------------------- /tmp/6861553251681.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/6861553251681.jpg -------------------------------------------------------------------------------- /tmp/6871553251682.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/6871553251682.jpg -------------------------------------------------------------------------------- /tmp/6881553251683.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/6881553251683.jpg -------------------------------------------------------------------------------- /tmp/6891553251683.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/6891553251683.jpg -------------------------------------------------------------------------------- /tmp/6901553251684.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/6901553251684.jpg -------------------------------------------------------------------------------- /tmp/6911553251685.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/6911553251685.jpg -------------------------------------------------------------------------------- /tmp/6921553251686.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/6921553251686.jpg -------------------------------------------------------------------------------- /tmp/6931553252064.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/6931553252064.jpg -------------------------------------------------------------------------------- /tmp/6941553252065.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/6941553252065.jpg -------------------------------------------------------------------------------- /tmp/6951553252065.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/6951553252065.jpg -------------------------------------------------------------------------------- /tmp/6961553252066.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/6961553252066.jpg -------------------------------------------------------------------------------- /tmp/6971553252067.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/6971553252067.jpg -------------------------------------------------------------------------------- /tmp/7681554555106.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/7681554555106.jpg -------------------------------------------------------------------------------- /tmp/7691554555107.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/7691554555107.jpg -------------------------------------------------------------------------------- /tmp/7711554678212.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/7711554678212.jpg -------------------------------------------------------------------------------- /tmp/7721554678216.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/7721554678216.jpg -------------------------------------------------------------------------------- /tmp/7731554678217.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/7731554678217.jpg -------------------------------------------------------------------------------- /tmp/7771554678221.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/7771554678221.jpg -------------------------------------------------------------------------------- /tmp/7781554678222.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/7781554678222.jpg -------------------------------------------------------------------------------- /tmp/7791554678254.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/7791554678254.jpg -------------------------------------------------------------------------------- /tmp/7811554678256.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/7811554678256.jpg -------------------------------------------------------------------------------- /tmp/7851554678260.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/7851554678260.jpg -------------------------------------------------------------------------------- /tmp/7861554678261.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/7861554678261.jpg -------------------------------------------------------------------------------- /tmp/a1f7f7f4da19b06d929fbc05ba225459.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/tmp/a1f7f7f4da19b06d929fbc05ba225459.png -------------------------------------------------------------------------------- /travel/amami_20190830.md: -------------------------------------------------------------------------------- 1 | 抢到了lcc的特价票,
2 | 一人单程才4000日元,
3 | 回程用积累的里程换了ana的票,
4 | 所以就有了这次奄美大島的旅行.
5 | 6 | 里程用的好可以节省不是旅游的费用,
7 | 赶紧跟着莘哥一起刷起里程来。
8 | 9 | 下午1点多下飞机后坐上潜店来接的车直接去潜水,
10 | 奄美大島真是十分推荐,
11 | 海不输给冲绳和各种离岛,
12 | 价格要合适的多,
13 | 最重要的是还没有过多的游客,
14 | 15 | 推荐一下这家潜店和住宿
16 | 海景房,楼下就是海滩,
17 | 随时可以浮潜
18 | 19 | 奄美大島虽然属于鹿儿岛县,
20 | 但是不管是地理上还是各种民俗风情都和冲绳更接近。
21 | 与其说是鹿儿岛的离岛,不如说是冲绳的离岛吧。
22 | 23 | 奄美大島的海温暖
24 | 珊瑚礁多,
25 | 有多彩的热带鱼,
26 | 生物多样性丰富,
27 | 是潜水的好地方.
28 | 29 | 第二天早上浮潜后转机去了鹿儿岛,
30 | 鹿儿岛是南九州最繁华的大都市,
31 | 32 | 鹿儿岛还是明治维新的发源地,
33 | 明治三杰之一的本乡隆盛的根据地,
34 | 喜欢历史的朋友可以在各种博物馆体验这一段跌宕起伏的历史
35 | 36 | 活火山樱岛是鹿儿岛的地标,
37 | 至今还冒着烟时不时的喷发一次显示存在感,
38 | 可以爬山城上公园,隔江对望樱岛,
39 | 也可以乘船到岛上泡个温泉看个夕阳,
40 | 岛上其实没有什么景点,
41 | 火山的高温和地貌也没有过多的生机,
42 | 不是太推荐多花时间游玩
43 | 44 | 鹿儿岛地处九州,
45 | 和朝鲜半岛交流频繁,
46 | 大航海时代从荷兰的货船也带来了许多新的交流,
47 | 所以自然是少不了好吃的。
48 | 49 | 九州也出好酒,
50 | 离海近所以海鲜也新鲜
51 | 所以夜晚可以在居酒屋吃一吃地道的本地料理
-------------------------------------------------------------------------------- /travel/hawaii_20191010.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/travel/hawaii_20191010.md -------------------------------------------------------------------------------- /travel/iceland_20191227.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cheungYX/algorithm/cf25185533ae2aa97fc3f7c2095cc14823f579ce/travel/iceland_20191227.md -------------------------------------------------------------------------------- /travel/kozu_20190920.md: -------------------------------------------------------------------------------- 1 | 定了PADI的神津岛潜水tour,
2 | 周五的晚上在竹芝坐船出发,
3 | 第二天早上8点40到达神津岛,
4 | 上船后可以先找一个甲板的好位置,
5 | 看看东京湾的夜景,
6 | 酒喝到微醺,然后一路睡死过去是最好的,
7 | 東海汽船提前预定可以打折,
8 | 座椅比较窄,
9 | 比较大只的同学可能不好睡。
10 | 11 | * 东京湾彩虹桥 12 | - ![东京湾彩虹桥](../tmp/13401569471602.jpg) 13 | 14 | 船会一路在各个离岛停留,
15 | 所以早上会比较吵,
16 | 可以早起看一个日出
17 | 18 | * ![阴天](../tmp/13481569471639.jpg) 19 | 20 | 到了岛上民宿的staff来接,
21 | 安顿好后直接去潜店准备潜水,
22 | 岛不大,居住区都在港口附近的一块平地
23 | 所以还算方便,
24 | 岛上没有便利店,
25 | 杂货店也在晚上7点就关门,
26 | 采购要提早。
27 | 28 | 岛上居住设施不多,
29 | 要提早联系计划,
30 | 31 | * 岛上主要景点 32 | - ![主要景点](../tmp/441567669203.jpg) 33 | 34 | 神津岛虽然没有比较好的珊瑚礁,
35 | 但是游客比较少,
36 | 加上黒潮下流带来的暖流与营养,物种丰富,
37 | 海底的白沙滩也是难得一见的风景。
38 | 可惜这次来台风在附近,
39 | 水比较浑浊,
40 | 即使如此也比东京附近的潜点干净且清澈的多。
41 | 42 | * 潜点 43 | - ![潜点1](../tmp/13421569471605.jpg) 44 | * 白沙滩与鱼 45 | - ![白沙滩与鱼](../tmp/13451569471623.jpg) 46 | - ![白沙滩与鱼](../tmp/13431569471606.jpg) 47 | - ![白沙滩与鱼](../tmp/13471569471629.jpg) 48 | 49 | 岛的北美有个有趣的赤崎遊歩道,
50 | 跳水浮潜自由潜都很适合,
51 | 岛上的交通手段不多,
52 | 巴士也只到下午4点多就结束营业,
53 | 54 | * 温泉疗养中心前的海湾 55 | - ![潜点1](../tmp/13441569471620.jpg) 56 | 57 | 岛上灯光污染少,
58 | 夜晚可以看到很美的星空,
59 | 可以带着帐篷来岛上野营,
60 | 看着星空听着海淘十分惬意。
61 | 62 | 回程去东京的9点半出发17点到,
63 | 有点浪费时间,
64 | 推荐3个小时的汽艇,
65 | 不过在船上远离网络倒是很适合读书学习。
66 | 67 | 周末想逃离都市的话,
68 | 东京的各个离岛都是不错的选择。
--------------------------------------------------------------------------------