leetcode自练编程题(简单)

1. 两数之和

给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target  的那 两个 整数,并返回它们的数组下标。

你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。

你可以按任意顺序返回答案。

示例 1:

输入:nums = [2,7,11,15], target = 9
输出:[0,1]
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。

using namespace std;

#include <iostream>
#include <vector>

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        vector<int> ret = {};

        int i = 0, j = 0;

        for (i = 0; i < nums.size(); i++ ) 
        {
          for (j = 0; j < nums.size(); j++) 
          {     
            if (j==i) 
              break;            
            if (nums[i] + nums[j] == target)
            {
              ret.emplace_back(i);
              ret.emplace_back(j);              
              return ret;
            }            
          }
        } 

        return ret;
    }
};

int main ()
{
  vector<int> input = {2,7,11,15};
 
  Solution solu;  

  vector<int> output = solu.twoSum(input, 26);

  cout << output[0] << "," << output[1] << endl;

  return 0;
}

2. 最长公共前缀

using namespace std;

#include <iostream>
#include <vector>

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
      string out = "";
      int i = 0, j = 0;
      int len = strs[0].length();
      bool is_same = true;
      for (i=0; i<len; i++) {
        char ch = strs[0].c_str()[i];
        printf("%c\n", ch); 
        
        for (j=1; j<strs.size(); j++){
          if (ch !=  strs[j].c_str()[i]){
            is_same = false;
            break;
          } else {
            printf("%c = %c\n", ch, strs[j].c_str()[i]);
          }
        }
        if (is_same) {          
          out += ch;
          printf("out = %s\n", out.c_str());
        } else {
          break;
        }
      }
      printf("return = %s\n", out.c_str());
      return out;
    }
};

int main ()
{
  vector<string> vec_str = {string("ab123"), string("abc45"), string("abd78")};

  Solution solu;
  string str = solu.longestCommonPrefix(vec_str);

  printf("result = %s\n", str.c_str());    
  return 0;
}

3. 有效括号

有效字符串需满足:

左括号必须用相同类型的右括号闭合。
左括号必须以正确的顺序闭合。
每个右括号都有一个对应的相同类型的左括号。
 

示例 1:

输入:s = "()[]{}"
输出:true

#include <iostream>
#include <stack>

using namespace std;

class Solution {
public:
    bool isPair(char a, char b)
    {
      if ((a == '(') && (b == ')')||
          (a == '[') && (b == ']')||
          (a == '{') && (b == '}')) {
        return true;
      } else {
        return false;
      }
    }

    bool isValid(string s) {
      int len = s.length();
      const char * pstr = s.c_str();      
      
      stack <char> st;
      for(int i=0; i<len; i++) {        
        if ((pstr[i] == '{') ||
            (pstr[i] == '(') ||
            (pstr[i] == '[') ) {          
            st.push(pstr[i]);
        } else if ( (pstr[i] == '}') || 
             (pstr[i] == ')') || 
             (pstr[i] == ']')) {
          if(st.empty()) {
            return false;
          }else{
            if (isPair(st.top(), pstr[i])){
              st.pop();
            } else {            
              return false;
            }  
          }      
        } else {
          continue;
        }        
      }
     
      if(st.size() == 0) {
        return true;
      } else {        
        return false;
      }      
    }
};


int main ()
{
  string str = "([{}])";

  Solution solu;

  if (solu.isValid(str)){
    cout << "true" << endl;
  } else {
    cout << "false" << endl;
  }
 
  return 0;
}

4. 线程

#include <iostream>
#include <thread>
using namespace std;

class Para
{
public:
    Para() { cout << "Create" << endl; }
    Para(const Para& p) { cout << "Copy" << endl; }//参数做复制时对象进行了拷贝构造函数
    ~Para() { cout << "Drop" << endl; }
    string name;
};

void ThreadMain(int p1, float p2, string str, Para p4)
{
    getchar();
    this_thread::sleep_for(100ms);
    getchar();
    cout << "return" << endl;
    //cout << "ThreadMain " << p1 << " " << p2 << " " << str <<" "<<p4.name<< endl;
    //cout << "ThreadMain name = " << p4.name << endl;
}

void ThreadMainPtr(Para* p)
{
    this_thread::sleep_for(100ms);
    cout << "ThreadMainPtr name = " << p->name << endl;
}

void ThreadMainRef(Para& p)
{
    this_thread::sleep_for(100ms);
    cout << "ThreadMainRef name = " << p.name << endl;
}

int main(int argc, char* argv[])
{
    #if 1
    {
       //01
        Para p;
        p.name = "test ref";
        thread th(ThreadMainRef, ref(p));
        th.detach();
    }
    getchar();
      //02
    {
        Para p;
        p.name = "test ThreadMainPtr name";
        thread th(ThreadMainPtr, &p); //错误 ,detach不行,线程访问的p空间会提前释放
        th.detach();
    }
    getchar();
    //03
    {
        Para p;
        p.name = "test ThreadMainPtr name";
        thread th(ThreadMainPtr, &p);
        th.join();

    }
    getchar();
    #endif

    //04
    thread th;
    {
    float f1 = 12.1f;
    Para p;
    p.name = "test Para class 04";
    cout << "1" << endl;

    //所有的参数做复制
    th =  thread(ThreadMain, 101, f1, "test string para",p);
    cout << "2" << endl;
    th.join();
    getchar();
    }

    return 0;
}

5. 判断环形链表

给你一个链表的头节点 head ,判断链表中是否有环。

如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值