LeetCode #5 (#35, #38, #53)

35. Search Insert Position

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You may assume no duplicates in the array.

Example 1:

Input: [1,3,5,6], 5
Output: 2

Example 2:

Input: [1,3,5,6], 2
Output: 1

Example 3:

Input: [1,3,5,6], 7
Output: 4

Example 4:

Input: [1,3,5,6], 0
Output: 0
//Solution
//总结:只要遍历一遍即可,找到等于则返回,大于的话就是取代当前位置插入,小于就继续判断
int searchInsert(int* nums, int numsSize, int target){
    int i;
    int cnt = 0;
    for(i = 0;i<numsSize;i++)
    {
        if(nums[i]<target)
        {
            cnt ++;
        }
        else if (nums[i] >= target) return i;
    }
    return cnt;
}
38. Count and Say

The count-and-say sequence is the sequence of integers with the first five terms as following:

1.     1
2.     11
3.     21
4.     1211
5.     111221

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.

Given an integer n where 1 ≤ n ≤ 30, generate the nth term of the count-and-say sequence. You can do so recursively, in other words from the previous member read off the digits, counting the number of digits in groups of the same digit.

Note: Each term of the sequence of integers will be represented as a string.

Example 1:

Input: 1
Output: "1"
Explanation: This is the base case.

Example 2:

Input: 4
Output: "1211"
Explanation: For n = 3 the term was "21" in which we have two groups "2" and "1", "2" can be read as "12" which means frequency = 1 and value = 2, the same way "1" is read as "11", so the answer is the concatenation of "12" and "11" which is "1211".
//Solution
//总结:感觉也太坑了555
//首先它是计数的,计的是上一个数中出现的个数
//直接举例4:1211 表示上衣个数3中是1个2然后2个1
//一开始最难理解的是5:不是说好的3个1和1个2吗?当然不是,他还要分开,就是先1个1然后1个2最后2个1
//套娃行动启动!
class Solution {
public:
    string countAndSay(int n) {
        if(n<=0) return "";
        if(n==1) return "1";
        string str = "1";
        string temp ;
        int cnt;
        for(int i =0;i<n-1;i++)
        {
            temp = "";
            for(int j = 0;j<str.size();j++)
            {
                cnt =1;
                while(j +1 <str.size() && str[j]==str[j+1])
                {
                    cnt++;
                    j++;
                }
                temp = temp +to_string(cnt)+str[j];
            }
            str = temp;
        }
        return str;
    }
};


53. Maximum Subarray

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

Example 1:

Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.

Example 2:

Input: [1],
Output: 1

Example 3:

Input: [-2,1],
Output: 1

Example 4:

Input: [-2],
Output: -2

Follow up:

If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

//Solution
//总结:
//O^2暴力当然可以
int maxSubArray(int* nums, int numsSize){
    int max = nums[0];
    int sum;
    for(int i = 0;i<numsSize;i++)
    {
        sum = 0;
        for(int j =i;j<numsSize;j++)
        {
            sum = sum +nums[j];
            if(sum>max) max = sum;
        }
    }
    return max;
}
//想想自己也是学过数据结构的人,最大子列和怎么可以搞我?
int maxSubArray(int* nums, int numsSize){
    int ThisSum =0, MaxSum =nums[0];
	for(int i=0;i<numsSize;i++)
	{
		ThisSum +=nums[i];
		if(ThisSum>MaxSum){
			MaxSum = ThisSum;
		}
        if(ThisSum<0){
			ThisSum = 0;
		}
	}
    return MaxSum;
}

发布了72 篇原创文章 · 获赞 10 · 访问量 5840

猜你喜欢

转载自blog.csdn.net/weixin_44198992/article/details/105322166