inversion在matlab怎么用,没什么用的matlab代码1

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

四数相加

function answer=foursum_18_1(nums,target)

combination = nchoosek(nums,4);

m = sum(combination,2);

n = find(m==target);

answer = combination(n,:);

end

#三数最近

function answer=threesumclosest_16_1(nums,target)

combination = nchoosek(nums,3);

m = sum(combination,2);

n = m-target;

p = abs(n);

[x,y] = min(p);

answer = m(x,y);

end

#种花

function res= canPlaceFlowers(flowerbed,n)

t = [0 flowerbed 0];

res=true;

for i =1: (length(t)-2)

if t(i) == 0 && t(i+1) == 0 && t(i+2) == 0

t(i+1) = 1;

n = n-1;

i = i+1;

if n == 0

return

end

end

end

res=false;

end

#缺失数字

function answer=missingnumber_268(nums)

nums=sort(nums)

for i=1:length(nums)

if nums(i)~=i-1

answer=i-1

end

end

end

#搜索二维矩阵

function answer=searcha2dmatrix_74(matrix,target)

nRows=length(matrix(:,1))

nColumns=length(matrix(1,:))

answer=false

for i=1:nRows

for j=1:nColumns

if matrix(i,j)==target

answer=true

break

end

if answer==true

break

end

end

end

end

或者

function answer=searcha2dmatrix_741(matrix,target)

if find(matrix==target)

answer = true;

else

answer = false;

end

end

#矩阵置零

function answer=setmatrixzeroes_731(A)

[m,n] = find(A==0);

A(m,:) = 0;

A(:,n) = 0;

answer = A;

end

#买卖股票

function answer=besttimetobuyandsellstock2_122(prices)

profit=0

for i=2:length(prices)

tmp=prices(i)-prices(i-1)

if tmp>0

profit=profit+tmp

end

end

answer=profit

end

#合并数组

function answer=mergesortedarray_88(nums1,nums2,m,n)

for i=1:n

nums1(m+i)=nums2(i)

end

answer= sort(nums1)

end

#移除元素

function answer=removeelement(nums,val)

i=0

for j=1:length(nums)

if nums(j)==val

i=i+1

end

end

answer=length(nums)-i

end

#种花问题

function answer=flower_605(flowerbed,n)

flowerbed=[0,flowerbed,0]

Count=0

i=2

while i <= length(flowerbed)-1

if flowerbed(i-1) == 0 && flowerbed(i)==0 && flowerbed (i+1)==0

Count=Count+1

i=i+2

else

i=i+1

end

end

if n>Count

answer=false

else

answer=true

end

end

或者

function res= canPlaceFlowers(flowerbed,n)

t = [0 flowerbed 0];

res=true;

for i =1: (length(t)-2)

if t(i) == 0 && t(i+1) == 0 && t(i+2) == 0

t(i+1) = 1;

n = n-1;

i = i+1;

if n == 0

return

end

end

end

res=false;

end

#有效三角形

function answer=triangle_611(nums)

nums=[1,2,2,3,4]

nums=sort(nums)

C=nchoosek(nums,3)

n=length(C(:,1))

Rowset=[]%表示哪几行符合

answer=0

for i=1:n

if C(i,1)+C(i,2)>C(i,3)%按顺序排列后前两个边之和大于第三笔边

Rowset=[Rowset,i]%把符合条件的放入组中

answer=answer+1

end

end

answer=C(Rowset,:)%把符合的行分别列出来

end

#全局倒置function answer=inversion_775(A)

A = [1,2,0]

n=length(A)

Count1=0

for i=1:n

for j=1:n

if iA(j)

Count1=Count1+1

end

end

end

Count2=0

for i=1:n-1

if A(i)>A(i+1)

Count2=Count2+1

end

end

if Count1==Count2

answer=true

else

answer=false

end

或者

function res=isIdealPermutation(a)

res=true;

for i = 1:length(a)

if a(i) ~= i && ((abs(a(i) - i) > 2)||(abs(a(i) - i)==0))

res=false;

return

end

end

#适龄好友

function answer=numFriendRequests(A)

clc,clear

A=[20,30,100,110,120]

a=sort(A)

answer=0

n=length(a)

C=n*n

count=0

for i=1:n

for j=1:n

if ((a(i)>100)&&(a(j)<100)) || a(i)<=0.5*a(j)+7 || a(i)>a(j)

count=count+1

end

if i==j

count=count+1

end

end

end

answer=C-count

end

或者

function res=numFriendRequests(A)

a=sort(A);

res=0;

for i=1:(length(a)-1)

for j=(i+1):length(a)

if a(i)>0.5*a(j)+7 && (a(j)<=100 || a(i)>=100)

if a(i)==a(j)

res=res+2;

else

res=res+1;

end

end

end

end

#第三大的数

function thirdMax = thirdmax(A)

A = unique(A);

num = numel(A);

if num >= 3

temp = find(A == max(A));

A(temp) = [];

temp = find(A == max(A));

A(temp) = [];

thirdMax = max(A);

else

thirdMax = max(A);

end

end

TEST

A = input('输入一个数组A = ');

thirdMax = thirdmax(A);

disp(thirdMax);

或者

function answer = thirdmax(A)

A=[2,2,3,1]

A=unique(A)

n=length(A)

a = sort(A,'descend') %从大到小排列

if n >= 3

answer = a(3) %第一个

else

answer=a(1)

end

end

#观光组合

A=[8,1,5,2,6]

n=length(A)

scorearray=[]

for i=1:n

for j=i+1:n

score=A(i)+A(j)+i-j

scorearray=[scorearray,score]

end

end

answer=max(scorearray)

或者

function maxScoreSightseeingPair = maxscoresightseeingpair(A)

res = 0;

pre_max = A(1) + 1;

for j = 2:length(A)

res = max(res, pre_max + A(j) - j);

pre_max = max(pre_max, A(j) + j);

end

maxScoreSightseeingPair = res;

end

#最大连续1

function findMaxConsecutiveOnes = findmaxconsecutiveones(A)

count = 0;

temp = 0;

for i = 1:length(A)

if A(i) == 1

count = count + 1;

else

temp = max(temp,count);

count = 0;

end

end

temp = max(temp,count);

findMaxConsecutiveOnes = temp;

end

TEST

A = input('输入一个数组A = ');

findMaxConsecutiveOnes = findmaxconsecutiveones(A);

disp(findMaxConsecutiveOnes);

#消失数字

function answer = finddisappearednumber1(nums)

nums=[4,3,2,8,5,2,3,1]

A=tabulate(nums)

index=find(A(:,2)==0)

answer=A(index,1)'

end

#车站距离

function distanceBetweenBusStops = distancebetweenbusstops(distance, start, destination)

if start < destination

a = sum(distance(start + 1:destination));

else

a = sum(distance(destination + 1:start));

end

b = sum(distance) - a;

distanceBetweenBusStops = min(a,b);

end

TEST

distance = input('输入表示车站距离的数组:');

start = input('输入起始车站:');

destination = input('输入终点车站:');

distanceBetweenBusStops = distancebetweenbusstops(distance, start, destination);

disp(distanceBetweenBusStops);

#提莫攻击

function findPoisonedDuration = findpoisonedduration(timeSeries, duration)

time = 0;

if duration == 0

findPoisonedDuration = 0;

else

for i = 1:length(timeSeries) - 1

if timeSeries(i) + duration <= timeSeries(i+1)

time = time + duration;

else

time = time + timeSeries(i+1) - timeSeries(i);

end

end

time = time + duration;

findPoisonedDuration = time;

end

end

TEST

timeSeries = input('输入攻击的时间序列数组: ');

duration = input('输入中毒持续时间: ');

findPoisonedDuration = findpoisonedduration(timeSeries, duration);

disp(findPoisonedDuration);

#重复数据

function answer=duplicatenumber_442(nums)

nums=[4,3,2,7,5,2,3,1]

A=tabulate(nums)

index=find(A(:,2)==2)

answer=A(index,1)'

end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值