Algorithms DP-01 LPS

Dynamic Programming Leetcode 5. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example 1: Input: "babad" Output: "bab" Note: "aba" is also a valid answer. Example 2: Input: "cbbd" Output: "bb" 解题思路 1. 根据回文类型 将回文串分为三种: 第一种是对称式回文串,比如:acddca 第二种是重复式的,比如:aaaaa 第三种是夹心饼干使的:acbbca 针对这三种情况来分析 C代码 2. 马拉车(Maracher)算法 马拉车(Maracher)算法 这道题用c语言的马拉车算法反而更耗时,因为需要频繁地申请内存,可能其他语言会更加高效。 C代码

1 min · 62 words · Me

Algorithms Q & A

Q 每瓶啤酒2元,2个空酒瓶或4个瓶盖可换1瓶啤酒。先付钱再喝酒,不能赊账,请实现一个函数计算N元钱最多可喝多少瓶啤酒? A int getSum(int n) { if(n < 2) return 0; int x = n / 2; int y = n / 2; int sum = x; //printf("%d %d %d\n", x, y, sum); while (x >= 2 || y >= 4) { while(x >= 2) { sum += 1; x -= 2; x += 1; y += 1; } while(y >= 4) { sum += 1; x += 1; y -= 4; y += 1; } } //printf("%d\n", sum); return sum; }

1 min · 83 words · Me