LeetCode刷题(回文子串、数字的补数、密钥格式化、最大连续1的个数)

1.回文子串
1.1 题目描述

给定一个字符串,计算字符串中的回文个数,相同字母在不同位置也被视为不同的子串

例如:
输入:“abc”
输出:3 “a” “b” “c”
输入:“aaa”
输出:6 “a”,“a”,“a”,“aa”,“aa”,“aaa”

题目传送门

1.2题目分析

比较笨的办法:一个字符串的最少回文就是自身的字符个数,然后对其进行遍历判断其是否具有其他的回文(如果已经添加了单个字符,就认定单个字符不是回文)。
当然对于这种可以加速的算法也比较多,之前算法课中有一个叫做Manacher(马拉车)的算法,但是对于该算法还处于理解中,也没有用来实现,借用别人的程序。
算法描述:

1.向字符串中加入特殊的字符,例如‘#’或者‘@’
2.记录每个位置为回文中心的回文半径或直径,使用一个回文数组记录
3.更新对比中心,若当前中心不在回文右边界内,暴力破解
若当前的中心在回文右边界中,分为三种情况:
1)某个中心i关于当前中心的对称i’全部在其右边界中
2)某个中心i关于当前中心的对称中心i’的界大于当前中心的界
3)某个中心i关于C对称点I‘的回文半径与C的回文半径相同
例如:
几种情况的图示

1.3代码如下
class Solution {
    public int countSubstrings(String s) {
       int left = 0;
		int right = s.length();
		int count = s.length();
		for (; left < right; left++) {
			int temp = right - 1;
			while (left < temp) {
				int temp2 = temp + 1;
				if (isHui(s.substring(left, temp2))) {
					count++;
				}
				temp--;
			}
		}
		return count;
    }
    //判断字符串是否为回文串,默认单个字符不是回文
    public  boolean isHui(String s) {
		if (s.length() == 0 || s.length() == 1)
			return false;
		int left = 0;
		int right = s.length() - 1;
		while (left < right) {
			if (s.charAt(left) != s.charAt(right))
				return false;
			left++;
			right--;
		}
		return true;
	}
}

Manacher算法的代码

class Solution {
    public int countSubstrings(String s) {
        int n = s.length();
        StringBuffer t = new StringBuffer("$#");
        for (int i = 0; i < n; ++i) {
            t.append(s.charAt(i));
            t.append('#');
        }
        n = t.length();
        t.append('!');

        int[] f = new int[n];
        int iMax = 0, rMax = 0, ans = 0;
        for (int i = 1; i < n; ++i) {
            // 初始化 f[i]
            f[i] = i <= rMax ? Math.min(rMax - i + 1, f[2 * iMax - i]) : 1;
            // 中心拓展
            while (t.charAt(i + f[i]) == t.charAt(i - f[i])) {
                ++f[i];
            }
            // 动态维护 iMax 和 rMax
            if (i + f[i] - 1 > rMax) {
                iMax = i;
                rMax = i + f[i] - 1;
            }
            // 统计答案, 当前贡献为 (f[i] - 1) / 2 上取整
            ans += f[i] / 2;
        }

        return ans;
    }
}
2.数字的补数
2.1 题目描述

给定一个整数,输出其数的补数,一个数的补数是该数二进制取反的数字

例如:
输入:5
输出:2 5的二进制为101,取反为010(2)
题目传送门

2.2 题目分析

对于一个十进制的数字将其转换成二进制,采用除数取余法,将余数取出来进行取反1变为0,0变为1,那就是将余数与数字1进行异或

1^0 = 1
1^1 = 0
达到了一个取反的效果

2.3 代码如下
class Solution {
    public int findComplement(int num) {
        int count = 0;
		int carry = 1;
		while (num > 0) {
			int temp = num % 2;
			count += carry * (1 ^ temp);
			carry <<= 1;
			num /= 2;
		}
		return count;
    }
}
3.密钥格式化
3.1 题目描述

给定一个密钥,一个字符串S,和一个整数K,要求将字符串中的所有非’-‘字符分为除了第一组外其余各组元素的个数均为K,每组之间用’-'分割开,并将字符串中的字符全部转换成大写字母。

例如:
输入:S=”5F3Z-2e-9-w“,K=4
输出为:S=”5F3Z-2E9W“
题目传送门

3.2题目分析

从后向前一次遍历字符串,将每个不为’-'的字符放在StringBuilder中,并将小写字符转换成大写字符。

3.3代码如下
class Solution {
    public String licenseKeyFormatting(String S, int K) {
        int index = S.length() - 1;
		StringBuilder sb = new StringBuilder();
		int count = 0;
		for (; index >= 0; index--) {
			char ch = S.charAt(index);
			if (count == K) {
				sb.append('-');
				count = 0;
			}
			if ((ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')) {
				count++;
				sb.append(ch);
			} else if (ch >= 'a' && ch <= 'z') {
				count++;
				sb.append(toUpper(ch));
			}
		}
        if (sb.length() == 0)
			return "";
		String res = sb.reverse().toString();
		//取出多余的'-'
		if (res.charAt(0) == '-')
			res = res.substring(1);
		return res;
    }
    //将大写转换成小写
    public  char toUpper(char ch) {
		return (char) (ch - 32);
	}
}
4.最大连续1个数
4.1 题目描述

给定一个01组成的二进制数组,计算数组中连续出现的1的个数,求数组中最大连续1个数。

例如
输入:[1,1,0,0,1,1,1]
输出:3

题目传送门

4.2题目分析

循环遍历统计1的个数,较为简单

4.3代码如下
class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        int index = 0;
		int count = Integer.MIN_VALUE;
		int temp = 0;
		for (; index < nums.length; index++) {
			if (nums[index] == 1)
				temp = temp + 1;
			else {
				count = temp > count ? temp : count;
				temp = 0;
			}
		}
		count = temp > count ? temp : count;
		return count;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值