LeetCode题解(六)0500-0599,Android开发指南

      • 509. Fibonacci Number
  • 561. Array Partition I

  • 589. N-ary Tree Preorder Traversal

  • 590. N-ary Tree Postorder Traversal

  • 595. Big Countries

509. Fibonacci Number

[Description]

The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,

F(0) = 0, F(1) = 1

F(N) = F(N - 1) + F(N - 2), for N > 1.

Given N, calculate F(N).

Example 1:

Input: 2

Output: 1

Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.

Example 2:

Input: 3

Output: 2

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

Example 3:

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++)

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
img

学习宝典

对我们开发者来说,一定要打好基础,随时准备战斗。不论寒冬是否到来,都要把自己的技术做精做深。虽然目前移动端的招聘量确实变少了,但中高端的职位还是很多的,这说明行业只是变得成熟规范起来了。竞争越激烈,产品质量与留存就变得更加重要,我们进入了技术赋能业务的时代。

不论遇到什么困难,都不应该成为我们放弃的理由!

很多人在刚接触这个行业的时候或者是在遇到瓶颈期的时候,总会遇到一些问题,比如学了一段时间感觉没有方向感,不知道该从那里入手去学习,对此我针对Android程序员,我这边给大家整理了一套学习宝典!包括不限于高级UI、性能优化、移动架构师、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter等全方面的Android进阶实践技术;希望能帮助到大家,也节省大家在网上搜索资料的时间来学习,也可以分享动态给身边好友一起学习!

Android学习PDF+架构视频+面试文档+源码笔记

【Android核心高级技术PDF文档,BAT大厂面试真题解析】

【算法合集】

【延伸Android必备知识点】

Flutter等全方面的Android进阶实践技术;希望能帮助到大家,也节省大家在网上搜索资料的时间来学习,也可以分享动态给身边好友一起学习!

Android学习PDF+架构视频+面试文档+源码笔记

【Android核心高级技术PDF文档,BAT大厂面试真题解析】

[外链图片转存中…(img-X49avQy2-1710844845759)]

【算法合集】

[外链图片转存中…(img-xGHS0OAX-1710844845760)]

【延伸Android必备知识点】

[外链图片转存中…(img-TAIiAB3O-1710844845760)]

【Android部分高级架构视频学习资源】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值