leedcode--Excel Sheet Column Number
2016-03-22 13:40
411 查看
Related to question Excel Sheet Column Title
Given a column title as appear in an Excel sheet, return its corresponding column number.
解题思路:进制的转换,26进制转10进制,注意点就是A-Z不是以0开头,所以要记得加1。
java版:
c++:
Given a column title as appear in an Excel sheet, return its corresponding column number.
解题思路:进制的转换,26进制转10进制,注意点就是A-Z不是以0开头,所以要记得加1。
java版:
public class Solution { public int titleToNumber(String s) { int res=0; for(int i=0;i<s.length();i++){ res=res*26+(s.charAt[i]-'A'+1);//得到每一位进行减A,然后加1 } } return res; }
c++:
class Solution { public: int titleToNumber(string s) { int res=0; for (int i=0;i<s.length();i++){ res=res*26+(s[i]-'A'+1);//和java有所不同的是,c++可以直接得到字符串的每一位。 } return res; } };
相关文章推荐
- leedcode题解
- 判断二叉树是否相等:
- 【LeedCode】328. Odd Even Linked List
- Letter Combinations of a Phone Number题意
- 234. Palindrome (Linked List)
- 【算法题】Leedcode 273. Integer to English Words 用英文表示数字
- leetcode 67---Add Binary 二进制 进位 String
- 237. Delete Node in a Linked List--LeetCode Record
- [LeedCode]Find the Duplicate Number/Linked List Cycle II
- [LeedCode] Single Number II
- leedcode一刷--Binary Tree
- leedcood一刷--Binary Search
- LeedCode种有关链表List题目总结二
- LeedCode种有关链表List题目总结一
- Best Time to Buy and Sell Stock I\II\III\IV
- 算法中常见的Math类问题总结(一)
- 88. Merge Sorted Array
- 118. Pascal's Triangle
- 26. Remove Duplicates from Sorted Array
- 80. Remove Duplicates from Sorted Array II