其他分享
首页 > 其他分享> > LeetCode题解(六)0500-0599,从思维图到基础再到深入

LeetCode题解(六)0500-0599,从思维图到基础再到深入

作者:互联网

Input: 4

Output: 3

Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.

Note:

0 ≤ N ≤ 30.

[Answer]

Runtime: 0 ms, faster than 100.00% of Java online submissions for Fibonacci Number.

Memory Usage: 32.7 MB, less than 5.10% of Java online submissions for Fibonacci Number.

class Solution {

public int fib(int N) {

double pi = 1 + Math.sqrt(5);

pi = pi / 2;

return (int) Math.round(Math.pow(pi, N) / Math.sqrt(5));

}

}

Runtime: 1 ms, faster than 37.18% of Java online submissions for Fibonacci Number.

Memory Usage: 32.8 MB, less than 5.10% of Java online submissions for Fibonacci Number.

class Solution {

public int fib(int N) {

switch (N) {

case 0:

return 0;

case 1:

return 1;

case 10:

return 55;

case 20:

return 6765;

case 30:

return 832040;

default:

return fib(N - 1) + fib(N - 2);

}

}

}

Runtime: 9 ms, faster than 25.56% of Java online submissions for Fibonacci Number.

Memory Usage: 32.8 MB, less than 5.10% of Java online submissions for Fibonacci Number.

class Solution {

public int fib(int N) {

if (N == 0) return 0;

if (N == 1) return 1;

return fib(N - 1) + fib(N - 2);

}

}

561. Array Partition I

[Description]

Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), …, (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.

Example 1:

Input: [1,4,3,2]

Output: 4

Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).

Note:

n is a positive integer, which is in the range of [1, 10000].

All the integers in the array will be in the range of [-10000, 10000].

[Answer]

Runtime: 14 ms, faster than 92.92% of Java online submissions for Array Partition I.

Memory Usage: 39.2 MB, less than 99.90% of Java online submissions for Array Partition I.

class Solution {

public int arrayPairSum(int[] nums) {

Arrays.sort(nums);

int sum = 0;

for (int i = 0; i < nums.length - 1; i = i + 2) {

sum += nums[i];

}

return sum;

}

}

589. N-ary Tree Preorder Traversal

[Description]

Given an n-ary tree, return the preorder traversal of its nodes’ values.

For example, given a 3-ary tree:

1

/ |

3 2 4

/

5 6

Return its preorder traversal as: [1,3,5,6,2,4].

Note:

Recursive solution is trivial, could you do it iteratively?

[Answer]

Runtime: 2 ms, faster than 46.12% of Java online submissions for N-ary Tree Preorder Traversal.

Memory Usage: 45.2 MB, less than 85.04% of Java online submissions for N-ary Tree Preorder Traversal.

/*

// Definition for a Node.

class Node {

public int val;

public List children;

public Node() {}

public Node(int _val,List _children) {

val = _val;

children = _children;

}

};

*/

class Solution {

public List preorder(Node root) {

List result = new ArrayList<>();

if (root == null) return result;

result.add(root.val);

for (int i = 0; i < root.children.size(); i++)

result.addAll(preorder(root.children.get(i)));

return result;

}

}

Runtime: 6 ms, faster than 18.45% of Java online submissions for N-ary Tree Preorder Traversal.

Memory Usage: 43.5 MB, less than 98.26% of Java online submissions for N-ary Tree Preorder Traversal.

/*

// Definition for a Node.

class Node {

public int val;

public List children;

public Node() {}

public Node(int _val,List _children) {

val = _val;

children = _children;

}

};

*/

class Solution {

public List preorder(Node root) {

List result = new ArrayList<>();

if (root == null) return result;

result.add(root.val);

for (int i = 0; i < root.children.size(); i++) {

List resultT = preorder(root.children.get(i));

for (int j = 0; j < resultT.size(); j++)

result.add(resultT.get(j));

}

return result;

}

}

Input null

Output null

Expected []

/*

// Definition for a Node.

class Node {

public int val;

public List children;

public Node() {}

public Node(int _val,List _children) {

val = _val;

children = _children;

}

};

*/

class Solution {

public List preorder(Node root) {

总结

【Android 详细知识点思维脑图(技能树)】

我个人是做Android开发,已经有十来年了,目前在某创业公司任职CTO兼系统架构师。虽然 Android 没有前几年火热了,已经过去了会四大组件就能找到高薪职位的时代了。这只能说明 Android 中级以下的岗位饱和了,现在高级工程师还是比较缺少的,很多高级职位给的薪资真的特别高(钱多也不一定能找到合适的),所以努力让自己成为高级工程师才是最重要的。

这里附上上述的面试题相关的几十套字节跳动,京东,小米,腾讯、头条、阿里、美团等公司19年的面试题。把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节。

由于篇幅有限,这里以图片的形式给大家展示一小部分。

详细整理在GitHub可以见;

Android架构视频+BAT面试专题PDF+学习笔记

网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。

大家展示一小部分。

[外链图片转存中…(img-mgSGxUOV-1644042286985)]

详细整理在GitHub可以见;

Android架构视频+BAT面试专题PDF+学习笔记

网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。

最后,赠与大家一句话,共勉!

标签:Node,return,val,图到,题解,children,int,0500,public
来源: https://blog.csdn.net/m0_66264819/article/details/122790206