Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit067b35b

Browse files
committed
add more solutions
1 parentf77e6fd commit067b35b

10 files changed

+499
-50
lines changed

‎150 Evaluate Reverse Polish Notation.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,7 @@ var evalRPN = function(tokens) {
3939
}
4040

4141
returnstack.pop();
42-
};
42+
};
43+
44+
45+
console.log(evalRPN([12,12,12,'*','+',3,4,'-','+']));
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* initialize your data structure here
3+
*@constructor
4+
*/
5+
varTwoSum=function(){
6+
this.hashmap=newMap();
7+
};
8+
9+
/**
10+
* Add the number to an internal data structure.
11+
*@param {number} input
12+
*@returns {void}
13+
*/
14+
TwoSum.prototype.add=function(input){
15+
this.hashmap[input]=this.hashmap[input]||0;
16+
this.hashmap[input]++;
17+
};
18+
19+
/**
20+
* Find if there exists any pair of numbers which sum is equal to the value.
21+
*@param {number} val
22+
*@returns {boolean}
23+
*/
24+
TwoSum.prototype.find=function(val){
25+
for(varkeyinthis.hashmap){
26+
vardiff=val-parseInt(key);
27+
28+
if(diff===parseInt(key)){
29+
if(this.hashmap[diff]>=2){
30+
returntrue;
31+
}
32+
}elseif(this.hashmap[diff]>=1){
33+
returntrue;
34+
}
35+
}
36+
37+
returnfalse;
38+
};
39+
40+
/**
41+
* Your TwoSum object will be instantiated and called as such:
42+
* var twoSum = new TwoSum();
43+
* twoSum.add(number);
44+
* twoSum.find(value);
45+
*/

‎205 Isomorphic Strings.js

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,49 @@
1-
// Leetcode #205
2-
// Language: Javascript
3-
// Problem: https://leetcode.com/problems/isomorphic-strings/
4-
// Author: Chihung Yu
1+
// Given two strings s and t, determine if they are isomorphic.
2+
3+
// Two strings are isomorphic if the characters in s can be replaced to get t.
4+
5+
// All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.
6+
7+
// For example,
8+
// Given "egg", "add", return true.
9+
10+
// Given "foo", "bar", return false.
11+
12+
// Given "paper", "title", return true.
13+
14+
// Note:
15+
// You may assume both s and t have the same length.
16+
17+
// Hide Company Tags LinkedIn
18+
// Hide Tags Hash Table
19+
// Hide Similar Problems (E) Word Pattern
20+
21+
22+
523
/**
624
*@param {string} s
725
*@param {string} t
826
*@return {boolean}
927
*/
1028
varisIsomorphic=function(s,t){
11-
if(s===null||t===null){
12-
returntrue;
29+
if(s.length!==t.length){
30+
returnfalse;
1331
}
1432

1533
varhash1={};
1634
varhash2={};
1735

18-
for(vari=0;i<s.length;i++){
19-
varsc=s[i];
20-
vartc=t[i];
21-
22-
hash1[sc]=tc;
23-
hash2[tc]=sc;
36+
for(vari=0;i<s.length;i++){
37+
hash1[s[i]]=t[i];
38+
hash2[t[i]]=s[i];
2439
}
2540

26-
varresult1="";
27-
varresult2="";
41+
varresult1='';
42+
varresult2='';
2843

29-
for(i=0;i<s.length;i++){
30-
sc=s[i]
31-
tc=t[i];
32-
result1+=hash1[sc];
33-
result2+=hash2[tc];
44+
for(i=0;i<s.length;i++){
45+
result1+=hash1[s[i]];
46+
result2+=hash2[t[i]];
3447
}
3548

3649
returnresult1===t&&result2===s;

‎224 Basic Calculator.js

Lines changed: 91 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,102 @@ Note: Do not use the eval built-in library function.
1818
*@param {string} s
1919
*@return {number}
2020
*/
21-
varcalculate=function(s){
22-
varstack=[],
23-
len=s.length,
24-
sum=0,
25-
num,
26-
ch,
27-
j,
28-
i;
21+
//var calculate = function(s) {
22+
// var stack = [],
23+
// len = s.length,
24+
// sum = 0,
25+
// num,
26+
// ch,
27+
// j,
28+
// i;
2929

30-
stack.push(1);
31-
stack.push(1);
30+
// stack.push(1);
31+
// stack.push(1);
3232

33-
for(i=0;i<len;i++){
34-
ch=s.charAt(i);
33+
// for (i = 0; i < len; i++) {
34+
// ch = s.charAt(i);
3535

36-
if(!isNaN(parseInt(ch))){
37-
num=parseInt(ch);
36+
// if (!isNaN(parseInt(ch))) {
37+
// num = parseInt(ch);
3838

39-
for(j=i+1;j<len&&!isNaN(parseInt(s.charAt(j)));j++){
40-
num=num*10+parseInt(s.charAt(j));
41-
}
39+
// for (j = i + 1; j < len && !isNaN(parseInt(s.charAt(j))); j++) {
40+
// num = num * 10 + parseInt(s.charAt(j));
41+
// }
4242

43-
sum+=stack.pop()*num;
43+
// sum += stack.pop() * num;
4444

45-
i=j-1;
46-
}elseif(ch==='+'||ch==='('){
47-
stack.push(stack[stack.length-1]);
48-
}elseif(ch==='-'){
49-
stack.push(stack[stack.length-1]*(-1));
50-
}elseif(ch===')'){
51-
stack.pop();
45+
// i = j - 1;
46+
// } else if (ch === '+' || ch === '(') {
47+
// stack.push(stack[stack.length - 1]);
48+
// } else if (ch === '-') {
49+
// stack.push(stack[stack.length - 1] * (-1));
50+
// } else if (ch === ')') {
51+
// stack.pop();
52+
// }
53+
// }
54+
55+
// return sum;
56+
// };
57+
58+
59+
60+
/**
61+
Implement a basic calculator to evaluate a simple expression string.
62+
63+
The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .
64+
65+
You may assume that the given expression is always valid.
66+
67+
Some examples:
68+
"1 + 1" = 2
69+
" 2-1 + 2 " = 3
70+
"(1+(4+5+2)-3)+(6+8)" = 23
71+
Note: Do not use the eval built-in library function.
72+
*/
73+
/**
74+
*@param {string} s
75+
*@return {number}
76+
*/
77+
78+
79+
80+
console.log(calculate("10+20-((2-4)*6-5)*6"));
81+
varcalculate=function(s){
82+
83+
84+
85+
86+
87+
}
88+
89+
functionhelper(s,i,sign,finalSum,currentSum){
90+
while(i<s.length){
91+
if(s[i].match(/[0-9]/)){
92+
varnum=0;
93+
94+
while(i<s.length){
95+
if(s[i].match(/[0-9]/)){
96+
num*=10;
97+
num+=parseInt(s[i]);
98+
i++
99+
}else{
100+
break;
101+
}
102+
}
103+
}elseif(s[i]==='-'){
104+
105+
}elseif(s[i]==='+'){
106+
returnnum+helper(s,i,sign,finalSum,currentSum);
107+
}elseif(s[i]==='*'){
108+
109+
}elseif(s[i]==='/'){
110+
111+
}elseif(s[i]==='('){
112+
113+
}elseif(s[i]===')'){
114+
52115
}
116+
117+
i++;
53118
}
54-
55-
returnsum;
56-
};
119+
}

‎261 Graph Valid Tree.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ var validTree = function(n, edges) {
3232
}
3333
}
3434

35-
returnunionFind.unionCount()===1;
35+
returnunionFind.count()===1;
3636
};
3737

3838
// reference: http://blog.csdn.net/dm_vincent/article/details/7655764
@@ -75,7 +75,7 @@ class UnionFind {
7575
returnthis.find(m)===this.find(n);
7676
}
7777

78-
unionCount(){
78+
count(){
7979
returnthis.count;
8080
}
8181
}

‎349 Intersection of Two Arrays.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Given two arrays, write a function to compute their intersection.
2+
3+
// Example:
4+
// Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].
5+
6+
// Note:
7+
// Each element in the result must be unique.
8+
// The result can be in any order.
9+
// Hide Tags Binary Search Hash Table Two Pointers Sort
10+
// Hide Similar Problems (E) Intersection of Two Arrays II
11+
12+
13+
/**
14+
*@param {number[]} nums1
15+
*@param {number[]} nums2
16+
*@return {number[]}
17+
*/
18+
varintersection=function(nums1,nums2){
19+
varhash={};
20+
varresult=[];
21+
vari=0;
22+
while(i<nums1.length||i<nums2.length){
23+
if(i<nums1.length){
24+
hash[nums1[i]]=hash[nums1[i]]||[];
25+
hash[nums1[i]][0]=true;
26+
}
27+
28+
if(i<nums2.length){
29+
hash[nums2[i]]=hash[nums2[i]]||[];
30+
hash[nums2[i]][1]=true;
31+
}
32+
33+
i++
34+
}
35+
36+
for(iinhash){
37+
if(hash[i][0]&&hash[i][1]){
38+
result.push(parseInt(i));
39+
}
40+
}
41+
42+
returnresult;
43+
};

‎350 Intersection of Two Arrays II.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Given two arrays, write a function to compute their intersection.
2+
3+
// Example:
4+
// Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].
5+
6+
// Note:
7+
// Each element in the result should appear as many times as it shows in both arrays.
8+
// The result can be in any order.
9+
// Follow up:
10+
// What if the given array is already sorted? How would you optimize your algorithm?
11+
// What if nums1's size is small compared to nums2's size? Which algorithm is better?
12+
// What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
13+
// Hide Tags Binary Search Hash Table Two Pointers Sort
14+
// Hide Similar Problems (E) Intersection of Two Arrays
15+
16+
17+
18+
/**
19+
*@param {number[]} nums1
20+
*@param {number[]} nums2
21+
*@return {number[]}
22+
*/
23+
varintersect=function(nums1,nums2){
24+
varhash={};
25+
vararr1,arr2;
26+
27+
if(nums1.length>nums2.length){
28+
arr1=nums2;
29+
arr2=nums1;
30+
}else{
31+
arr1=nums1;
32+
arr2=nums2;
33+
}
34+
35+
varcount=arr1.length;
36+
varresult=[];
37+
38+
for(vari=0;i<arr1.length;i++){
39+
hash[arr1[i]]=hash[arr1[i]]||0;
40+
hash[arr1[i]]++;
41+
}
42+
43+
for(i=0;i<arr2.length&&count!==0;i++){
44+
if(hash[arr2[i]]>0){
45+
hash[arr2[i]]--;
46+
count--;
47+
result.push(arr2[i]);
48+
}
49+
}
50+
51+
returnresult;
52+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp