my leetcode#1 #2 #3 #4 #6 #7 #8

4 篇文章 0 订阅
2 篇文章 0 订阅

#1 Two Sum.cpp

#include<vector>
#include<stdlib.h>
#include<iostream>
using namespace std;


class Solution {
public:
Solution(){ cout<<"Solution Constructor"<<endl;}
    vector<int> twoSum(vector<int>& nums, int target) {
int index1 = 0;
int index2 = 0;
vector<int> result;
        vector<int> ivector = nums;
int num = ivector.size();
cout<<"ivector.size(): "<<num<<endl;
for(int i=0; i < num; i++){
index1 = i;
for(int j = i+1; j < num; j++){
if(ivector[i] + ivector[j] == target){
index2 = j;
break;
}
}
if(index2 != 0) break;
}


result.push_back(++index1);
result.push_back(++index2);
return result;
    }
};

+++++++++++++++++++++++++++++++++++++++++++++++++++

#2 Add Two Numbers

#include<vector>
#include<stdlib.h>
#include<iostream>
using namespace std;


 //Definition for singly-linked list.
  struct ListNode {
      int val;
      ListNode *next;
      ListNode(int x) : val(x), next(NULL) {}
  };
 
class Solution {
public:
Solution(){}
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
int carry = 0;
int sum = 0;
int count = 0;
ListNode *p1 = l1;
ListNode *p2 = l2;


while(p1->next != NULL || p2->next != NULL){
if(p1->next == NULL){
ListNode *node = new ListNode(0);
p1->next = node;
}
if(p2->next == NULL){
ListNode *node = new ListNode(0);
p2->next = node;
}
p1 = p1->next;
p2 = p2->next;
}


ListNode *head = new ListNode(0);
ListNode *p =head;
while(l1 != NULL){
count ++;
cout <<count<<endl;
sum = (l1->val + l2->val + carry)%10 ;
cout <<"l1->val: "<<l1->val<<"l2->val: "<<l2->val<<endl;
ListNode *node = new ListNode(sum);
p->next = node;
p = p->next;
carry = (l1->val + l2->val + carry)/10;
l1 = l1->next;
l2 = l2->next;
}
if(carry == 1){
ListNode *node = new ListNode(carry);
p->next = node;
p = p->next;
}

return head->next;
    }


};


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#3 Longest Substring Without Repeating Characters.cpp

#include <vector>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;


class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        vector<char> norepeat;
int max_length = 1;
int cur_length = 1;
int bound = 0;
vector<char>::iterator itor;
if(s.size() == 0) max_length = 0;
else{
norepeat.push_back(s[0]);
for(size_t i = 1; i < s.length(); i++){
if(is_include(norepeat, s[i], bound)){
itor = norepeat.begin();
while(bound>-1){
    itor=norepeat.erase(itor);
cur_length--;
bound--;
}
}
cur_length ++;
norepeat.push_back(s[i]);
if(cur_length > max_length)
max_length = cur_length;
}
}
return max_length;
    }


int is_include(vector<char> &cvec , char c, int &bound){
int flag = 0;
size_t i;
vector<char> vec = cvec;
for( i = 0; i < vec.size(); i++){
if(c == vec[i]){
flag = 1;
break;
}
}
bound = i;
return flag;
}
};


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#4 Median of Two Sorted Arrays.cpp

#include <vector>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;


class Solution {
public:
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int count;
int i = 0;
int j = 0;
int index = 0;
int n1 = nums1.size();
int n2 = nums2.size();
count = (n1 + n2)/2 + 1;
vector<int> median;
if(n1 == 0)
median = nums2;
else if(n2 == 0)
median = nums1;
else{
while(index < count){
if(i < n1 && j < n2){
if(nums1[i] < nums2[j]){
median.push_back(nums1[i]);
i++;
}
else{
median.push_back( nums2[j]);
j++;
}
}else{
if(i < n1) median.push_back(nums1[i++]);
if(j < n2) median.push_back(nums2[j++]);
}
index++;
}
}
if((n1 + n2)%2 == 0) return (double)(median[count-2] + median[count-1])/2;
else return (double)median[count-1];
    }
};

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#6 ZigZag Conversion.cpp

#include <stdlib.h>
#include <stdio.h>
#include <string.h>


char* convert(char* s, int numRows) {
    int len = strlen(s);
int i, k, val, v1, v2, j = 0, flag;
char *r = (char *)malloc(len);
val = 2*numRows-2;
if(numRows == 1) return s;
for(i = 0; i < numRows; i++){
v1 = val - i * 2;
v2 = i * 2;
k = i;
flag = 0;
while(k < len){
r[j] = s[k];
if(flag%2){
k+=v2;
if(v2) j++;
}else{
k+=v1;
if(v1) j++;
}
flag++;
}
}
r[len] = '\0';
return r;
}


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#7 Reverse Integer.cpp

#include <stdlib.h>
#include <stdio.h>
#include <string.h>


int reverse(int x) {
int i = 0;
int c = 0;
int len = 0;
int tmp = x;
int reverse = 0;
char data1[11];
char data2[11];
char *max = "2147483647";
int flag = 0;
if(x < 0){
flag = 1;
x = -x;
}

i = 0;
while(tmp!=0){
tmp = tmp/10;
i++;
  }
len = i;


i = 0;
while(i < len){
data1[len - i -1] =  x%10 + '0';
x = x/10;
i++;
}

i = 0;
while( i < len){
data2[len-1-i] = data1[i];
i++;
}
 
if(len == 10){
while(c < len){
if(max[c] > data2[c]) {
break;
}
else if(max[c] == data2[c]){
c++;
continue;
}
else return 0;
}
}


i = 0;
while(i < len){
reverse =  reverse*10 + (data2[i] - '0');
i++;
}
return flag? -reverse :  reverse;
}

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#8 String to Integer (atoi)

#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<iostream>
using namespace std;


class Solution {
public:
    int myAtoi(string str) {
      int value = 0;
      int flag = 0;
 int extra = 0;
 int slen = str.size();
      if(slen == 0) return value;
      else{
while(str[extra] == ' ') extra++; 
        if(str[extra] == '-'){
          flag = 1;
          value = getval(str, 1 + extra, flag);
        }else if(str[extra] == '+'){
          value = getval(str, 1 + extra, flag);}
        else value = getval(str, extra, flag);
        return value;
      }
    }
  int getval(string str, int start, int flag){
   double value = 0;
    for(int i = start; i < str.size(); i++){
        if((str[i]-'0')<0 || str[i] - '0' > 9) break;
        if(str[i] == ' ')
            value = value*10 + 0;
        else
    value = value*10 + (str[i]-'0');
    }
if(value>2147483647 && flag == 0) value = 2147483647;
if(flag == 1 && value > 2147483647 ) value = 2147483648;
    return flag==1? 0-value : value;
  }
};

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值