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. xgene:疾病相关基因,耳聋,彩色,老年痴呆,帕金森

    神经元的传递:一个下游神经元,它接受其上游神经元的各个突触传过来的信号,然而,每个突触对该下游神经元的激活权重是不同的. 从神经网络的本质上说,当人连续.多次遭受失败的时候,大脑内就会释放大量的抑制性 ...

  2. 7、scala面向对象-继承

    一.继承 1.extends Scala中,让子类继承父类,与Java一样,也是使用extends关键字 继承就代表,子类可以从父类继承父类的field和method:然后子类可以在自己内部放入父类所 ...

  3. HTML5学习笔记(六)web worker

    当在 HTML 页面中执行脚本时,页面的状态是不可响应的,直到脚本已完成.web worker 是运行在后台的 JavaScript,不会影响页面的性能,页面可以响应. 在创建 web worker ...

  4. 从阿里中台战略看企业IT架构转型之道

    此文是我阅读<企业IT架构转型之道>一书的学习笔记,所有内容出自钟华老师的这本书. 零.为何读<企业IT架构转型之道> 在加入X公司后,开始了微服务架构的实践,也开始了共享平台 ...

  5. Wannafly summer camp Day2I(思维)

    #include<bits/stdc++.h>using namespace std;int a[1000007],b[1000007],c[1000007];int find_max(i ...

  6. 51nod1042(0-x出现次数&分治)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1042 题意:中文题诶- 思路:这道题和前面的51nod100 ...

  7. 树状数组的神操作QAQ

    卧槽 厉害了,我的树状数组 1.单点修改,单点查询 用差分数组维护 #include<cstdio> #include<iostream> using namespace st ...

  8. Java基础笔记(七)—— 成员变量、静态变量、局部变量

    public class Test { int c; //成员变量(实例变量) static int s1; //静态变量(类变量)(全局变量) public static void main(Str ...

  9. promise封装小程序的请求类(request,清爽易懂)

    话不多说直接上代码,清爽易懂: import { config } from '../config.js' const tips = { 1:'抱歉出现了一个错误', 2:'网络错误', 1005:' ...

  10. Codeforces Round #565 (Div. 3) A. Divide it!

    链接: https://codeforces.com/contest/1176/problem/A 题意: You are given an integer n. You can perform an ...