LeetCode----521(E)、172(E)、26(E)

快期末啦,抓紧刷刷题


521. Longest Uncommon Subsequence I

Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings.

subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string.

The input will be two strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn't exist, return -1.

Example 1:

Input: "aba", "cdc"
Output: 3
Explanation: The longest uncommon subsequence is "aba" (or "cdc"), 
because "aba" is a subsequence of "aba",
but not a subsequence of any other strings in the group of two strings.

Note:

  1. Both strings' lengths will not exceed 100.
  2. Only letters from a ~ z will appear in input strings.


分析:

一开始以为这个和最长公共子序列的方法可能差不多,大概要用一下动态规划,结果后来发现,按照题目的意思,求最长不同的子序列,后来发现,只要两个字符串不同,那么最长的不同子序列就是长度比较长的那个。这道题还有一个升级版,求多个字符串中的最长公共子序列,还没做出来,下周写。

c++实现

class Solution {
public:
    //如果a和b相同,则返回-1;a和b不同,则返回比较长的字符串长度
    //这个题。。很迷
    int findLUSlength(string a, string b) {
        if(a.compare(b) != 0) 
            return max(a.size(), b.size());
        else return -1;
    }
};


172. Factorial Trailing Zeroes

求出n的阶乘末尾零的个数

Given an integer n, return the number of trailing zeroes in n!.

Note: Your solution should be in logarithmic time complexity.

分析:

如果先计算n!,在找出0的个数,那么会超过时间显示

  所以我们要找因数中10的个数,而10可分解为2和5,那么这道题就是计算有多少对5和2

因为5的个数比2少,所以2和5组成的pair的个数由5的个数决定,那么只要找出5的个数即可

观察15! = 有3个5(来自其中的5, 10, 15), 所以计算n/5就可以

但是25! = 有6个5(有5个5来自其中的5, 10, 15, 20, 25, 另外还有1个5来自25=(5*5)的另外一个5)  

所以除了计算n/5, 还要计算n/5/5, n/5/5/5, n/5/5/5/5, ..., n/5/5/5,,,/5直到商为0。

 C++实现

class Solution {
public:
    int trailingZeroes(int n) {
        if(n == 0)
            return 0;
            
        int ans = 0;
        
        while(n > 0) {
            //每5个数产生一个0
            ans += n/5;
            n /= 5;
        } 
        
        return ans;
    }
};


26. Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

For example,
Given input array nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.


分析:

因为数组已经排好序了,只需要考虑如何在不增加空间的情况下移除掉重复的数字,那么就是要考虑位置指针的变化问题。


C++代码

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int ans = nums.size();
        
        if(nums.size() == 0 || nums.size() == 1)
            return nums.size();
        
        int cur = nums[0];
        for(int i = 1; i < nums.size(); i++) {
            if(nums[i] == cur) {
                ans--;
                cur = nums[i];
                nums.erase(nums.begin()+i);
                i--;
            }
            else cur = nums[i];
        }
        return ans;
    }
};


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值