1. 重复的子字符串
题目描述:
给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成。给定的字符串只含有小写英文字母,并且长度不超过10000。
代码:
package leetcode.week8;
/**
* @author chengzhengda
* @version 1.0
* @date 2020-02-25 17:39
* @desc
*/
public class t459 {
public static boolean repeatedSubstringPattern(String s) {
String str = s + s;
String s1 = str.substring(1, str.length() - 1);
if (s1.indexOf(s) > -1) {
return true;
}
return false;
}
public static void main(String[] args) {
String str = "abab";
System.out.println(repeatedSubstringPattern(str));
}
}
2. 下一个更大元素
题目描述:
给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素。数字 x 的下一个更大的元素是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你应该循环地搜索它的下一个更大的数。如果不存在,则输出 -1。
代码:
package leetcode.week8;
import java.util.Stack;
/**
* @author chengzhengda
* @version 1.0
* @date 2020-02-25 17:03
* @desc
*/
public class t503 {
public static int[] nextGreaterElements(int[] nums) {
int[] res = new int[nums.length];
Stack<Integer> stack = new Stack<>();
for (int i = 2 * nums.length - 1; i >= 0; i--) {
while (!stack.empty() && nums[stack.peek()] <= nums[i % nums.length]) {
stack.pop();
}
res[i % nums.length] = stack.empty() ? -1 : nums[stack.peek()];
stack.push(i % nums.length);
}
return res;
}
public static void main(String[] args) {
int[] nums = {1, 2, 3, 4, 1, 2};
int[] res = nextGreaterElements(nums);
for (int i = 0; i < res.length; i++) {
System.out.println(res[i]);
}
}
}
3. 字符串相加
题目描述:
给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。
注意:
num1 和num2 的长度都小于 5100.
num1 和num2 都只包含数字 0-9.
num1 和num2 都不包含任何前导零。
你不能使用任何內建 BigInteger 库, 也不能直接将输入的字符串转换为整数形式。
代码:
package leetcode.week8;
/**
* @author chengzhengda
* @version 1.0
* @date 2020-02-24 21:12
* @desc 字符串相加
*/
public class t1250 {
public static String addStrings(String num1, String num2) {
StringBuilder stringBuilder = new StringBuilder();
int maxLength = num1.length() >= num2.length() ? num1.length() : num2.length();
int minLength = num1.length() < num2.length() ? num1.length() : num2.length();
int i = maxLength - 1;
int j = minLength - 1;
int join = 0;
int sum = 0;
while (i >= 0 && j >= 0) {
sum = num1.length() < maxLength ? num1.charAt(j) - '0' + num2.charAt(i) - '0' + join :
num1.charAt(i) - '0' + num2.charAt(j) + join - '0';
join = sum > 9 ? 1 : 0;
stringBuilder.append(sum > 9 ? sum % 10 : sum);
i--;
j--;
}
while (i >= 0) {
sum = num1.length() < maxLength ? num2.charAt(i) - '0' + join : num1.charAt(i) - '0' + join;
join = sum > 9 ? 1 : 0;
stringBuilder.append(sum > 9 ? sum % 10 : sum);
i--;
}
if (join > 0) {
stringBuilder.append(join);
}
return stringBuilder.reverse().toString();
}
public static void main(String[] args) {
System.out.println(addStrings("9", "1"));
}
}
4. 数组中数字出现的次数
题目描述:
一个整型数组 nums 里除两个数字之外,其他数字都出现了两次。请写程序找出这两个只出现一次的数字。要求时间复杂度是O(n),空间复杂度是O(1)。
代码:
package leetcode.week8;
/**
* @author chengzhengda
* @version 1.0
* @date 2020-02-25 22:02
* @desc
*/
public class t1535 {
public static int[] singleNumbers(int[] nums) {
int ret[] = new int[2];
int xor = 0;
for (int i = 0; i < nums.length; i++) {
xor ^= nums[i];
}
int lastOne = xor & -xor;
int a = 0;
int b = 0;
for (int i = 0; i < nums.length; i++) {
if ((nums[i] & lastOne) == 0) {
a ^= nums[i];
} else {
b ^= nums[i];
}
}
ret[0] = a;
ret[1] = b;
return ret;
}
public static void main(String[] args) {
int[] nums = {1, 3, 4, 5, 3, 4, 5, 2};
int res[] = singleNumbers(nums);
for (int r : res) {
System.out.println(r);
}
}
}