newcaoguo

我在转角吃炒粉。


  • Home

  • Tags

  • Archives

剪绳子

Posted on 2020-02-15

题目描述

给你一根长度为n的绳子,请把绳子剪成整数长的 m 段(m、n都是整数,n>1并且m>1),每段绳子的长度记为 k[0], k[1],…, k[m]。请问 k[0] x k[1] x … x k[m] 可能的最大乘积是多少?例如,当绳子的长度是 8 时,我们把它剪成长度分别为 2、3、3 的三段,此时得到的最大乘积是 18。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public int cutRope(int target) {
if(target < 2) {
return 0;
}
if(target == 2) {
return 1;
}
if(target == 3){
return 2;
}
int[] dp = new int[target + 1];
dp[0] = 0;
dp[1] = 1;
dp[2] = 2;
dp[3] = 3;
int max = 0;
int tmp = 0;
for (int i = 4; i <= target; i++){
max = 0;
for (int j = 1 ; j <= i / 2; j++){
tmp = dp[j] * dp[i - j];
if (tmp > max){
max = tmp;
}
}
dp[i] = max;
}
return dp[target];
}

重建二叉树

Posted on 2020-02-09

题目描述

输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列 {1,2,4,7,3,5,6,8} 和中序遍历序列 {4,7,2,1,5,3,8,6},则重建二叉树并返回。

先序排列顺序为:根-左-右,中序排列为:左-根-右。
例如输入前序遍历序列 {1,2,4,7,3,5,6,8} 和中序遍历序列 {4,7,2,1,5,3,8,6}。
那么如题根为1,则根据中序遍历序列则可以得到左子树 {4,7,2} 和右子树 {5,3,8,6}。
又根据前序遍历则可以得到左子树的根为 2,右子树的根为 3。
重复以上两步,直到左右子树皆为空时即可重建二叉树如图。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public TreeNode reConstructBinaryTree(int[] pre, int[] in) {
if (pre == null || in == null || pre.length != in.length) {
return null;
}
return re(pre, 0, pre.length - 1, in, 0, in.length - 1);
}

public TreeNode re(int[] pre,int startPre,int endPre,int[] in,int startIn,int endIn){
if (startPre > endPre || startIn > endIn) {//判定是否序列是否便利完。
return null;
}
TreeNode root = new TreeNode(pre[startPre]);//存入节点
for (int i = startIn; i <= endIn; i++){//从中序遍历开始,寻找和根节点相同的元素。
if (in[i] == pre[startPre]) {//找到了之后分为左右子树,递归进行查找。
root.left = re(pre, startPre + 1, startPre + i - startIn, in, startIn, i - 1);
root.right = re(pre, startPre + i - startIn + 1, endPre, in, i + 1, endIn);
}
}
return root;
}

用两个栈实现队列

Posted on 2020-02-09

题目描述

用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();

public void push(int node) {
stack1.push(node);
}

public int pop() {
while (!stack1.empty()) {
stack2.push(stack1.pop());
}
int value = stack2.pop();
while (!stack2.empty()) {
stack1.push(stack2.pop());
}
return value;
}

反转链表

Posted on 2020-02-08

题目描述

输入一个链表,反转链表后,输出新链表的表头。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public ListNode ReverseList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode p = head;
ListNode q = head.next;
ListNode r;
head.next = null;
while (q != null) {
r = q.next;
q.next = p;
p = q;
q = r;
}
head = p;
return head;
}

从尾到头打印链表

Posted on 2020-02-08

题目描述

输入一个链表,按链表从尾到头的顺序返回一个ArrayList。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {

Stack<Integer> mIntegerStack = new Stack<>();
while (listNode != null) {
mIntegerStack.push(listNode.val);
listNode = listNode.next;
}

ArrayList<Integer> mList = new ArrayList<>();

for (; !mIntegerStack.isEmpty(); ) {
mList.add(mIntegerStack.pop());
}
return mList;
}
1234
newcaoguo

newcaoguo

16 posts
1 categories
4 tags
GitHub
© 2020 newcaoguo
Powered by Hexo
|
Theme — NexT.Muse v5.1.4