easy-算法
意涵团_晴天
愿有岁月可回首
展开
-
二叉树的最大最小深度
列表内容 二叉树的最大深度指的是从根节点到最远子节点的长度。function maxDepth(root) { if(root == null){ return 0; }else if(root.left == null && root.right == null){ return 1 }else{ var leftMaxLen原创 2017-03-08 15:37:02 · 469 阅读 · 0 评论 -
Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), desi原创 2017-04-18 16:00:14 · 185 阅读 · 0 评论 -
Word Pattern
Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = “abba”, str = “dog cat cat dog” should return true. pattern = “abba”, str = “dog cat cat fish” should retu原创 2017-04-06 10:35:59 · 251 阅读 · 0 评论 -
Perfect Number
We define the Perfect Number is a positive integer that is equal to the sum of all its positive divisors except itself.Now, given an integer n, write a function that returns true when it is a perfect n原创 2017-04-06 10:56:09 · 279 阅读 · 0 评论 -
Count Primes
Count the number of prime numbers less than a non-negative number, n.var countPrimes = function(n) { var arr= []; for(var m = 0; m < n; m++) { arr[m] = true } for(var i = 2; i <原创 2017-04-06 11:49:53 · 243 阅读 · 0 评论 -
Excel Sheet Column Title
Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example:1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB var convertToTitle = function(n) { var原创 2017-04-06 14:29:40 · 455 阅读 · 0 评论 -
Plus One
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.You may assume the integer do not contain any leading zero, except the number 0 itself.The digits are st原创 2017-04-06 15:08:01 · 265 阅读 · 0 评论 -
Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, r原创 2017-04-06 16:34:32 · 331 阅读 · 0 评论 -
Isomorphic Strings
Given two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be replaced with another chara原创 2017-04-19 19:18:01 · 189 阅读 · 0 评论 -
Next Greater Element I
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1’s elements in the corresponding places of nums2.The原创 2017-04-06 19:10:18 · 244 阅读 · 0 评论 -
118. Pascal's Triangle
Given numRows, generate the first numRows of Pascal’s triangle.For example, given numRows = 5, Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ]var generate = function(n) {原创 2017-04-20 11:42:24 · 210 阅读 · 0 评论 -
Reverse String II
Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them.原创 2017-04-18 15:43:48 · 226 阅读 · 0 评论 -
Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; ot原创 2017-04-13 15:48:32 · 188 阅读 · 0 评论 -
js判断是否二叉树是否对称
对于二叉树遍历,有前序遍历、中序遍历、后序遍历3种算法。下面分别使用3种方法看是否为对称二叉树 前序遍历:先遍历根节点,再遍历左右子树 中序遍历:先遍历左(右)子树,然后访问根节点,最后遍历右(左)子树 后序遍历:先遍历左右子树,再遍历根节点 采用前序遍历的方法判断是否为对称二叉树:var isSymmetric = function(root) { if(root == null){原创 2017-03-08 15:06:39 · 1479 阅读 · 0 评论 -
二维数组中的查找
在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。//因为矩阵是有序的,所以我们选择从左下角开始搜寻,当要查找的数字比左下角的数字大时,右移继续查找,小时上移继续查找。 function find(target,arr){ var len = arr.length - 1;原创 2017-03-21 15:50:44 · 231 阅读 · 0 评论 -
Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array。var findMaxConsecutiveOnes = function(arr) { var num = 0; var res = 0; for(var i = 0; i < arr.length;i++){原创 2017-03-21 17:07:44 · 210 阅读 · 0 评论 -
Construct the Rectangle
For a web developer, it is very important to know how to design a web page’s size. So, given a specific rectangular web page’s area, your job by now is to design a rectangular web page, whose length L原创 2017-04-07 10:30:57 · 251 阅读 · 0 评论 -
K-diff Pairs in an Array
Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in原创 2017-03-22 16:20:33 · 282 阅读 · 0 评论 -
求两个字符串最长公共子串的长度
function maxCharLen(s1,s2) { var res = 0; for(var i = 0; i < s1.length; i++) { if(s2.indexOf(s1.charAt(i)) != -1) { var len = 0, m = i, n = s2.indexOf(s1.charAt(i)); while原创 2017-03-24 14:41:12 · 497 阅读 · 0 评论 -
Assign Cookies
Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a cook原创 2017-04-12 11:57:32 · 279 阅读 · 0 评论 -
Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals: “Gold Medal”, “Silver Medal” and “Bronze Medal”.var findRelativeRanks原创 2017-04-12 11:58:27 · 244 阅读 · 0 评论 -
Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, “A man, a plan, a canal: Panama” is a palindrome. “race a car” is not a pal原创 2017-03-27 10:22:52 · 215 阅读 · 0 评论 -
js数组求交集
//交集 function intersect(arr1, arr2) { var res = []; for(var i = 0; i < arr1.length; i++){ for(var j = 0; j < arr2.length; j++){ if(arr1[i] == arr2[j]){ res.push(arr1[i])原创 2017-03-22 15:34:40 · 477 阅读 · 0 评论 -
反转数字
eg:输入123 输出321function reverseNum(n) { var res = 0; while(n > 0) { var m = n % 10; res = res * 10 + m; n = parseInt(n / 10) } return res }原创 2017-04-24 18:50:19 · 248 阅读 · 0 评论