카타 8급 Convert a String to a Number!

1 C++[ | ]

int string_to_number(const std::string& s) {
  return stoi(s);
}
int string_to_number(const std::string& s) {
  int len = s.length();
  bool neg = false;
  int res = 0;
  for(int i=0; i<len; i++) {
    char ch = s[i];
    if(ch == '-') {
      neg = true;
      continue;
    }
    res *= 10;
    res += ch-'0';
  }
  return neg?-res:res;
}

2 JavaScript[ | ]

var stringToNumber = function(str){
  return parseInt(str);
}
var stringToNumber = function(str){
  return Number(str);
}
var stringToNumber = function(str){
  return +str;
}
var stringToNumber = function(str){
  return str << 0;
}
var stringToNumber = function(str){
  return 1*str;
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}