Skip to content

Instantly share code, notes, and snippets.

@dreamline2
Created January 15, 2018 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dreamline2/17b5ef3202fad0933ffcd759d04ba730 to your computer and use it in GitHub Desktop.
Save dreamline2/17b5ef3202fad0933ffcd759d04ba730 to your computer and use it in GitHub Desktop.
ezprice_interview_question
//https://leetcode.com/problems/basic-calculator/description/
var calculate = function(s) {
var positiveArr = s.replace(/\s|\(|\)|\-\d{1}/g,"").split(/\+/);
var positiveNum = positiveArr.reduce( (x,y) => parseInt(x) + parseInt(y) );
var hasNegative = s.replace(/\s|\(|\)/g,"").replace(/^\d{1}|\+\d{1}/g, "");
var negativeNum, negativeArr;
if (hasNegative) {
negativeArr = hasNegative.split(/\-/).filter(x=>x);
negativeNum = parseInt(negativeArr.reduce( (x,y) => parseInt(x) + parseInt(y) ));
} else {
negativeNum = 0;
};
return positiveNum - negativeNum;
};
// https://leetcode.com/problems/regular-expression-matching/description/
var isMatch = function(s, p) {
const re = new RegExp(p);
const supportRe = new RegExp(/\.|\*/);
if (supportRe.test(p)) {
return re.test(s);
} else {
return s == p;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment