add q73/q102/q118/q224

pull/6/head
yuanguangxin 5 years ago
parent e846ffbde9
commit 80aa37ed93

@ -1,9 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="af7ffdf2-4ddc-4ed6-8222-60ed5acbc2ed" name="Default Changelist" comment="">
<list default="true" id="af7ffdf2-4ddc-4ed6-8222-60ed5acbc2ed" name="Default Changelist" comment="update Rocket.md">
<change afterPath="$PROJECT_DIR$/src/动态规划/q118_杨辉三角/Solution.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/数组操作/q73_矩阵置零/Solution.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/栈操作/q224_基本计算器/f1/Solution.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/栈操作/q224_基本计算器/f2/Solution.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/树的遍历/q102_二叉树的层次遍历/Solution.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/树的遍历/q102_二叉树的层次遍历/TreeNode.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Rocket.md" beforeDir="false" afterPath="$PROJECT_DIR$/Rocket.md" afterDir="false" />
<change beforePath="$PROJECT_DIR$/README.md" beforeDir="false" afterPath="$PROJECT_DIR$/README.md" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -66,11 +72,11 @@
</component>
<component name="RecentsManager">
<key name="CopyClassDialog.RECENTS_KEY">
<recent name="树的遍历.q102_二叉树的层次遍历" />
<recent name="q110.q2" />
<recent name="q110" />
<recent name="q450" />
<recent name="q701" />
<recent name="q98.f3" />
</key>
<key name="CopyFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/src/hash相关" />
@ -246,7 +252,10 @@
<workItem from="1581061282899" duration="70334000" />
<workItem from="1581749148787" duration="1225000" />
<workItem from="1581847341168" duration="16207000" />
<workItem from="1582007077483" duration="836000" />
<workItem from="1582007077483" duration="2054000" />
<workItem from="1582286924766" duration="554000" />
<workItem from="1582552791282" duration="3963000" />
<workItem from="1582634245000" duration="4081000" />
</task>
<task id="LOCAL-00001" summary="add q5/q7/q172">
<created>1580045439607</created>
@ -479,7 +488,14 @@
<option name="project" value="LOCAL" />
<updated>1582020729706</updated>
</task>
<option name="localTasksCounter" value="34" />
<task id="LOCAL-00034" summary="update Rocket.md">
<created>1582020913236</created>
<option name="number" value="00034" />
<option name="presentableId" value="LOCAL-00034" />
<option name="project" value="LOCAL" />
<updated>1582020913236</updated>
</task>
<option name="localTasksCounter" value="35" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@ -570,10 +586,10 @@
<screen x="0" y="0" width="1440" height="900" />
</state>
<state x="533" y="166" key="#com.intellij.ide.util.MemberChooser/0.0.1440.900@0.0.1440.900" timestamp="1578548165321" />
<state x="303" y="50" key="CommitChangelistDialog2" timestamp="1582020729240">
<state x="303" y="50" key="CommitChangelistDialog2" timestamp="1582641231644">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state x="303" y="50" key="CommitChangelistDialog2/0.0.1440.900@0.0.1440.900" timestamp="1582020729240" />
<state x="303" y="50" key="CommitChangelistDialog2/0.0.1440.900@0.0.1440.900" timestamp="1582641231644" />
<state x="144" y="78" width="1152" height="720" key="DiffContextDialog" timestamp="1581927787180">
<screen x="0" y="23" width="1440" height="797" />
</state>
@ -623,18 +639,18 @@
<screen x="0" y="0" width="1440" height="900" />
</state>
<state x="221" y="63" key="SettingsEditor/0.0.1440.900@0.0.1440.900" timestamp="1581061096545" />
<state x="320" y="190" key="Vcs.Push.Dialog.v2" timestamp="1582020731697">
<state x="320" y="190" key="Vcs.Push.Dialog.v2" timestamp="1582020914413">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state x="320" y="190" key="Vcs.Push.Dialog.v2/0.0.1440.900@0.0.1440.900" timestamp="1582020731697" />
<state x="320" y="190" key="Vcs.Push.Dialog.v2/0.0.1440.900@0.0.1440.900" timestamp="1582020914413" />
<state x="100" y="100" width="1240" height="700" key="com.intellij.history.integration.ui.views.DirectoryHistoryDialog" timestamp="1581744794182">
<screen x="0" y="23" width="1440" height="797" />
</state>
<state x="100" y="100" width="1240" height="700" key="com.intellij.history.integration.ui.views.DirectoryHistoryDialog/0.23.1440.797@0.23.1440.797" timestamp="1581744794182" />
<state x="378" y="207" width="683" height="486" key="find.popup" timestamp="1581957308672">
<state x="378" y="207" width="683" height="486" key="find.popup" timestamp="1582564254151">
<screen x="0" y="0" width="1440" height="900" />
</state>
<state x="378" y="207" width="683" height="486" key="find.popup/0.0.1440.900@0.0.1440.900" timestamp="1581957308672" />
<state x="378" y="207" width="683" height="486" key="find.popup/0.0.1440.900@0.0.1440.900" timestamp="1582564254151" />
<state x="385" y="210" key="run.anything.popup" timestamp="1581652493432">
<screen x="0" y="0" width="1440" height="900" />
</state>

@ -38,10 +38,12 @@
### 数组操作
* [q54_螺旋矩阵](/src/数组操作/q54_螺旋矩阵)
* [q73_矩阵置零](/src/数组操作/q73_矩阵置零)
### 栈操作
* [q20_有效的括号](/src/栈操作/q20_有效的括号)
* [q224_基本计算器](/src/栈操作/q224_基本计算器)
### 递归
@ -58,6 +60,7 @@
* [q5_最长回文子串](/src/动态规划/q5_最长回文子串)
* [q53_最大子序和](/src/动态规划/q53_最大子序和)
* [q118_杨辉三角](/src/动态规划/q118_杨辉三角)
* [q746_使用最小花费爬楼梯](/src/动态规划/q746_使用最小花费爬楼梯)
* [q1277_统计全为1的正方形子矩阵](/src/动态规划/q1277_统计全为1的正方形子矩阵)
@ -70,6 +73,7 @@
### 树的遍历
* [q94_二叉树的中序遍历](/src/树的遍历/q94_二叉树的中序遍历)
* [q102_二叉树的层次遍历](/src/树的遍历/q102_二叉树的层次遍历)
* [q110_平衡二叉树](/src/树的遍历/q110_平衡二叉树)
* [q144_二叉树的前序遍历](/src/树的遍历/q144_二叉树的前序遍历)
* [q145_二叉树的后序遍历](/src/树的遍历/q145_二叉树的后序遍历)

@ -1,4 +1,4 @@
package .;
package .;
import java.util.Stack;

@ -1,4 +1,4 @@
package .;
package .;
public class TreeNode {
int val;

@ -0,0 +1,35 @@
package .q118_;
import java.util.ArrayList;
import java.util.List;
/**
* o(n^2)
*/
public class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> triangle = new ArrayList<List<Integer>>();
if (numRows == 0) {
return triangle;
}
triangle.add(new ArrayList<>());
triangle.get(0).add(1);
for (int rowNum = 1; rowNum < numRows; rowNum++) {
List<Integer> row = new ArrayList<>();
List<Integer> prevRow = triangle.get(rowNum-1);
row.add(1);
for (int j = 1; j < rowNum; j++) {
row.add(prevRow.get(j-1) + prevRow.get(j));
}
row.add(1);
triangle.add(row);
}
return triangle;
}
}

@ -0,0 +1,54 @@
package .q73_;
/**
* o(1) o(m*n)
*/
public class Solution {
public void setZeroes(int[][] matrix) {
//第一行是否需要置零
boolean row = false;
//第一列是否需要置零
boolean column = false;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] == 0) {
if (i == 0) {
row = true;
}
if (j == 0) {
column = true;
}
//第i行第一个元素置零表示这一行需要全部置零
matrix[i][0] = 0;
//第j列第一个元素置零表示这一列需要全部置零
matrix[0][j] = 0;
}
}
}
for (int i = 1; i < matrix.length; i++) {
if (matrix[i][0] == 0) {
for (int j = 1; j < matrix[i].length; j++) {
matrix[i][j] = 0;
}
}
}
for (int j = 1; j < matrix[0].length; j++) {
if (matrix[0][j] == 0) {
for (int i = 1; i < matrix.length; i++) {
matrix[i][j] = 0;
}
}
}
if (row) {
for (int j = 1; j < matrix[0].length; j++) {
matrix[0][j] = 0;
}
}
if (column) {
for (int i = 1; i < matrix.length; i++) {
matrix[i][0] = 0;
}
}
}
}

@ -0,0 +1,96 @@
package .q224_.f1;
import java.util.Stack;
/**
* +o(n)
*/
public class Solution {
public int calculate(String s) {
char[] array = s.toCharArray();
int n = array.length;
Stack<Integer> num = new Stack<>();
Stack<Character> op = new Stack<>();
int temp = -1;
for (int i = 0; i < n; i++) {
if (array[i] == ' ') {
continue;
}
// 数字进行累加
if (isNumber(array[i])) {
if (temp == -1) {
temp = array[i] - '0';
} else {
temp = temp * 10 + array[i] - '0';
}
} else {
//将数字入栈
if (temp != -1) {
num.push(temp);
temp = -1;
}
//遇到操作符
if (isOperation(array[i] + "")) {
while (!op.isEmpty()) {
if (op.peek() == '(') {
break;
}
//不停的出栈,进行运算,并将结果再次压入栈中
int num1 = num.pop();
int num2 = num.pop();
if (op.pop() == '+') {
num.push(num1 + num2);
} else {
num.push(num2 - num1);
}
}
//当前运算符入栈
op.push(array[i]);
} else {
//遇到左括号,直接入栈
if (array[i] == '(') {
op.push(array[i]);
}
//遇到右括号,不停的进行运算,直到遇到左括号
if (array[i] == ')') {
while (op.peek() != '(') {
int num1 = num.pop();
int num2 = num.pop();
if (op.pop() == '+') {
num.push(num1 + num2);
} else {
num.push(num2 - num1);
}
}
op.pop();
}
}
}
}
if (temp != -1) {
num.push(temp);
}
//将栈中的其他元素继续运算
while (!op.isEmpty()) {
int num1 = num.pop();
int num2 = num.pop();
if (op.pop() == '+') {
num.push(num1 + num2);
} else {
num.push(num2 - num1);
}
}
return num.pop();
}
private boolean isNumber(char c) {
return c >= '0' && c <= '9';
}
private boolean isOperation(String t) {
return t.equals("+") || t.equals("-") || t.equals("*") || t.equals("/");
}
}

@ -0,0 +1,59 @@
package .q224_.f2;
import java.util.Stack;
/**
* o(n)
*/
public class Solution {
public int evaluateExpr(Stack<Object> stack) {
int res = 0;
if (!stack.empty()) {
res = (int) stack.pop();
}
while (!stack.empty() && !((char) stack.peek() == ')')) {
char sign = (char) stack.pop();
if (sign == '+') {
res += (int) stack.pop();
} else {
res -= (int) stack.pop();
}
}
return res;
}
public int calculate(String s) {
int operand = 0;
int n = 0;
Stack<Object> stack = new Stack<Object>();
for (int i = s.length() - 1; i >= 0; i--) {
char ch = s.charAt(i);
if (Character.isDigit(ch)) {
operand = (int) Math.pow(10, n) * (int) (ch - '0') + operand;
n += 1;
} else if (ch != ' ') {
if (n != 0) {
stack.push(operand);
n = 0;
operand = 0;
}
if (ch == '(') {
int res = evaluateExpr(stack);
stack.pop();
stack.push(res);
} else {
stack.push(ch);
}
}
}
if (n != 0) {
stack.push(operand);
}
return evaluateExpr(stack);
}
}

@ -0,0 +1,43 @@
package .q102_;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
/**
* o(n)
*/
public class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> levels = new ArrayList<>();
if (root == null) {
return levels;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
int level = 0;
while (!queue.isEmpty()) {
levels.add(new ArrayList<>());
int levelLength = queue.size();
for (int i = 0; i < levelLength; ++i) {
TreeNode node = queue.remove();
levels.get(level).add(node.val);
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
level++;
}
return levels;
}
}

@ -0,0 +1,11 @@
package .q102_;
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
Loading…
Cancel
Save