This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var chai = require('chai'); | |
var expect = chai.expect; | |
describe('Test Two Sum', function() { | |
it('Expect result will be [0, 1]', function() { | |
var nums = [2, 7, 11, 15]; | |
var target = 9 | |
var result = twoSum(nums, target); | |
expect(result).to.deep.equal([0, 1]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* * @param {number[]} nums | |
* * @param {number} target | |
* * @return {number[]} | |
* */ | |
var twoSum = function(nums, target) { | |
for (var i = 0; i<=nums.length; i++) { | |
for (var j =i+1; j<=nums.length; j++) { | |
if (nums[i] + nums[j] === target) return [i, j]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* * @param {number[]} nums | |
* * @param {number} target | |
* * @return {number[]} | |
* */ | |
var twoSum = function(nums, target) { | |
var map = []; | |
for (var i=0; i<nums.length; i++) { | |
var value = nums[i]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number[]} nums | |
* @return {number} | |
*/ | |
var singleNumber = function(nums) { | |
nums.sort(); | |
for (var i=0; i<nums.length; i=i+2) { | |
if (nums[i] !== nums[i+1]) return nums[i]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number[]} nums | |
* @return {number} | |
*/ | |
var singleNumber = function(nums) { | |
var result = 0; | |
for (var i=0; i<nums.length; i++) { | |
result = result ^ nums[i]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var chai = require('chai'); | |
var expect = chai.expect; | |
describe('Test Single Number', function() { | |
it('Test1', function() { | |
var input = [1, 2, 2, 3, 1]; | |
var result = singleNumber(input); | |
expect(result).to.equal(3); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for singly-linked list. | |
* function ListNode(val) { | |
* this.val = val; | |
* this.next = null; | |
* } | |
*/ | |
/** | |
* @param {ListNode} head |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Definition for singly-linked list. | |
function ListNode(val) { | |
this.val = val; | |
this.next = null; | |
} | |
var chai = require('chai'); | |
var expect = chai.expect; | |
describe('Test has cycle', function () { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {string} s | |
* @return {number} | |
*/ | |
var mStr = "-ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
var num = 26; | |
var titleToNumber = function(s) { | |
var result = 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {string} s | |
* @return {number} | |
*/ | |
var mStr = "-ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
var titleToNumber = function(s) { | |
var result = 0; |
OlderNewer