1.Merge Sorted Array
Given two sorted integer arrays A and B, merge B into A as one sorted array.
void merge(int A[], int m, int B[], int n) {
while(m>0&&n>0)
{
if(A[m-1]>B[n-1])
{
A[m+n-1]=A[m-1];
m--;
}
else
{
A[m+n-1]=B[n-1];
n--;
}
}
while(m>0)
{
A[m+n-1]=A[m-1];
m--;
}
while(n>0)
{
A[m+n-1]=B[n-1];
n--;
}
}
2.Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example, Given 1->1->2, return 1->2.
ListNode *deleteDuplicates(ListNode *head) {
if(head==NULL)
return NULL;
ListNode* result=head;
while(head->next!=NULL)
{
ListNode*mid=head->next;
if(head->val==mid->val)
{
head->next=mid->next;
delete mid;
}
else
{
head=head->next;
}
}
return result;
}
3.Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
分析:该题为简单的动态规划,用
step[i]
表示到第i层的方法数
step[i]=step[i−1]+step[i−2]
.
int climbStairs(int n) {
vector<int> step(n+1,0);
step[0]=1; step[1]=1;
for(int i=2;i<=n;i++)
{
step[i]=step[i-1]+step[i-2];
}
return step[n];
}
4.Add Binary
Given two binary strings, return their sum (also a binary string).For example,
a="11"b="1"
Return “100”.
string addBinary(string a, string b) {
int lena=a.length();
int lenb=b.length();
if(lena==0)
return b;
if(lenb==0)
return a;
string result;
int flag=0;
int i=0;
while(i<lena&&i<lenb)
{
int temp=a[lena-1-i]-'0'+b[lenb-1-i]-'0'+flag;
if(temp>=2)
{
flag=1;
result.push_back(temp-2+'0');
}
else
{
flag=0;
result.push_back(temp+'0');
}
i++;
}
while(i<lena)
{
int temp=a[lena-1-i]-'0'+flag;
if(temp>=2)
{
flag=1;
result.push_back(temp-2+'0');
}
else
{
flag=0;
result.push_back(temp+'0');
}
i++;
}
while(i<lenb)
{
int temp=b[lenb-1-i]-'0'+flag;
if(temp>=2)
{
flag=1;
result.push_back(temp-2+'0');
}
else
{
flag=0;
result.push_back(temp+'0');
}
i++;
}
if(flag==1)
{
result.push_back('1');
}
int len=result.length();
for(int j=0;j<len/2;j++)
{
char temp=result[j];
result[j]=result[len-j-1];
result[len-j-1]=temp;
}
return result;
}
5.Plus One
Given a non-negative number represented as an array of digits, plus one to the number.
vector<int> plusOne(vector<int> &digits) {
reverse(digits.begin(),digits.end());
int r,i=0;
bool flag;
int length=digits.size();
do{
flag=false;
r=digits[i]+1;
if(r>=10)
{
digits[i]=r%10;
flag=true;
}
else
{
digits[i]=r;
}
i++;
}while(flag&&i<length);
if(flag)
{
digits.push_back(1);
}
reverse(digits.begin(),digits.end());
return digits;
}
6.Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ’ ‘, return the length of last word in the string.If the last word does not exist, return 0.
int lengthOfLastWord(string s) {
int len=s.length();
int count=0;
if(len==0)
return 0;
while(len>0&&s[len-1]==' ')
len--;
if(len==0)
return 0;
while(len>0&&s[len-1]!=' ')
{
len--;
count++;
}
return count;
}
7.Count and Say
The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, …后一个数是前一个数的读法,一个1即11,两个1即21,一个2一个1即1211,一个1一个2两个1即111221.
string solve(string s)
{
string res;
char pre =s[0];
int count = 1;
for(int i = 1; i < s.size(); i ++)
{
if(s[i]==pre)
{
count ++;
}
else
{
char tmp = count+'0';
res = res + tmp + pre;
pre = s[i];
count = 1;
}
}
char tmp = count+'0';
res= res + tmp + pre;
return res;
}
string countAndSay(int n) {
string res = "1";
int j = 1;
while( j< n){
res=solve(res);
j++;
}
return res;
}
8.Valid Sudoku
Determine if a Sudoku is valid,即每一行,每一列及每一个小块中是否有相同的数。
bool isValidSudoku(vector<vector<char> > &board) {
char exist[10][10]={0};
int i,j;
for(i=0; i<9; i++)
{
for(j=0; j<9; j++)
{
if(board[i][j]!='.')
{
if(exist[i][board[i][j] - '0']& 0x1)return false;
if(exist[j][board[i][j] - '0']& 0x2) return false;
if(exist[(i/3) *3 + j/3][board[i][j] - '0']& 0x4) return false;
exist[i][board[i][j] - '0']^=0x1;
exist[j][board[i][j] - '0']^=0x2;
exist[(i/3) *3 + j/3][board[i][j] - '0']^=0x4;
}
}
}
return true;
}
9.Implement strStr()
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. KMP算法。
void GetNextval(string p, vector<int>& next)
{
int pLen = p.length();
next[0] = -1;
int k = -1;
int j = 0;
while (j < pLen - 1)
{
//p[k]表示前缀,p[j]表示后缀
if (k == -1 || p[j] == p[k])
{
++j;
++k;
//较之前next数组求法,改动在下面4行
if (p[j] != p[k])
next[j] = k; //之前只有这一行
else
//因为不能出现p[j] = p[ next[j ]],所以当出现时需要继续递归,k = next[k] = next[next[k]]
next[j] = next[k];
}
else
{
k = next[k];
}
}
}
int strStr(string haystack, string needle) {
int lena=haystack.length();
int lenb=needle.length();
if(lenb==0)
return 0;
int i=0,j=0;
vector<int> next(lenb, 0);
GetNextval(needle, next);
while (i < lena && j < lenb)
{
//①如果j = -1,或者当前字符匹配成功(即S[i] == P[j]),都令i++,j++
if (j == -1 || haystack[i] == needle[j])
{
i++;
j++;
}
else
{
j = next[j];
}
}
if (j == lenb)
return i - j;
else
return -1;
}
10.Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.
int removeElement(int A[], int n, int elem) {
int length=n;
for(int i=n-1;i>=0;i--)
{
if(A[i]==elem)
{
if(i==(length-1))
{
length--;
}
else
{
int swap=A[i];
A[i]=A[length-1];
A[length-1]=swap;
length--;
}
}
}
return length;
}
11.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.
int removeDuplicates(int A[], int n) {
if(n==0)
return 0;
int length=0;
for(int i=1;i<n;i++)
{
if(A[length]!=A[i])
{
A[length+1]=A[i];
length++;
}
}
return length+1;
}