刷题第一天复习...

来源:互联网 发布:什么软件聊天 编辑:程序博客网 时间:2024/06/05 06:30

基本上很多都忘记了

写下来自己好记住,感谢地大某ACM队员的教导233

主要工具就是我的VS2015+leetcode


VS快捷键(有用的)

实际操作,按住Ctrl键不放,先按K键,再按F键,其他操作是类似的。
格式化全部代码       Ctrl+A+K+F
格式化选中的代码     Ctrl+K+F
注释代码   Ctrl+K+C(comment)
反注释代码  Ctrl+K+U


c++知识(对不起忘得比较厉害了)

vector<int>vec;

vec.size();//数组的数目

vec.capacity();//不分配内存的情况下的数目

vec[i];//访问第i个元素


题目:easy

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

我的思路:

遍历数组第一个变量为a,然后用target-a,找数组里有没有这个数,找到就输出,因为只有一个解。

class Solution 
{
public:
    vector<int> twoSum(vector<int>& nums, int target) 
    {
        vector<int> ta;
        int nu=0;
        bool aa=0;
        for(int a=0;a<nums.size();a++)
        {
            nu=target-nums[a];
         for(int b=0;b<nums.size()&&!aa;b++)
         {
             if(a==b)
             continue;
             
             if(nu==nums[b])
             {
                ta.push_back(a);
                ta.push_back(b);
                aa=1;
             }
         }
        }
        return ta;
    }
};

题目:easy

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

y=pow(1.3, 2);//计算1.3的2次幂 pow的返回值是double
我的思路:两个list先变成两个数字,然后再相加,再变成一个list输出
class Solution {public:    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)     {        long long numa=0,numb=0;        ListNode *ta=new ListNode(0);        ta->next=NULL;        ListNode *aa;        int a=0,b=0;        for(ListNode *x=l1;x!=NULL;x=x->next)        {            numa+=x->val*pow(10,a);             a++;        }        for(ListNode *y=l2;y!=NULL;y=y->next)        {            numb+=y->val*pow(10,b);            b++;        }        numa=numa+numb;        numb=numa;        a=0;        while(numb)        {a++;numb/=10;}        if(a==0)        a++;        aa=ta;        for(;a>0;a--)        {            ListNode *bb=new ListNode(numa%10);            numa-=numa%10;            numa=numa/10;            aa->next=bb;            aa=bb;           }        return ta->next;    }};
这道题现在有bug,....应该是我思路错了...我先复习下c++吧,待会问一下别人


原创粉丝点击