LeetCode第二天

1、Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Determine if you are able to reach the last index.

For example:
A = [2,3,1,1,4], return true.

A = [3,2,1,0,4], return false.

辅助数组,表示是否可达,判断该位置是否可达,从该位置往前,判断前面位置可达且两者距离小于可跳值。

class Solution {
public:
    bool canJump(vector<int>& nums) {
       int len=nums.size();
       if(len==1)
       {
           return true;
       }
       vector<bool> vbFlag(len,false);
       vbFlag[0]=true;
       for(int i=1;i<len;++i)
       {
           for(int j=i-1;j>=0;--j)
           {
               if(vbFlag[j]&&nums[j]>=i-j)
               {
                   vbFlag[i]=true;
                   break;
               }
           }
       }
       return vbFlag[len-1];
    }
};

2、Roman to Integer
Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

判断前面是否小于当前,小于则减掉重算。

class Solution {
public:
    int romanToInt(string s) {
        map<char,int> mRoman={
            {'I',1},{'V',5},{'X',10},{'L',50},
            {'C',100},{'D',500},{'M',1000}
        };
        int iRes=0;
        for(auto iter=s.cbegin();iter!=s.cend();++iter)
        {
            if(iter>s.cbegin()&&mRoman[*iter]>mRoman[*(iter-1)])
            {
                iRes+=(mRoman[*iter]-2*mRoman[*(iter-1)]);
            }
            else
            {
                iRes+=mRoman[*iter];
            }
        }
        return iRes;
    }
};

3、Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

排序后!!!检测数值与前面的值是否出现相等情况。

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        if(nums.size()<=1)
        {
            return false;
        }
        sort(nums.begin(),nums.end());
        for(auto iter=nums.begin()+1;iter!=nums.end();++iter)
        {
            if(*iter==*(iter-1))
            {
                return true;
            }
        }
        return false;
    }
};

4、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 titleToNumber(string s) {
        int len=s.length();
        if(len>1)
        {
            string str(s.substr(1));
            return (s[0]-'A'+1)*pow(26,len-1)+titleToNumber(str);//pow!!!
        }
        else
        {
            return s[0]-'A'+1;
        }
    }
};

5、Delete Node in a Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.

Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function.

删除该节点竟然只告诉该节点,那只能将后面一个节点的值copy过来,然后将后面的一个节点删掉。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    void deleteNode(ListNode* node) {
        ListNode *p=node->next;
        node->val=p->val;
        node->next=p->next;
        delete p;
        //free(p);
    }
};

6、Power of Three
Given an integer, write a function to determine if it is a power of three.

Follow up:
Could you do it without using any loop / recursion?
想了一晚上,结果竟然这么简单,数学啊数学,你就是这么奇妙,n是不是3的m次幂,你求出m然后求幂验算!!!当然,参考至

http://blog.csdn.net/yzhang6_10/article/details/51357226

最好是求10的对数。。。

class Solution {
public:
    bool isPowerOfThree(int n) {//log:ln,log10:lg
        return n>0&&n==pow(3,int(log10(n)/log10(3)+0.5));
    }
};

7、Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.

Note:
You may assume k is always valid, 1 ≤ k ≤ BST’s total elements.

将BST中序遍历取出就好了。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    void inOrder(TreeNode *root,vector<int> &v,int &k)
    {
        if(root)
        {
            inOrder(root->left,v,k);
            v.push_back(root->val);
            if(v.size()<=k)
            {
                inOrder(root->right,v,k);
            }
        }
    }
    int kthSmallest(TreeNode* root, int k) {
        vector<int> v;
        inOrder(root,v,k);
        return v[k-1];
    }
};
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值