Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array nums = [1,1,2]
,
Your function should return length = 2
, with the first two elements of nums being 1
and 2
respectively. It doesn't matter what you leave beyond the new length.
给出有序序列,把序列中重复出现的数字删掉,并返回数组长度
class Solution
{
public:
int removeDuplicates(vector<int>& nums)
{
if (nums.size()==0) return 0;
int temp=1;
for (int i=1;i<nums.size();i++)
{
if (nums[i]==nums[i-1]);
else
nums[temp++]=nums[i];
}
return temp;
}
};
Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
删除数组中指定元素
class Solution {
public:
int removeElement(vector<int>& nums, int val)
{
int mark=0;
for (int i=0;i<nums.size();i++)
if (nums[i]==val);
else
nums[mark++]=nums[i];
return mark;
}
};
Implement strStr()
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
模拟strstr函数
class Solution
{
public:
int strStr(string haystack, string needle)
{
int a=haystack.length();
int b=needle.length();
int j,i;
for (i=0;i+b<=a;i++)
{
for (j=0;j<b;j++)
if (haystack[i+j]!=needle[j])
break;
if (j==b)
return i;
}
return -1;
}
};
Divide Two Integers
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
不用除法写除法 --# 注意处理越界
class Solution
{
public:
int divide(int dividend, int divisor)
{
int x=1;
if (dividend<0) x*=-1;
if (divisor<0) x*=-1;
long long big=abs((long long )dividend);
long long small=abs((long long )divisor);
long long cnt=1;
long long mark=small;
while (mark<big)
{
mark<<=1;
cnt<<=1;
}
long long ans=0;
while (mark>=small)
{
if (mark<=big)
{
big-=mark;
ans+=cnt;
}
cnt>>=1;
mark>>=1;
}
ans*=x;
if (ans==INT_MAX+1ll) ans--;
return ans;
}
};
Substring with Concatenation of All Words
You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in wordsexactly once and without any intervening characters.
For example, given:
s: "barfoothefoobarman"
words: ["foo", "bar"]
You should return the indices: [0,9]
.
(order does not matter).
给定一个字符串S和一个字符串数组words,words中的字符串长度都相等,找出S中所有的子串恰好包含words中所有字符各一次,返回子串的起始位置。用map判断一下即可
class Solution
{
public:
vector<int> findSubstring(string s, vector<string>& words)
{
vector<int>ans;
if (words.size()==0) return ans;
int wordNum=words.size();
int wordLen=words[0].length();
int wordsLen=wordNum*wordLen;
map<string,int>word;
for (int i=0;i<wordNum;i++)
{
if (word.find(words[i])==word.end())
word[words[i]]=1;
else
word[words[i]]++;
}
int i,j;
for (i=0;i<=s.length()-wordsLen;i++)
{
map<string,int>cur;
for (j=i;j<i+wordsLen;j+=wordLen)
{
string temp=s.substr(j,wordLen);
if (cur.find(temp)==cur.end())
cur[temp]=1;
else
cur[temp]++;
if (word.find(temp)==word.end()) break;
if (cur[temp]>word[temp]) break;
}
if (j>=i+wordsLen) ans.push_back(i);
}
return ans;
}
};