33 篇文章 0 订阅

# 5.1 两题（动态+数据结构）

## 139. 单词拆分

### 个人总结（需要回顾）（suc）

这道题的解体思路就是对于这个字符串的每一个字串都能分辨

dp(n) = dp(j) && dp(j,...,n-1);


class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
boolean[] result = new boolean[s.length() +1 ];

result[0] = true;
for(int i=0;i<=s.length();i++){
for(int j=0; j<i;j++){
if(result[j] && wordDict.contains(s.substring(j,i))){
result[i]=true;
break;
}
}
}
return result[s.length()];
}
}


### 基础知识总结

• contains

这里的contains是从0到len的，len = 原有的数组长度+1；

• substring

• 连接字符串

### 官方解法

public class Solution {
public boolean wordBreak(String s, List<String> wordDict) {
Set<String> wordDictSet = new HashSet(wordDict);
boolean[] dp = new boolean[s.length() + 1];
dp[0] = true;
for (int i = 1; i <= s.length(); i++) {
for (int j = 0; j < i; j++) {
if (dp[j] && wordDictSet.contains(s.substring(j, i))) {
dp[i] = true;
break;
}
}
}
return dp[s.length()];
}
}



## 21. 合并两个有序链表 （suc）

### 个人解法（suc）

/**
* 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 {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
int len1 = 0;
int len2 = 0;
if(list1==null &&  list2==null){
return null;
}
ListNode cur1 = list1;
ListNode cur12 = list2;
List<Integer> newlist = new ArrayList<>();
while(list1!=null){
len1++;
list1 = list1.next;
}
list1 = cur1;
while(list2!=null){
len2++;
list2 = list2.next;
}
list2 = cur12;
System.out.println(newlist);
int[] list = new int[newlist.size()];
for(int i=0;i<newlist.size();i++){
list[i] = newlist.get(i);
}
Arrays.sort(list);
String a = Arrays.toString(list);
System.out.println(a);
int len = newlist.size()-1;
ListNode cur = new ListNode(list[0]);
ListNode cur3 = cur;
int i = 0;
while(len!=0){
len--;

cur.next = new ListNode(list[i+1]);
cur = cur.next;
i++;
}
return cur3;

}
}


### 官方解法

class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) {
return l2;
} else if (l2 == null) {
return l1;
} else if (l1.val < l2.val) {
l1.next = mergeTwoLists(l1.next, l2);
return l1;
} else {
l2.next = mergeTwoLists(l1, l2.next);
return l2;
}
}
}



# 5.3

## 203. 移除链表元素

### 个人总结（suc）

/**
* 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 {
public ListNode removeElements(ListNode head, int val) {
if(cur==null){
return null;
}

while(cur.next!=null){
if(cur.next.val == val){
cur.next = cur.next.next;
}else{
cur = cur.next;
}
}
return null;
}else{
}
}

}
}


### 迭代解法 和 个人尝试（回顾）

class Solution {
public ListNode removeElements(ListNode head, int val) {
/**如果使用递归，需要判断
1.递归什么时候停止，
2.递归返回的参数是什么和递归的连续性
3. 条件限定
*/

// 1.递归什么时候停止，
}

//3.递归的条件限定
}else{
}
}
}


class Solution {
public ListNode removeElements(ListNode head, int val) {
}
}
}



### 迭代解法

class Solution {
public ListNode removeElements(ListNode head, int val) {
while (temp.next != null) {
if (temp.next.val == val) {
temp.next = temp.next.next;
} else {
temp = temp.next;
}
}
}
}



## 144. 二叉树的前序遍历

### 个人总结（suc）

/**
* Definition for a binary tree node.
* 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 List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
pre(root,list);
return list;
}

public void pre(TreeNode root, List<Integer> list) {
if(root==null){
return ;
}
pre(root.left, list);
pre(root.right,list);
}
}


### 对树进行debug


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;
}
}

public class Tree {
@Test
public void preorderTraversal() {
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.left.left = new TreeNode(3);
root.left.right = new TreeNode(4);
List<Integer> list = new ArrayList<>();
pre(root,list);
System.out.println(list);
}

// 返回条件就是走到null，
public void pre(TreeNode root, List<Integer> list) {
if(root==null){
return ;
}

pre(root.left, list);
pre(root.right,list);
}
}


## 94. 二叉树的中序遍历

### 个人总结（suc）

class Solution {
//中序 就是 左中右
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
mid(root,list);
return list;
}

public void mid(TreeNode root, List<Integer> list){
if(root == null){
return;
}
mid(root.left,list);
mid(root.right,list);
}
}


## 145. 二叉树的后序遍历

### 个人总结（suc）

class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
mid(root,list);
return list;
}

public void mid(TreeNode root, List<Integer> list){
if(root == null){
return;
}
mid(root.left,list);
mid(root.right,list);
}
}


# 5.4 剑指 Offer

## 30. 包含min函数的栈

### 个人解法（lose）

public class MinStack {
Stack<Integer> integer = new Stack<>();
Stack<Integer> minlist = new Stack<>();
/** initialize your data structure here. */
public MinStack() {
//栈，后入先出
}

public void push(int x) {

integer.push(x);
int min = fuzu();
minlist.push(min);
}

public void pop() {
integer.pop();
minlist.pop();
}

public int top() {
return integer.peek();
}

public int min(){

return minlist.peek();
}

public int fuzu() {
Stack<Integer> integer1 = (Stack<Integer>) integer.clone();
int min = integer1.peek();
int temp = 0;
while(integer1.empty()==false){
temp = integer1.pop();
if (temp<min){
min = temp;
}
}
return min;
}
}


### 官方解法

class MinStack {
Stack<Integer> xStack = new Stack<>();
Stack<Integer> minStack = new Stack<>();
/** initialize your data structure here. */

public MinStack() {
xStack =new Stack<>();
minStack =new Stack<>();
minStack.push(Integer.MAX_VALUE);
}

public void push(int x) {
xStack.push(x);
minStack.push(Math.min(minStack.peek(), x));
}

public void pop() {
xStack.pop();
minStack.pop();
}

public int top() {
return xStack.peek();
}

public int min() {
return minStack.peek();
}
}



## 06. 从尾到头打印链表

### 个人解法（suc）

class Solution {

}
int[] arr = new int[list.size()];
int i = 0;
while(i<list.size()){
arr[i] = list.get(list.size()-i-1);
i++;
}

return arr;

}
}


### 官方解法

/**
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) { val = x; }
* }
*/
class Solution {
Stack<ListNode> stack = new Stack<ListNode>();
while (temp != null) {
stack.push(temp);
temp = temp.next;
}
int size = stack.size();
int[] print = new int[size];
for (int i = 0; i < size; i++) {
print[i] = stack.pop().val;
}
return print;
}
}



## 24. 反转链表

### 个人总结（suc）

class Solution {
}

return cur;
}
}


### 官方解法

class Solution {
ListNode prev = null;
while (curr != null) {
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
}



## 35. 复杂链表的复制

### 官方解法

class Solution {
Map<Node, Node> cachedNode = new HashMap<Node, Node>();
return null;
}
}
}
}


# 5.5 剑指 Offer

## 05. 替换空格

### 个人解法

    public String replaceSpace(String s) {
String sa = "";
int i =0;
for (String retval: s.split(" ")){
if(i>=1){
sa = sa.concat("%20");
}
sa = sa.concat(retval);
i++;

}
return sa;
}


### 官方解法

class Solution {
public String replaceSpace(String s) {
int length = s.length();
char[] array = new char[length * 3];
int size = 0;
for (int i = 0; i < length; i++) {
char c = s.charAt(i);
if (c == ' ') {
array[size++] = '%';
array[size++] = '2';
array[size++] = '0';
} else {
array[size++] = c;
}
}
String newStr = new String(array, 0, size);
return newStr;

}
}


### 重做-总结

    @Test
public void replaceSpace() {
String s = "We are happy.";
int len = s.length();
char[] chars = new char[len*3];
int size = 0;
char cur;
for (int i = 0; i < len; i++) {
cur = s.charAt(i);
if(cur == ' '){
chars[size++] = '%';
chars[size++] = '2';
chars[size++] = '0';
}else {
chars[size++] = cur;
}
}
String list = new String(chars,0,size);
System.out.println(list);
}


#### 单引号和双引号

• 单引号引的数据 是char类型的 单引号只能引一个字符（表示单个字符）
• 双引号引的数据 是String类型的 而双引号可以引0个及其以上（引用字符串）
char类型的值用单引号引起来的单个字符

String d="a"



## 58 - II. 左旋转字符串

### 个人解法（suc）

class Solution {
public String reverseLeftWords(String s, int n) {

int len = s.length();
char cur;
char[] list = new char[len];
for (int i = n; i < len; i++) {
cur = s.charAt(i);
list[i-n] = cur;
}

for (int i = 0; i < n; i++) {
cur = s.charAt(i);
list[len-n+i] = cur;
}

String l = new String(list, 0, len);
return l;
}
}


### 官方解法

class Solution {
public String reverseLeftWords(String s, int n) {
return s.substring(n, s.length()) + s.substring(0, n);
}
}



## 03. 数组中重复的数字

### 个人解法（suc）（Map）

class Solution {
public int findRepeatNumber(int[] nums) {
for (int i = 0; i < nums.length; i++) {
if(map.get(nums[i])==null){
map.put(nums[i],1);
}else{
map.put(nums[i],map.get(nums[i]) + 1);
}
}
Set<Integer> set= map.keySet();
List<Integer> list = new ArrayList<>();

for (Integer obj: set) {
if(map.get(obj)>1){
}
}
// int[] arr = new int[list.size()];
// for (int i = 0; i < list.size(); i++) {
//     arr[i] = list.get(i);
// }
return list.get(0);
}
}


### 个人解法（suc）（Set）

class Solution {
public int findRepeatNumber(int[] nums) {
for (int i = 0; i < nums.length; i++) {
return (nums[i]);
}
}
return 0;
}
}


## 53 - I. 在排序数组中查找数字 I

### 个人总结

class Solution {
public int search(int[] nums, int target) {
int time = 0;
for (int i = 0; i < nums.length; i++) {
if(nums[i] == target){
time++;
}
}
return (time);
}
}


## 53 - II. 0～n-1中缺失的数字

### 个人总结

class Solution {
public int missingNumber(int[] nums) {
for(int i =0; i<nums.length;i++){
if(nums[i] != i){
return i;
}
}
return nums.length;
}
}


### 官方解法

class Solution {
public int missingNumber(int[] nums) {
int i = 0, j = nums.length - 1;
while(i <= j) {
int m = (i + j) / 2;
if(nums[m] == m) i = m + 1;
else j = m - 1;
}
return i;
}
}



# 5.6 剑指 Offer

## 11. 旋转数组的最小数字

### 个人解法

class Solution {
public int minArray(int[] numbers) {
// if(numbers==null){
//     return -1;
// }
int minV = numbers[0];
for(int i =0; i<numbers.length;i++){
if(numbers[i] < minV){
minV = numbers[i];
}
}
return minV;
}
}


### 官方解法

class Solution {
public int minArray(int[] numbers) {
int low = 0;
int high = numbers.length - 1;
while (low < high) {
int pivot = low + (high - low) / 2;
if (numbers[pivot] < numbers[high]) {
high = pivot;
} else if (numbers[pivot] > numbers[high]) {
low = pivot + 1;
} else {
high -= 1;
}
}
return numbers[low];
}
}



## 50. 第一个只出现一次的字符

### 个人解法

class Solution {
public char firstUniqChar(String s) {
Map<Character, Integer> map = new LinkedHashMap<>();

for (int i = 0; i < s.length(); i++) {
char one = s.charAt(i);
if(map.get(one)==null){
map.put(one,1);
}else {
map.put(one, map.get(one) + 1);
}
}
Set<Character> set = map.keySet();
for(Character a:set){
if(map.get(a)==1){
return (a);
}
}
return ' ';
}
}


### 官方解法

class Solution {
public char firstUniqChar(String s) {
Map<Character, Integer> frequency = new HashMap<Character, Integer>();
for (int i = 0; i < s.length(); ++i) {
char ch = s.charAt(i);
frequency.put(ch, frequency.getOrDefault(ch, 0) + 1);
}
for (int i = 0; i < s.length(); ++i) {
if (frequency.get(s.charAt(i)) == 1) {
return s.charAt(i);
}
}
return ' ';
}
}



### 推荐解法

class Solution {
// 计数数组
public char firstUniqChar(String s) {
int[] count = new int[26];
char[] chars = s.toCharArray();
for (char c : chars) count[c - 'a']++;
for (char c : chars) {
if (count[c - 'a'] == 1) return c;
}
return ' ';
}
}


## 04. 二维数组中的查找

### 个人解法

class Solution {
public boolean findNumberIn2DArray(int[][] matrix, int target) {
if(matrix.length == 1){
if(matrix[0].length < 1){
return (false);
}
}
for (int i = 0; i < matrix.length; i++) {
if(matrix[i][0]>target){
return (false);
}
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j]>target){
break;
}
if(matrix[i][j] == target){
return (true);
}
}
}
return false;
}
}


### 官方解法

#### 暴力

class Solution {
public boolean findNumberIn2DArray(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return false;
}
int rows = matrix.length, columns = matrix[0].length;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (matrix[i][j] == target) {
return true;
}
}
}
return false;
}
}


#### 线性查找

class Solution {
public boolean findNumberIn2DArray(int[][] matrix, int target) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return false;
}
int rows = matrix.length, columns = matrix[0].length;
int row = 0, column = columns - 1;
while (row < rows && column >= 0) {
int num = matrix[row][column];
if (num == target) {
return true;
} else if (num > target) {
column--;
} else {
row++;
}
}
return false;
}
}



# 5.7 是凌晨一点的补习

## 32 - II. 从上到下打印二叉树 II

### 个人解法

/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> list = new ArrayList<>();
if(root!=null){
}

while(!queue.isEmpty()){
for (int i = queue.size(); i > 0; i--) {
root = queue.poll();
}
}
return list;
}
}


# 5.8 是改论文得第365天

## 32 - I. 从上到下打印二叉树

### 个人解法 BFS（suc）

/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int[] levelOrder(TreeNode root) {
if(root!=null){
}
while(!queue.isEmpty()){
for (int i = queue.size(); i > 0; i--) {
root = queue.poll();
}
}
int[] r = new int[arr.size()];
for (int i = 0; i < arr.size(); i++) {
r[i] = arr.get(i);
}
return r;
}
}


### 官方解法

class Solution {
public int[] levelOrder(TreeNode root) {
if(root == null) return new int[0];
ArrayList<Integer> ans = new ArrayList<>();
while(!queue.isEmpty()) {
TreeNode node = queue.poll();
}
int[] res = new int[ans.size()];
for(int i = 0; i < ans.size(); i++)
res[i] = ans.get(i);
return res;
}
}



### queue

• remove() ：移除队首元素，若移除成功，则返回true；如果移除失败（队列为空），则会抛出异常；
• offer(E e) ：将元素e插入到队列末尾，如果插入成功，则返回true；如果插入失败（即队列已满），则返回false；
• poll() ：移除并获取队首元素，若成功，则返回队首元素；否则返回null；
peek() ：获取队首元素，若成功，则返回队首元素；否则返回nul

## 32 - III. 从上到下打印二叉树 III（回顾）

### 官方解法

class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
while(!deque.isEmpty()) {
// 打印奇数层
List<Integer> tmp = new ArrayList<>();
for(int i = deque.size(); i > 0; i--) {
// 从左向右打印
TreeNode node = deque.removeFirst();
// 先左后右加入下层节点
}
if(deque.isEmpty()) break; // 若为空则提前跳出
// 打印偶数层
tmp = new ArrayList<>();
for(int i = deque.size(); i > 0; i--) {
// 从右向左打印
TreeNode node = deque.removeLast();
// 先右后左加入下层节点
}
}
return res;
}
}



## 第一题

• 0
点赞
• 0
收藏
• 打赏
• 0
评论
02-07 2092
01-15 40
06-12 726
02-26 5536
07-29 2万+
11-24 631
05-31 785
12-08 579
12-09 614

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

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

¥2 ¥4 ¥6 ¥10 ¥20

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