今天做的题目如下:
一、27. Remove Element
题目的意思是从一个数组中移除给定的元素,并返回最后数组中元素的个数。
我的解法是使用vector中的erase函数来实现,代码如下:
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
for(int i=0; i<nums.size();) {
if(nums[i] == val) {
nums.erase(nums.begin()+i) ;
} else {
++ i ;
}
}
return nums.size() ;
}
};
LeetCode中另一种使用数组的解法是:对于一个元素i(i从0开始),如果它前面有c个与给定值相同的元素,那么这个元素i应该移动到(i-c)这个位置,这样给定的元素就被替换掉了。
二、28. Implement strStr()
题目意思是找出字符串needle在字符串haystack中第一次出现的位置。
这道题是字符串的模式匹配问题,我使用了KMP来解决,代码如下:
class Solution {
public:
int strStr(string haystack, string needle) {
if((haystack == needle) || (haystack=="" && needle==""))
return 0 ;
for(int i=0; (haystack.size()-i)>=needle.size();) {
int k = i ;
int t = -1 ;
int j = 0 ;
for(j; j<needle.size(); ++j) {
if(haystack[k]==needle[0] && k>i && t==-1) {
t = k ;
}
if(needle[j] == haystack[k]) {
++ k ;
} else {
i = (t>0) ? t : (i+1);
break ;
}
}
if(j == needle.size()) {
return i ;
}
}
return -1 ;
}
};
题目意思是给定一个数组和一个数,找到这个数在数组中出现的位置,如果没找到则返回这个数应该插入在数组中的位置。
这道题很容易想到的解法是直接遍历数组,然后返回位置。代码如下:
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
for(int i=0; i<nums.size(); ++i) {
if(target <= nums[i])
return i ;
}
return nums.size() ;
}
};
也可以在查找的时候使用二分法,代码如下:
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int min = 0 , max = nums.size()-1 , mid = (max+min)/2 ;
while((nums.size()>1) && (min<max)) {
if(target == nums[mid]) {
return mid ;
} else if(target < nums[mid]) {
max = mid - 1 ;
} else {
min = mid + 1 ;
}
cout << min << endl ;
mid = (max+min)/2 ;
}
cout << mid << endl ;
if(target <= nums[mid])
return mid ;
else
return mid+1 ;
}
};
四、38. Count and Say
题目意思是给定一个数字,返回其对应的字符串。数字与字符串之间的转换规则如下:
1
is read off as "one 1"
or 11
.
11
is read off as "two 1s"
or 21
.
21
is read off as "one 2
, then one 1"
or 1211
.
可以发现每一个数字读出来的对应字符串都是下一个数字所代表的字符串。
根据题意,我的代码如下:
#include <sstream>
class Solution {
public:
string countAndSay(int n) {
string s = "1";
string ss = "" ;
for(int i=0; i<n-1; ++i) {
int count = 1;
for(int j=0; j<s.length();++j) {
if(j!=s.length()-1 && s[j+1] == s[j]) {
++ count ;
} else {
ss = ss + to_string(count) + s[j] ;
count = 1 ;
}
}
if(ss!="") {
s = ss ;
ss = "" ;
}
}
return s ;
}
};
五、58. Length of Last Word
题目的意思是求出给定字符串中最后一个单词的长度,在该字符串中单词之间以空格分割。
解题时要注意字符串的末尾可能是空格,因此从字符串的末尾开始跳过空格找到最后一个字符串并求出长度。代码如下:
class Solution {
public:
int lengthOfLastWord(string s) {
int count = 0 ;
for(int i=s.size()-1; s!="" && i>=0; --i) {
if(s[i]!=' ') {
++ count ;
} else if(count!=0){
break ;
}
}
return (s=="") ? 0 : count ;
}
};
六、66. Plus One
题目意思是给定一个数组,将这个数组看成一个数字,返回这个数组加一之后的结果。
代码如下:
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int i = digits.size()-1 ;
while(i>=0 && digits[i]+1 >= 10) {
digits[i] = 0 ;
-- i ;
}
if(i==-1) {
digits.insert(digits.begin(),1) ;
} else {
++ digits[i] ;
}
return digits ;
}
};
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int i = digits.size()-1 ;
while(i>=0 && digits[i]+1 >= 10) {
digits[i] = 0 ;
-- i ;
}
if(i==-1) {
digits[0] = 1 ;
digits.push_back(0) ;
} else {
++ digits[i] ;
}
return digits ;
}
};