LintCode刷题(入门篇)
来源:互联网 发布:淘宝店在哪找货源 编辑:程序博客网 时间:2024/06/06 07:49
最近在玩LintCode上面的算法题。下面分享一下部分题目的答案。如果其他同学有更好的答案,可以和我交流讨论,本人菜鸟一个,各位大佬多指点。
同时说一下,这个上面的二叉树 和 链表 我不懂,所以这类题目没有做。现在才刚开始玩,做了大概二十来题,以后会持续更新。。。
我是依次一题题往下看的,从入门开始,这边记录也从这里开始吧。部分题目没看懂的,就过了。
二叉树的最大节点(这题参考别人的,二叉树不是很懂)
public TreeNode maxNode(TreeNode root) {// Write your code here
ArrayList<TreeNode> result = new ArrayList<TreeNode>();
result.add(root);
search(root , result);
return result.get(0);
}
public void search(TreeNode root , ArrayList<TreeNode> result){
if(root == null){
return ;
}
if(result.get(0).val < root.val){
result.set(0 , root);
}
if(root.left != null){
search(root.left , result);
}
if(root.right != null){
search(root.right , result);
}
}
斐波纳契数列 (这题相对比较简单,没啥好说的)
public int fibonacci(int n) {// write your code here
int a = 0;
int b = 1;
int c = 0;
if (n == 1) {
return 0;
} else if (n == 2) {
return 1;
} else {
for(int j = 3; j <= n; j++) {
c = a + b;
a = b;
b = c;
}
return c;
}
}
矩阵面积(这题更没啥好说的)
public class Rectangle {
// write your code here
private int mWidth;
private int mHeight;
public Rectangle(int mWidth, int mHeight) {
this.mWidth = mWidth;
this.mHeight = mHeight;
}
public int getArea() {
return mWidth * mHeight;
}
}
// write your code here
private int mWidth;
private int mHeight;
public Rectangle(int mWidth, int mHeight) {
this.mWidth = mWidth;
this.mHeight = mHeight;
}
public int getArea() {
return mWidth * mHeight;
}
}
整数排序(直接调用API就好了,对于这种千万不要浪费时间)
Arrays.sort(A);
阅读全文
0 0
- LintCode刷题(入门篇)
- Lintcode(入门)463
- LintCode【入门】整数排序
- LintCode【入门】斐波纳契数列
- lintcode-入门-整数排序
- lintcode-入门-斐波纳契数列
- LintCode刷题(容易篇 一)
- LintCode刷题(容易篇 二)
- LintCode刷题(容易篇 三)
- LintCode刷题(容易篇 四)
- lintCode入门篇之整数排序--直接插入
- Lintcode刷题
- LintCode-[入门] 454. 矩阵面积
- LintCode【入门】删除链表中的元素
- lintcode-入门-删除链表中的元素
- lintCode 入门题 python版解答
- 【LintCode】454.矩阵面积(入门)解题报告
- LintCode刷题记录入门题汇总
- 51nod 1585 买邮票送邮票 容斥原理+线性筛
- iPhone X 的适配
- EKF—面向初学者的互动教程(1)
- SGA 的自动管理
- 1004. 成绩排名 (20)
- LintCode刷题(入门篇)
- 06-图3 六度空间(30 point(s))
- (一)logback模块和主要接口
- Java
- Android布局优化
- VIN码识别/车牌识别:是入口,是门面
- 三个工具帮你记住Linux命令
- 双目立体视觉匹配算法-----SAD匹配算法、BM算法、SGBM算法、GC算法
- 1006. 换个格式输出整数 (15)