+关注继续查看

# 1. 外观数列

「外观数列」是一个整数序列，从数字 1 开始，序列中的每一项都是对前一项的描述。

countAndSay(1) = "1"

countAndSay(n) 是对 countAndSay(n-1) 的描述，然后转换成另一个数字字符串。

1.     1

2.     11

3.     21

4.     1211

5.     111221

countAndSay(1) = "1"

countAndSay(2) = 读 "1" = 一 个 1 = "11"

countAndSay(3) = 读 "11" = 二 个 1 = "21"

countAndSay(4) = 读 "21" = 一 个 2 + 一 个 1 = "12" + "11" = "1211"

1 <= n <= 30

https://edu.csdn.net/practice/26974821

import java.util.*;
public class countAndSay {
public static class Solution {
public String countAndSay(int n) {
String pre = "1";
for (int i = 1; i < n; i++) {
StringBuilder temp = new StringBuilder();
char c = pre.charAt(0);
int cnt = 1;
for (int j = 1; j < pre.length(); j++) {
char cc = pre.charAt(j);
if (c == cc) {
cnt++;
} else {
temp.append(cnt).append(c);
cnt = 1;
c = cc;
}
}
temp.append(cnt).append(c);
pre = temp.toString();
}
return pre;
}
}
public static void main(String[] args) {
Solution s = new Solution();
System.out.println(s.countAndSay(1));
System.out.println(s.countAndSay(4));
}
}

1

1211

# 2. 将有序数组转换为二叉搜索树

1 <= nums.length <= 10^4

-10^4 <= nums[i] <= 10^4

nums 按 严格递增 顺序排列

https://edu.csdn.net/practice/26974822

public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {
}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
return traversal(nums, 0, nums.length - 1);
}
public TreeNode traversal(int[] nums, int left, int right) {
if (left > right)
return null;
int mid = left + ((right - left) / 2);
TreeNode node = new TreeNode(nums[mid]);
node.left = traversal(nums, left, mid - 1);
node.right = traversal(nums, mid + 1, right);
return node;
}
}

# 3. 翻转字符串里的单词

输入字符串 s 可以在前面、后面或者单词间包含多余的空格。

翻转后单词间应当仅用一个空格分隔。

翻转后的字符串中不应包含额外的空格。

1 <= s.length <= 10^4

s 包含英文大小写字母、数字和空格 ' '

s 中 至少存在一个 单词

请尝试使用 O(1) 额外空间复杂度的原地解法。

https://edu.csdn.net/practice/26974823

import java.util.*;
public class reverseWords {
public static class Solution {
public StringBuilder trimSpaces(String s) {
int left = 0, right = s.length() - 1;
while (left <= right && s.charAt(left) == ' ')
++left;
while (left <= right && s.charAt(right) == ' ')
--right;
StringBuilder sb = new StringBuilder();
while (left <= right) {
char c = s.charAt(left);
if (c != ' ')
sb.append(c);
else if (sb.charAt(sb.length() - 1) != ' ')
sb.append(c);
++left;
}
return sb;
}
public void reverse(StringBuilder sb, int left, int right) {
while (left < right) {
char tmp = sb.charAt(left);
sb.setCharAt(left++, sb.charAt(right));
sb.setCharAt(right--, tmp);
}
}
public void reverseEachWord(StringBuilder sb) {
int n = sb.length();
int start = 0, end = 0;
while (start < n) {
while (end < n && sb.charAt(end) != ' ')
++end;
reverse(sb, start, end - 1);
start = end + 1;
++end;
}
}
public String reverseWords(String s) {
StringBuilder sb = trimSpaces(s);
reverse(sb, 0, sb.length() - 1);
reverseEachWord(sb);
return sb.toString();
}
}
public static void main(String[] args) {
Solution s = new Solution();
String str = "the sky is blue";
System.out.println(s.reverseWords(str));
str = "  hello world  ";
System.out.println(s.reverseWords(str));
str = "a good   example";
System.out.println(s.reverseWords(str));
str = "  Bob    Loves  Alice   ";
System.out.println(s.reverseWords(str));
str = "Alice does not even like bob";
System.out.println(s.reverseWords(str));
}
}

blue is sky the

world hello

example good a

Alice Loves Bob

bob like even not does Alice

|
4月前
|
Java
Java每日一练(20230518) 移除元素、跳跃游戏II、复原IP地址
Java每日一练(20230518) 移除元素、跳跃游戏II、复原IP地址
44 0
|
4月前
|

Java每日一练(20230517) 重复元素、链表重复元素、旋转数组
Java每日一练(20230517) 重复元素、链表重复元素、旋转数组
34 0
|
4月前
|
Java
Java每日一练(20230516) 最小栈、组合总和II、相同的树
Java每日一练(20230516) 最小栈、组合总和II、相同的树
36 0
|
4月前
|

Java每日一练(20230515) 阶乘后的零、矩阵置零、两数相除
Java每日一练(20230515) 阶乘后的零、矩阵置零、两数相除
58 0
|
4月前
|
Java
Java每日一练(20230514) 滑动窗、最大子序和、转罗马数字
Java每日一练(20230514) 滑动窗、最大子序和、转罗马数字
50 0
|
4月前
|

Java每日一练(20230513) 输出最值、盛水容器、旋转数组II
Java每日一练(20230513) 输出最值、盛水容器、旋转数组II
46 0
|
4月前
|
Java 索引
Java每日一练(20230512) 最大间距、串联子串、最长回文子串
Java每日一练(20230512) 最大间距、串联子串、最长回文子串
39 0
|
4月前
|
Java 索引
Java每日一练(20230511) 有效数字、重复元素II、类和子类
Java每日一练(20230511) 有效数字、重复元素II、类和子类
53 0
|
4月前
|
Java 容器
Java每日一练(20230510) 生成器类、螺旋矩阵II、删除链表的重复元素II
Java每日一练(20230510) 生成器类、螺旋矩阵II、删除链表的重复元素II
47 0
|
4月前
|

Java每日一练(20230509) 下一个排列、分隔链表、随机指针链表
Java每日一练(20230509) 下一个排列、分隔链表、随机指针链表
33 0