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. leetcode-002

    给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -& ...

  2. 面试题: mysql数据库 已看1 简单的sql练习

    数据库总结--MySQL常见面试题 2015年03月24日 17:56:06 阅读数:7787 1.根据部门号从高到低,工资从低到高列出员工的信息 select * from employee ord ...

  3. 301ReidrectPages中重复记录导致的500 server error

    在Umbraco平台开发一个系统时,遇到一个问题,报错500 server error, system is currently unable to handle this request. 按下F1 ...

  4. 【ssm整合打印sql语句】

    #定义LOG输出级别log4j.rootLogger=INFO,Console,File #定义日志输出目的地为控制台log4j.appender.Console=org.apache.log4j.C ...

  5. mobile web页面调试方法

    此文已由作者张含会授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 开发过程问题排查 Chrome Emulation关键词:使用方便 模拟各种设备尺寸.像素比.自定义user ...

  6. 图像的读取,显示与保存(基于skimage模块)

    一 skiamge模块 skimage包的全称是scikit-image SciKit (toolkit for SciPy) ,它对scipy.ndimage进行了扩展,提供了更多的图片处理功能.它 ...

  7. Ubuntu使用技巧

    命令 获取系统安装包的编译源码及脚本 apt-get source package 查询端口被占用的进程 lsof -i:端口号 配置 配置阿里源 # mv /etc/apt/source.list ...

  8. SCUT - 289 - 小O的数字 - 数位dp

    https://scut.online/p/289 一个水到飞起的模板数位dp. #include<bits/stdc++.h> using namespace std; typedef ...

  9. LibreOJ #6192. 「美团 CodeM 复赛」城市网络

    #6192. 「美团 CodeM 复赛」城市网络 内存限制:64 MiB时间限制:500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: sqc 提交提交记录统计讨论测试数据   题目描 ...

  10. VBA学习笔记

    这是一个学习VBA编程的学习笔记. 一. 介绍 二. 使用手册 2.1. 如何在Excel2010中开始使用VBA? 2.2. 如何使用VBA编辑器进行编程? 三. 语法说明 3.1 数据类型 3.2 ...