LeetCode Javascript实现 100. Same Tree 171. Excel Sheet Column Number

100. Same Tree

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} p
 * @param {TreeNode} q
 * @return {boolean}
 */
var isSameTree = function(p, q) {
    if(p===null&&q===null){
        return true;
    }
    if(p===null&&q!==null){
        return false;
    }
    if(q===null&&p!==null){
        return false;
    }
    if(p.val!==q.val){
        return false;
    }
    if(isSameTree(p.left,q.left)&& isSameTree(p.right,q.right)){
    return true;
    }
    else{
       return false;
    }

};

判断两个二叉树是不是相同,我的思路就是各种情况列出来,出错就false,然后false就会一直上浮到最上面,最后返回出正确的boolean值。

Your runtime beats 74.85% of javascriptsubmissions


171. Excel Sheet Column Number

/**
 * @param {string} s
 * @return {number}
 */
var titleToNumber = function(s) {
     var sl = s.length;
     var sc,result=0,i=0;
     while(sl!==0){
        sc = s.charAt(sl-1);
        result = (sc.charCodeAt()-64)*Math.pow(26,i)+result;
        sl--;
        i++;
     }
     return result;
};

这题主要用到函数charAt();charCodeAt();Math.pow(x,y)相当于转换成26进制的运算,字母和数字之间的转换用ASCII码完成。
Your runtime beats 22.00% of javascriptsubmissions.不知为何我的方法总是如此辣鸡- -。有机会第二次做的时候再追求效率吧

时间: 2024-10-16 11:43:44

LeetCode Javascript实现 100. Same Tree 171. Excel Sheet Column Number的相关文章

171. Excel Sheet Column Number(C++)

171. Excel Sheet Column Number Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 题目大意: 相

leetcode——171 Excel Sheet Column Number (表格字母与整数的映射)

Excel Sheet Column Number Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 Hide Tags: M

leetcode:171 Excel Sheet Column Number-每日编程第十题

Excel Sheet Column Number Total Accepted: 54683 Total Submissions: 140674 Difficulty: Easy Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -

171. Excel Sheet Column Number

Excel Sheet Column Number Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 static publi

【LeetCode】171 - Excel Sheet Column Number

Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 Solution:26进制转十进制,AAA=27*26+1 1 class

leetcode[171]Excel Sheet Column Number

Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 class Solution { public: int titleToNu

171. Excel Sheet Column Number Leetcode Python

Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 Credits: Special thanks to @ts for add

【LeetCode】171. Excel Sheet Column Number 解题小结

题目: Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 简单题,相当于26进制 class Solution { publi

LeetCode: 171 Excel Sheet Column Number(easy)

题目: Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 代码: 1 class Solution { 2 public: 3