软件工程作业一

题目1:删除排序数组中的重复数字

描述:给定一个排序数组,在原数组中删除重复出现的数字,使得每个元素只出现一次,并且返回新的数组的长度。

不要使用额外的数组空间,必须在原地没有额外空间的条件下完成。

样例:给出数组A =[1,1,2],你的函数应该返回长度2,此时A=[1,2]。

 1 public class Solution {
 2     /**
 3      * @param A: a array of integers
 4      * @return : return an integer
 5      */
 6     public int removeDuplicates(int[] nums) {
 7         // write your code here
 8         int n = nums.length;
 9     int i,j=1;
10     if(n == 0)
11     return n;
12     for(i = 1; i < n; i++)
13     {
14             if(nums[i] != nums[j-1])
15             {
16                 nums[j++] = nums[i];
17             }
18         
19     }
20     return j;
21     }
22 }

《面向对象方法及软件工程》实验报告

实验题目:删除排序数组中的重复数字

问题描述:

删除掉一个已经排好序的数组中重复出现的数字,使之在数组中只出现一次

数据输入:

一个已经排好序的int型数组

数据输出:

删除掉重复元素后数组的长度

涉及的数据类型:

int型

解题思路:

因为是已经排好序的数组,所以重复元素只可能出现在某个元素相邻的位置,所以只需要比较相邻的两个元素是否相等,通过for循环去遍历数组中的每个元素,相等就跳过,去找数组下一个位置的数与第一个数作比较,不相等就将第二个位置的数赋给数组的第二个位置 ,同时给数组长度加1。

易错点(需要考虑的特殊情况):

当数组为空的时候,直接输出0,而return返回的长度是早就定义好的1,是错误的。

主要算法描述(伪代码):

Read nums[]

n ← nums.length

j ← 1

if n == 0 then

j ← 0

else

for I from 1 to n step 1

   If num[I] == nums[j-1]

      Nums[j] ← nums[i]

      j ← j+1

end for

end if

print j


题目2:买卖股票的最佳时机

描述:假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格。如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润。

样例:给出一个数组样例 [3,2,3,1,2], 返回 1

 1 public class Solution {
 2     /**
 3      * @param prices: Given an integer array
 4      * @return: Maximum profit
 5      */
 6     public int maxProfit(int[] prices) {
 7         // write your code here
 8         int i,j,desc;
 9     int n = prices.length;
10     int max_profit = 0;
11     for(i = 0; i < n; i++)
12     {
13         for(j = i+1; j < n; j++)
14         {
15             desc = prices[j] - prices[i];
16             if(max_profit < desc)
17                 max_profit = desc;
18         }
19     }
20     //cout<<max_profit<<endl;
21     return max_profit;
22     }
23 }

题目3:爬楼梯

描述:假设你正在爬楼梯,需要n步你才能到达顶部。但每次你只能爬一步或者两步,你能有多少种不同的方法爬到楼顶部?

样例:比如n=3,1+1+1=1+2=2+1=3,共有3中不同的方法,返回 3

 1 public class Solution {
 2     /**
 3      * @param n: An integer
 4      * @return: An integer
 5      */
 6     public int climbStairs(int n) {
 7         // write your code here
 8         if(n == 0)
 9         return 1;
10         int[] number = new int[n];
11         number[0] = 1;
12         if(n > 1)
13         number[1] = 2;
14         for(int i = 2; i < number.length; i++)
15         number[i] = number[i-1] + number[i-2];
16         return number[number.length-1];
17     }
18 }

题目4:最长回文子串

描述:给出一个字符串(假设长度最长为1000),求出它的最长回文子串,你可以假定只有一个满足条件的最长回文串。(注:“回文串”是一个正读和反读都一样的字符串)

样例

给出字符串 "abcdzdcab",它的最长回文子串为 "cdzdc"。

 1 public class Solution {
 2     /**
 3      * @param s input string
 4      * @return the longest palindromic substring
 5      */
 6     public String longestPalindrome(String s) {
 7         // Write your code here
 8     int length = s.length();
 9     if(length == 1)
10     return s;
11     int maxlength = 0;
12     int j,k,length_=0;
13     int start = 0,endd = 0;
14         for(int i = 0; i < length; i++)
15         {
16             j = i-1;
17             k = i+1;
18             while(j >= 0 && k < length && s.charAt(j) == s.charAt(k))
19             {
20                 if(k - j + 1 > maxlength)
21                 {
22                     maxlength = k - j + 1;
23                 }
24                 if(maxlength > length_)
25                 {
26                     length_ = maxlength;
27                     start = j;
28                     endd = k+1;
29                 }
30                 j--;
31                 k++;
32             }
33         }
34         for(int i = 0; i < length; i++)
35         {
36             j = i;
37             k = i+1;
38             while(j >= 0 && k < length && s.charAt(j) == s.charAt(k))
39             {
40                 if(maxlength < k-j+1)
41                 {
42                     maxlength = k-j+1;
43                 }
44                 if(maxlength > length_)
45                 {
46                     length_ = maxlength;
47                     start = j;
48                     endd = k+1;
49                 }
50                 j--;
51                 k++;
52             }
53         }
54         return s.substring(start,endd);
55     }
56 }
原文地址:https://www.cnblogs.com/sunshine-free/p/6512280.html