LeetCode(605,581,566)

摘要:605盲改通过;581开始思路错误,后利用IDE修改(多重循环跳出方法);566用C语言时需要动态内存分配,并且入口参数未能完全理解,转用C++。

605. Can Place Flowers

中文描述:在保证数组中相邻的元素不同时为1时,判断是否能在原数组中添加n个1,返回false/true。

思路:先考虑前两个元素是否为零,再考虑中间的位置是否同时有三个相邻元素为零,最后考虑最后两个元素是否为零。当确定能添加一个“1”时,将原数组的相应位置置1,便于后面的判断。

//C语言描述:2017.6.7
bool canPlaceFlowers(int* flowerbed, int flowerbedSize, int n)
{
if(flowerbed[0]==0&&flowerbed[1]==0)
{
n--;
flowerbed[0]=1;
}
for(int i=1;i<flowerbedSize-2;i++)
if(flowerbed[i-1]==0&&flowerbed[i]==0&&flowerbed[i+1]==0)
{
n--;
flowerbed[i]=1;
}
if(flowerbed[flowerbedSize-1]==0&&flowerbed[flowerbedSize-2]==0)
n--;
return n < 1;
}

581.Shortest Unsorted Continuous Subarray

中文描述: 判断原始数组中需要排序的区域,返回其需要重新排序的元素数目

思路: 依次确定始末位置,然后做差处理后得到需要重新排序的元素数目。确定起始位置时,从第一个元素开始,依次将其与后面的每个元素进行比较,判断是否满足大小条件,若不满足则可立马判定起始位置,末尾位置判断步骤相同。

注: 在跳出多重循环时,第一种方法中使用了goto语句,编程实践中尽量不要使用,故有了第二种方法,使用break语句加上辅助标志flag,在外层循环中利用flag判断是否需要利用break跳出循环。

//方法一:C语言描述,使用goto语句
int findUnsortedSubarray(int* nums, int numsSize)
{ int start=0,final=0,i=0,j=0; for(i=0;i<numsSize-1;i++)
for(j=i+1;j<numsSize;j++)
{
if(nums[i]>nums[ j ])
{
start =i;
goto p1; //goto语句帮助跳出多重循环,但是不建议使用,但是真的很好用
};
}
p1: ; for(i=numsSize-1;i>0;i--)
for(j=i-1;j>=0;j--)
{
if(nums[i]<nums[j])
{
final = i;
goto p2; //goto语句帮助跳出多重循环,但是不建议使用,但是真的很好用
}
}
p2: ; if(final==start)
return 0;
else
return final-start+1;
}
//方法二:C语言描述,使用break语句和辅助标志flag
int start=0,final=0,i=0,j=0,flag; for(i=0,flag=0;i<numsSize-1;i++)
{
for(j=i+1;j<numsSize;j++)
{
if(nums[i]>nums[ j ])
{
start =i;
flag = 1;
break;
};
}
if(flag) break; // help getout of the big cycle
} for(i=numsSize-1,flag=0;i>0;i--)
{
for(j=i-1;j>=0;j--)
{
if(nums[i]<nums[j])
{
final = i;
flag = 1;
break;
}
}
if(flag) break;
} if(final==start)
return 0;
else
return final-start+1;

566. Reshape the Matrix

中文描述:如果维数合适的话,将原二维数组重塑成一维数组,否则返回原数组。类似matlab中的reshape函数。

思路:先判断维数是否正确,不正确原样输出,正确则reshape。reshape时,使用一个循环,先取原数组中的元素,依次将其放入新的数组中。

注:思路很简单,操作起来也很简单。但是使用C语言编写时,输入参数中的后两项int** columnSizes, int* returnSize未能理解,并且要求使用malloc函数,题目读了半天还是没有弄清楚怎么回事。故转用C++写,参考了一下已有的程序。

//C++描述:数组的行列转换。
class Solution {
public:
vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
int m = nums.size(), n = nums[0].size();
if (m * n != r * c) return nums;
vector<vector<int>> res(r, vector<int>(c));
for (int i = 0; i < r * c; ++i) {
res[i / c][i % c] = nums[i / n][i % n];
}
return res;
}
};

LeetCode(605,581,566)的更多相关文章

  1. c++ LeetCode(初级数组篇)十一道算法例题代码详解(一)

    原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/10940636.html 唉!最近忙着面试找实习,然后都是面试的很多是leetcode的算法题, ...

  2. LeetCode(194.Transpose File)(awk进阶)

    194. Transpose File Given a text file file.txt, transpose its content. You may assume that each row ...

  3. LeetCode(192. Word Frequency)

    192. Word Frequency Write a bash script to calculate the frequency of each word in a text file words ...

  4. LeetCode(283. 移动零)

    问题描述 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数 ...

  5. LeetCode(Add Two Numbers)

    一.题目要求 You are given two non-empty linked lists representing two non-negative integers. The digits a ...

  6. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  7. LeetCode 437. Path Sum III (路径之和之三)

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  8. LeetCode 371. Sum of Two Integers (两数之和)

    Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Exam ...

  9. LeetCode 292. Nim Game (取物游戏)

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

随机推荐

  1. 树莓派 Learning 003 --- GPIO 001 --- 点亮LED

    树莓派 Learning 003 - GPIO 001 - 点亮LED 我的树莓派型号:Raspberry Pi 2 Model B V1.1 装机系统:NOOBS v1.9.2 树莓派 Learni ...

  2. hadoop作业调优参数整理及原理

    hadoop作业调优参数整理及原理 10/22. 2013 1 Map side tuning参数 1.1 MapTask运行内部原理 当map task开始运算,并产生中间数据时,其产生的中间结果并 ...

  3. 13、Actor

    1.介绍 Scala的Actor类似于Java中的多线程编程.但是不同的是,Scala的Actor提供的模型与多线程有所不同. Scala的Actor尽可能地避免锁和共享状态,从而避免多线程并发时出现 ...

  4. Entity Framework Code-First(3):Setup Environment

    Setup Development Environment for EF Code-First: Let's setup the development environment for Code-Fi ...

  5. 20169219《linux内核原理与分析》第七周作业

    网易云课堂学习 把write系统调用加入到MenuOS里面 我在试验过程中在MenuOS里加入了time.time-asm.write和write-asm命令.以time和time-asm为例, 步骤 ...

  6. Boost Python学习笔记(四)

    你将学到什么 在Python中调用C++代码时的传参问题 基础类型 Python的字符串是常量,所以C++函数参数中的std::string &必须为const 修改源文件(main.cpp) ...

  7. 51nod1315(位运算)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1315 题意:中文题诶- 思路:位或(|)运算是二进制位有一个 ...

  8. 清北刷题冲刺 10-29 p.m

    洗澡 /* 这个题不能单纯判断左括号和右括号的多少,而应该从左到右扫一遍,看应该如何配对 */ #include<iostream> #include<cstdio> #inc ...

  9. 2017-10-13 NOIP模拟赛

    入阵曲 #include<iostream> #include<cstdio> #define maxn 401 #ifdef WIN32 #define PLL " ...

  10. 暴风魔镜SDK:MojingSDK For Unity V1.3.5112 (R).zip

    去年买了个暴风魔镜4,如今一直放在家里吃灰,这些天对Unity3D开发VR兴趣正浓,刚好公司项目不忙,花了几天玩玩暴风魔镜SDK,因为网上的资料不算多,暴风提供的文档也不太适合像我这样的Unity小白 ...