-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode.cpp
110 lines (110 loc) · 2.85 KB
/
leetcode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//21. Merge Two Sorted Lists
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode *temp1,*temp2;
ListNode *temp=new ListNode(0);
temp1=l1;
temp2=l2;
ListNode *temp3=temp;
if(!l1) return l2;
if(!l2) return l1;
while(temp1!=NULL && temp2!=NULL)
{
if(temp1->val < temp2->val)
{
temp->next=new ListNode(temp1->val);
temp1=temp1->next;
}
else
{
temp->next=new ListNode(temp2->val);
temp2=temp2->next;
}
temp=temp->next;
}
if(temp1)
{
temp->next=temp1;
}
if(temp2)
{
temp->next=temp2;
}
temp=temp3;
temp=temp->next;
delete temp3;
return temp;
}
//26. Remove Duplicates from Sorted Array
int removeDuplicates(vector<int>& nums) {
if(nums.size() == 0)
return 0;
int length=1;
for(int i=1;i<nums.size();i++)
{
if(nums[length-1] != nums[i])
nums[length++]=nums[i];
}
return length;
}
//27. Remove Element
int removeElement(vector<int>& nums, int val) {
int lenght=0;
for(int i=0;i<nums.size();i++)
{
if(nums[i]!=val)
{
nums[lenght++]=nums[i];
}
}
return lenght;
}
//28. Implement strStr()
int strStr(string haystack, string needle) {
if(needle.size()==0)
return 0;
if(haystack.size()<needle.size())
return -1;
for(int i=0;i<=(haystack.size()-needle.size());i++)
{
if(haystack[i]==needle[0])
{
int j;
for(j=1;j<needle.size();j++)
{
if(haystack[i+j]!=needle[j])
{
break;
}
}
if(j==needle.size())
return i;
}
}
return -1;
}
//717. 1-bit and 2-bit Characters
bool isOneBitCharacter(vector<int>& bits) {
if(bits.size()==1)
return true;
if(bits[bits.size()-2]==0) //倒数第二个字符为0,最后一个零只能为第一个字符
return true;
int i=0;
while(i<bits.size())
{
if(bits[i]==1) //跳过第二个字母的第二个字符0或者1
i=i+2;
else
i=i+1; //跳过第一个字符
if(i==(bits.size()-1)) //到达最后一个字符0
return true;
}
return false;
}