
算法
vulgatecn1
在宁波,有兴趣的人可以面聊
展开
-
leetcode 90
var subsetsWithDup = function(nums){let res = [[]]; let begin = 0;我的代码效率很低只有30%/** * @param {number[]} nums * @return {number[][]} */var subsetsWithDup = functi原创 2017-10-17 16:59:48 · 272 阅读 · 0 评论 -
leetcode 63
https://leetcode.com/problems/unique-paths-ii/description/Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle原创 2017-10-14 11:14:16 · 200 阅读 · 0 评论 -
leetcode 64
https://leetcode.com/problems/minimum-path-sum/description/Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbe原创 2017-10-14 17:09:16 · 168 阅读 · 0 评论 -
leetcode 125
i=0;j=s.length-1;s=s.toLocaleLowerCase();while(ij){ if(!(s[i].charCodeAt()'z'.charCodeAt() && s[i].charCodeAt()>='a'.charCodeAt() || (s[i].charCodeAt()'9'.charCodeAt() && s[i].charCodeAt()>='0'原创 2017-11-01 16:03:37 · 140 阅读 · 0 评论 -
leetcode 200
深度优先,仔细仔细仔细var numislands1 = function(grid){ //深度优先 arrl=[]; count=0; n=grid.length; if(n==0){ return 0; } m=grid[0].length; for(var i=0;in;i++) { arr原创 2017-11-01 17:22:10 · 454 阅读 · 0 评论 -
leetcode 77
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:这个题目非常简单,但我做了2-3个小时,我的思路和排名第一的思路是一致的,这里需要注意js需要深度co原创 2017-10-16 13:33:30 · 151 阅读 · 0 评论 -
leetcoder 78
Given a set of distinct integers, nums, return all possible subsets.Note: The solution set must not contain duplicate subsets.For example,If nums = [1,2,3], a solution is:[ [3], [1],原创 2017-10-16 15:58:39 · 148 阅读 · 0 评论 -
leetcode 118
我承认这个代码写的很水/** * @param {number} numRows * @return {number[][]} */var generate = function(numRows) { result1=[1]; result2=result1; result=[]; result.push(result1);原创 2017-10-25 11:22:53 · 148 阅读 · 0 评论 -
leetcode 119
一如既往的水/** * @param {number} rowIndex * @return {number[]} */var getRow = function(rowIndex) { numRows=rowIndex+1; result1=[1]; result2=result1; if(numRows==0)原创 2017-10-25 11:26:44 · 191 阅读 · 0 评论 -
leetcode 120
代码还是写的很水/** * @param {number[][]} triangle * @return {number} */var minimumTotal = function(triangle) { result1=triangle[0].slice(0); result2=result1.slice(0); min=triangl原创 2017-10-25 12:46:28 · 163 阅读 · 0 评论 -
leetcode 216
其实还有更加有效率的方法/** * @param {number} k * @param {number} n * @return {number[][]} */var combinationSum3 = function(k, n) { arrl=[]; arr=[]; var search=function(k1){原创 2017-11-02 21:14:33 · 202 阅读 · 0 评论 -
leetcode 49
超时,哇哈哈。/** * @param {string[]} strs * @return {string[][]} */var groupAnagrams = function(strs) { arrl = []; if(strs.length==0){ return []; } arrl[0]=[]; arrl[0].原创 2017-11-02 21:58:59 · 135 阅读 · 0 评论 -
leetcode 93
Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given "25525511135",return ["255.255.11.135", "255.255.111.35"]. (Order原创 2017-10-17 15:53:56 · 208 阅读 · 0 评论 -
leetcode 39
多写代码身体好/** * @param {number[]} candidates * @param {number} target * @return {number[][]} */var combinationSum = function(candidates, target) { var arrl=[]; arr=[];原创 2017-10-26 15:49:36 · 118 阅读 · 0 评论 -
leetcode 40
能解决问题,但效率不咋地/** * @param {number[]} candidates * @param {number} target * @return {number[][]} */var combinationSum = function(candidates, target) { var arrl=[]; arr=[]; candidates原创 2017-10-26 17:33:05 · 259 阅读 · 0 评论 -
leetcode 165
我的代码效率居然排100%但看别人的代码我这个代码不够精简。var search1 = function ( version1 ,version2) { pos1 = version1.indexOf('.'); pos2 = version2.indexOf('.'); if(pos1==-1 && pos2==-1){ if(pars原创 2017-11-03 16:51:25 · 234 阅读 · 0 评论 -
leetcode 349
我不怎么满意,实际效率也很低nums1.sort(function (a,b) {return a-b;});nums2.sort(function (a,b) {return a-b;});function unique(nums){ res=[]; hash=[]; for(i=0;ilength;i++){ if(hash[nums[原创 2017-11-03 20:40:52 · 114 阅读 · 0 评论 -
leetcode 662
做这个题目的人比价少,哇哈哈哈,还是蛮顺利的,效率也不错/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } *//** * @param {Tre原创 2017-11-03 22:12:45 · 322 阅读 · 0 评论 -
leetcode 52
这个题目我想复杂了,我以为要考虑旋转棋盘后得到的解是重复的,我search了一下,发现不是这个意思,然后稍微改一下代码即可arr_l=[];arr=[];search(0);var count=0;function search(i){ if(i==n){ arr1=[]; for(var i=0;ii++){原创 2017-10-23 13:20:57 · 273 阅读 · 0 评论 -
leetcode 51
https://leetcode.com/submissions/detail/124893020/83%的成绩,其实程序不难,但很久没写N Queen问题了var solveNQueens = function(n) { arr_l=[]; arr=[]; search(0); function search(i){ if(原创 2017-10-23 12:57:20 · 257 阅读 · 0 评论 -
leetcode 69
这题简单的,用系统函数,这个是我的代码我排名后面的原因是提交次数太多,sad/** * @param {number} x * @return {number} */var mySqrt = function(x) { return Math.floor(Math.sqrt(x));};原创 2017-10-18 15:54:05 · 242 阅读 · 0 评论 -
leetcode 73
和排名第一的代码差不多,这个题目不难/** * @param {number[][]} matrix * @return {void} Do not return anything, modify matrix in-place instead. */var setZeroes = function(matrix) { n=matrix.length;原创 2017-10-18 16:28:29 · 184 阅读 · 0 评论 -
leetcode 71
https://leetcode.com/problems/simplify-path/description/第一次拿第一,开心 /** * @param {string} path * @return {string} */ var simplifyPath = function(path) { arr=pa原创 2017-10-18 17:46:40 · 173 阅读 · 0 评论 -
js题目
罗马数字的题目。首先你需要知道罗马数字是怎么来的。具体我这里不说了我后来搜了一下别的答案,有更加好的,你们自己去搜吧,但是我不太确定他这个思路是对的还有类似于我这样查表的,但我这个表好的点在于,扩展性比较好,比如万,亿等等数字都可以在这里扩展,他们的答案是写死的function convert(num) {var arr=[['I','V','X'],['X','L',原创 2017-10-11 10:14:46 · 148 阅读 · 0 评论 -
leetcode题目 1
Given nums = [2, 7, 11, 15], target = 9,Because nums[0] + nums[1] = 2 + 7 = 9,return [0, 1].比较笨的方法,排名比较后面,哈哈哈/** * @param {number[]} nums * @param {number} target * @retur原创 2017-10-11 13:55:23 · 111 阅读 · 0 评论 -
leetcode 202
呵呵,这个我第一/** * @param {number} n * @return {boolean} */var isHappy = function(n) { function search(n){ sum=0; while(n>0){ sum+=(n % 10)*(n%10);原创 2017-10-28 15:24:52 · 127 阅读 · 0 评论 -
leetcode 204
第一种方法超时,需要的是ln(n)的空间第二种方法是筛选法,需要n的空间var countPrimes1 = function(n) { //最快的方式是一边找素数一遍数数 arr=[2,3]; p=4; count=2; while(p<n){ flag=0; for(var i=0;iarr.lengt原创 2017-10-28 16:13:14 · 152 阅读 · 0 评论 -
leetcode 205
/** * @param {string} s * @param {string} t * @return {boolean} */var isIsomorphic = function(s, t) { len_s=s.length; dict1=new Array(); dict2=new Array(); for(var i=0;ilen_s;i++原创 2017-10-29 12:48:46 · 186 阅读 · 0 评论 -
leetcode 231
很水 /** * @param {number} n * @return {boolean} */var isPowerOfTwo = function(n) { if(n return false; } if(n==1){ return true; } while(n>1){原创 2017-10-29 13:19:13 · 218 阅读 · 0 评论 -
leetcode 回文数
判断回文数的问题https://leetcode.com/problems/palindrome-number/description/Determine whether an integer is a palindrome. Do this without extra space.这里注意负数不要判断javascript证书和浮点数是一个数据类型,所以需要用mat原创 2017-10-12 12:07:05 · 273 阅读 · 0 评论 -
leetcode remove-element
https://leetcode.com/problems/remove-element/description/Given an array and a value, remove all instances of that value in place and return the new length.Do not allocate extra space f原创 2017-10-12 15:07:03 · 145 阅读 · 0 评论 -
leetcode generate-parentheses
https://leetcode.com/problems/generate-parentheses/description/这道题还好,一开始我还怕了一下子,想想就好了,也学会了用webstrom 调试nodejs的方式来调js,否则每次用chrome太麻烦了第一名的代码我也看过了,不用我那么麻烦的判断和额外的数组,这道题还行/** * @param {number} n原创 2017-10-12 17:52:29 · 187 阅读 · 0 评论 -
leetcode 28
Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.Seen this question in a real interview before? Y原创 2017-10-13 10:33:03 · 317 阅读 · 0 评论 -
leetcode 48
https://leetcode.com/problems/rotate-image/description/48. Rotate Image这个题目简单的就是需要动动脑子知道矩阵怎么旋转的,这个比较费劲,然后我排名比较后面,有点郁闷第一名是一开始做倒序排列,然后每次交换两个数字就好了/** * @param {number[][]}原创 2017-10-13 11:14:45 · 250 阅读 · 0 评论 -
leetcode Add to List 5. Longest Palindromic Substring
Add to List 5. Longest Palindromic Substringhttps://leetcode.com/problems/longest-palindromic-substring/description/说我超时,但我觉得我的代码已比较优化了,不知道系统怎么了/** * @param {string} s * @return原创 2017-10-13 14:04:37 · 146 阅读 · 0 评论 -
leetcode 46
/** * @param {number[]} nums * @return {number[][]} */var permute = function(nums) { arr=[]; arrl=[]; function search(nums){ if(nums.length==0){ arrl.push(arr.slice原创 2017-10-30 20:33:02 · 193 阅读 · 0 评论 -
leetcode 47
啧啧啧,我排名98%nums.sort(function (a,b) { return a>b});arrl=[];arr=[];function search(nums){ if(nums.length==0){ arrl.push(arr.slice(0)); return ; } for(var i=0;ilengt原创 2017-10-30 20:58:54 · 124 阅读 · 0 评论 -
leetcode 494
这个没有超时,但效率不高/** * @param {number[]} nums * @param {number} S * @return {number} */var findTargetSumWays = function(nums, S) { len1=nums.length; d=[]; d[0]={}; if(nums[0]==0){ d[0][nums[0].toString()]=2; }else {原创 2017-11-04 22:04:44 · 245 阅读 · 0 评论