随笔分类 - 算法设计
摘要:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single ...
阅读全文
摘要:Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations:getandset.get(key)- Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.set(key, value)- Set or insert the value if the key is not
阅读全文
摘要:Givennpoints on a 2D plane, find the maximum number of points that lie on the same straight line.Solution:/** * Definition for a point. * struct Point { * int x; * int y; * Point() : x(0), y(0) {} * Point(int a, int b) : x(a), y(b) {} * }; */class Solution {public: int maxPoints(v...
阅读全文
摘要:Evaluate the value of an arithmetic expression inReverse Polish Notation.Valid operators are+,-,*,/. Each operand may be an integer or another expression.Some examples: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13&qu
阅读全文
摘要:Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.For example:Given the below binary tree, 1 / \ 2 3Return6./** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * Tree...
阅读全文
摘要:Problem:Minimum Depth of Binary TreeGiven a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.Solution one based on recursive algrithm. It looks clear, but not optimal .//Solution one /** * Definition
阅读全文
摘要:链表反转,返回反转后的链表头节点。记得在遍历过程中保存前面节点的指针可以用递归和迭代两种方法来实现,在我学习的过程中,递归实现相对比较难以理解一些递归法:public static ListNode reverseList(ListNode head){ if(head == null){ return head;} //base case ListNode nHead = reverseList(head.next); head.next.next = head; head.next = null; return nHead;}递归算法就是显得比较优雅 尾部...
阅读全文
摘要:前几天看到Facebook 主办的比赛,题目还是比较有意思的,废话少说,直接上题题目如下:输入为 "#" 和 "." 构成的M*M的矩形,判断在图中由所有”#“构成的图形是否为实心正方形。为矩形则输出”YES“,否则输出”NO“比如:..##..##........ 输出为 YES但是这个..##..###... 输出为 NO 因为题目要求是所有"# "构成的图形....这个也不行..###..###..#.# 输出为 NO 因为题目要求是所有"#" 构成的图形..........如果觉得题目有意思,建议把你的ch
阅读全文
摘要:本文为个人的学习笔记,如果发现文中有那些不对的地方,希望大家多指点,在下先谢谢各位学友。出自 《Cracking the coding interview》首先想到的是简单的递归方法:private int height(TreeNode node){ if(root == null) return 0; return 1 + Math.max(height(root.left),height(root.right));}public boolean isBanlanced(TreeNode node){ if(node == null) return true; in...
阅读全文