# 题目一

#### 剑指 Offer II 027. 回文链表

``````输入: head = [1,2,3,3,2,1]

``````

``````输入: head = [1,2]

``````

• 链表 L 的长度范围为 `[1, 105]`
• `0 <= node.val <= 9`

## 解答过程

```````
/**
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode() {}
*     ListNode(int val) { this.val = val; }
*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
//把头结点存起来
//求链表长度
int size=0;
size++;
}
int[] ans = new int[size];
//把链表放进数组中
for(int i=0;i<size;i++){
}
//双指针遍历
int left=0,right = size-1;
while(left<right){
if(ans[left]!=ans[right]){
return false;
}
left++;
right--;
}

return true;
}
}
``````

# 题目二

#### 131. 分割回文串

``````输入：s = "aab"

``````

``````输入：s = "a"

``````

• `1 <= s.length <= 16`
• `s` 仅由小写英文字母组成

## 解答过程

``````class Solution {
boolean[][] flag;
List<List<String>> list1 = new ArrayList<List<String>>();
List<String> list2 = new ArrayList<String>();
int n;
public List<List<String>> partition(String s) {
n=s.length();
flag = new boolean[n][n];
for (int i = 0; i < n; ++i) {
Arrays.fill(flag[i], true);
}
for (int i = n - 1; i >= 0; --i) {
for (int j = i + 1; j < n; ++j) {
flag[i][j] = (s.charAt(i) == s.charAt(j)) && flag[i + 1][j - 1];
}
}
dfs(s,0);
return list1;
}
private void dfs(String s,int i){
//递归结束条件
if(i==n){
return;
}
for(int j=i;j<n;j++){
if(flag[i][j]){
dfs(s,j+1);
list2.remove(list2.size() - 1);
}
}
}
}
``````

# 题目三

#### 866. 回文素数

``````输入：6

``````

``````输入：8

``````

``````输入：13

``````

• `1 <= N <= 10^8`
• 答案肯定存在，且小于 `2 * 10^8`

# 题目四

#### 204. 计数质数

``````输入：n = 10

``````

``````输入：n = 0

``````

``````输入：n = 1

``````

• `0 <= n <= 5 * 106`

## 解答过程

``````class Solution {
public int countPrimes(int n) {
int[] isPrime = new int[n];
Arrays.fill(isPrime, 1);
int ans = 0;
for (int i = 2; i < n; ++i) {
if (isPrime[i] == 1) {
ans += 1;
if ((long) i * i < n) {
for (int j = i * i; j < n; j += i) {
isPrime[j] = 0;
}
}
}
}
return ans;
}
}
``````

## 结果为

...全文
26 回复 打赏 收藏 举报

2.4w+

2022-07-31 23:30

1.总榜第一：现金三百元

2.总榜第二与第三：Acwing算法课一套（价值150元可提现）

1.月榜前三：请喝奶茶一杯