软件工程实验一
删除排序数组的重复数字
public class Solution {
/**
* @param A: a array of integers
* @return : return an integer
*/
public int removeDuplicates(int[] nums) {
// write your code here
int i,j;
int m=nums.length;
for(i=0;i<m-1;i++)
{
if(nums[i]==nums[i+1])
{
for(j=i;j<m-1;j++)
{
nums[j]=nums[j+1];
}
m--;
i--;
}
}
return m;
}
}
买卖股票的最佳时机
class Solution {
public:
/**
* @param prices: Given an integer array
* @return: Maximum profit
*/
int maxProfit(vector<int> &prices) {
// write your code here
int i;
int ly=0;
if(prices.size()<2)
{
return ly;
}
int zx=prices[0];
for(i=1;i<prices.size();i++)
{
if(prices[i]-zx<ly)
{
ly=ly;
}
else
{
ly=prices[i]-zx;
}
if(zx>prices[i])
{
zx=prices[i];
}
else
{
zx=zx;
}
}
return ly;
}
};
爬楼梯
class Solution {
public:
/**
* @param n: An integer
* @return: An integer
*/
int climbStairs(int n) {
// write your code here
int i;
if(n<=1)
{
return 1;
}
int a[n+1];
a[0]=1;
a[1]=1;
for(i=2;i<n+1;i++)
{
a[i]=a[i-1]+a[i-2];
}
return a[n];
}
};