GVKun编程网logo

如何在Swift中获取2数组的公共元素列表?(如何在swift中获取2数组的公共元素列表数据)

15

对于如何在Swift中获取2数组的公共元素列表?感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍如何在swift中获取2数组的公共元素列表数据,并为您提供关于Algs4-1.4.12找出两个有序

对于如何在Swift中获取2数组的公共元素列表?感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍如何在swift中获取2数组的公共元素列表数据,并为您提供关于Algs4-1.4.12找出两个有序数组的公共元素-方法1、iOS |如何在swift中获取运行时图像的宽度/高度、ios – Swift:从元组的数组中获取一个元素数组、ios – 如何在Swift中搜索包含结构元素的数组?的有用信息。

本文目录一览:

如何在Swift中获取2数组的公共元素列表?(如何在swift中获取2数组的公共元素列表数据)

如何在Swift中获取2数组的公共元素列表?(如何在swift中获取2数组的公共元素列表数据)

我有两个数组:

fruitsArray = ["apple", "mango", "blueberry", "orange"]vegArray = ["tomato", "potato", "mango", "blueberry"]

我如何获得这两个数组中的常见项目列表

ouptput = ["mango", "blueberry"]

我无法使用,if contains(array, string)因为我想比较2个数组。

答案1

小编典典

您还可以结合使用filtercontains

let fruitsArray = ["apple", "mango", "blueberry", "orange"]let vegArray = ["tomato", "potato", "mango", "blueberry"]// only Swift 1let output = fruitsArray.filter{ contains(vegArray, $0) }// in Swift 2 and abovelet output = fruitsArray.filter{ vegArray.contains($0) }// orlet output = fruitsArray.filter(vegArray.contains)

SetArray仅计算共同元素相比

我们考虑以下代码片段:

let array1: Array = ...let array2: Array = ...// `Array`let commonElements = array1.filter(array2.contains)// vs `Set`let commonElements = Array(Set(array1).intersection(Set(array2)))// or (performance wise equivalent)let commonElements: Array = Set(array1).filter(Set(array2).contains)

我用Intshort和long Strings(10到100
Characters)(全部随机生成)做了一些(人工)基准测试。我总是用array1.count == array2.count

我得到以下结果:

如果您不只critical #(number of) elements转换为a,则更Set可取

data         |  critical #elements-------------|--------------------         Int |        ~50short String |       ~100 long String |       ~200

结果说明

使用该Array方法使用“蛮力”搜索,该搜索具有时间复杂度
O(N^2)N = array1.count =array2.count而与该Set方法相反O(N)。然而从转换ArraySet和背部是这解释了增加大数据非常昂贵的`critical

elements`更大的数据类型。


结论

对于Array具有约100个元素的小s,该Array方法很好,但对于较大的s,则应使用该Set方法。

如果您想多次使用此“常见元素”运算,则建议 仅* 在可能的情况下使用Sets (元素的类型必须为)。 *Hashable

结束语

ArraySet的转换比较昂贵,而从Set到的转换Array则非常便宜。

filter与with一起使用比在以下.filter(array1.contains)情况下性能更快.filter{array1.contains($0) }

  • 最后一个创建一个新的闭包( 仅一次 ),而第一个仅传递一个函数指针
  • 对于最后一个封闭件的调用创建花费空间和时间的附加堆栈帧( 多次O(N)

Algs4-1.4.12找出两个有序数组的公共元素-方法1

Algs4-1.4.12找出两个有序数组的公共元素-方法1

1.4.12编写一个程序,有序打印给定的两个有序数组(含有N个int值)中的所有公共元素,程序在最坏情况下所需的运行时间应该和N成比。
答:
图片
import java.util.Arrays;
public class TheSameElement
{
  public static void main(String[] args)
  {
     int[] a1=In.readInts(args[0]);
     int[] a2=In.readInts(args[1]);
     Arrays.sort(a1);
     Arrays.sort(a2);
     int a1lo=0;
     int a2lo=0;
     int a2KeyIndex=0;
     while (a1lo<a1.length && a2lo<a2.length)
     {
       a1lo=rankMax(a1,a1lo,a1.length-1,a1[a1lo]);
       a2KeyIndex=rankMax(a2,a2lo,a2.length-1,a1[a1lo]);
       if(a2KeyIndex!=-1)
       {
         StdOut.println(a1[a1lo]);
         a2lo=a2KeyIndex+1;
       }
       a1lo++;
     }
    
  }
 
   private static int rankMax(int[] a,int lo,int hi,int key)
  {
    int keyIndex=-1;
    while(lo<=hi)
    {
      int mid=lo+(hi-lo)/2;
      if (key<a[mid]) hi=mid-1;
      else if(key>a[mid]) lo=hi+1;
      else
      {
        keyIndex=mid;
        lo=mid+1;
      }//end if
    }//end while
      return keyIndex;
}//end rankMax
}

iOS |如何在swift中获取运行时图像的宽度/高度

iOS |如何在swift中获取运行时图像的宽度/高度

我在 Swift故事板中全屏使用’Aspect Fit’模式imageview.实际图像很好地保持比例.问题是我无法在运行时获得实际的图像宽度/高度.

当我使用imageview.frame.size.width时,它会按预期返回整个屏幕宽度.当我使用image.size.width时,它返回固定的图像宽度(实际图像宽度).但我的目的是获取UI中显示的运行时图像宽度.

如何从源头找到它.

编辑1

当前的肖像画面,中间有标签

enter image description here

当前景观屏幕中间有标签.标签宽度与imageview相同,但不是当前显示的图像宽度.

enter image description here

解决方法

请尝试以下代码.它完全符合您对iPhone 6S设备的要求.当设备屏幕方向更改为横向模式时,我正在计算标签框架的x位置和宽度.对于5S和4S等其他设备,您需要根据设备高度宽度比计算x位置和宽度.例如,我使用了一些常量值,如9和18.如果要为不同的设备使用代码,则需要使用比率因子.希望这有帮助!

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var theImageView: UIImageView!
@IBOutlet weak var theLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view,typically from a nib.
    theLabel.frame.size.width = (theImageView.image?.size.width)!

    NSNotificationCenter.defaultCenter().addobserver(self,selector: "rotated",name: UIDeviceOrientationDidChangeNotification,object: nil)
}

override func viewDidAppear(animated: Bool) {

    if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
    {
        if (theImageView.image?.size.height)! > (theImageView.image?.size.width)! {
            theLabel.frame.origin.x = (view.frame.size.width - (theImageView.image?.size.height)!)/2
            theLabel.frame.size.width = (theImageView.image?.size.height)!
        }
        else {
            theLabel.frame.origin.x = ((theImageView.image?.size.height)!/2) - 9
            theLabel.frame.size.width = rint(view.frame.size.width) - (theImageView.image?.size.height)! + 18
        }
    }

    if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
    {
        theLabel.frame.size.width = theImageView.frame.size.width
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // dispose of any resources that can be recreated.
}

override func viewWillTransitionToSize(size: CGSize,withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    //theImageView.frame.size.width = (theImageView.image?.size.width)!
    //theImageView.frame.size.height = (theImageView.image?.size.height)!
}

func rotated()
{
    if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
    {
        if (theImageView.image?.size.height)! > (theImageView.image?.size.width)! {
            theLabel.frame.origin.x = (view.frame.size.width - (theImageView.image?.size.height)!)/2
            theLabel.frame.size.width = (theImageView.image?.size.height)!
        }
        else {
            theLabel.frame.origin.x = ((theImageView.image?.size.height)!/2) - 9
            theLabel.frame.size.width = rint(view.frame.size.width) - (theImageView.image?.size.height)! + 18
        }
    }

    if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
    {
        theLabel.frame.size.width = theImageView.frame.size.width
    }


}

ios – Swift:从元组的数组中获取一个元素数组

ios – Swift:从元组的数组中获取一个元素数组

我有一个这样的元组数组:
var answers: [(number: Int,good: Bool)]

我想从它得到一个数字的成员数组.就像我做了一样的事情:

answers["number"] // -> Should give [Int] of all values named "number"

我没有找到任何类似的东西,也许这是不可能的,但这将是悲伤:(

解决方法

很简单:
answers.map { $0.number }

ios – 如何在Swift中搜索包含结构元素的数组?

ios – 如何在Swift中搜索包含结构元素的数组?

在String,Int等类型的数组中找到一个元素非常简单.
var States = ["CA","FL","MI"]
var filteredStates = States.filter {$0 == "FL"} // returns false,true,false

现在,我创建了一个结构体

struct Candy{
    let name:String
}

然后初始化它

var candies =  [Candy(name: "Chocolate"),Candy(name: "Lollipop"),Candy(name: "Caramel")]

任何人都可以建议在包含结构元素的数组中找到“巧克力”的正确方法?我无法实现查找或过滤方法.

解决方法

使用以下代码,您将收到数组中的所有糖果结构,它们与“巧克力”匹配.
var candiesFiltered = candies.filter{$0.name == "Chocolate"}

如果您只想要一个布尔值(如果已经找到),您可以使用以下代码:

var found = candies.filter{$0.name == "Chocolate"}.count > 0

关于如何在Swift中获取2数组的公共元素列表?如何在swift中获取2数组的公共元素列表数据的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于Algs4-1.4.12找出两个有序数组的公共元素-方法1、iOS |如何在swift中获取运行时图像的宽度/高度、ios – Swift:从元组的数组中获取一个元素数组、ios – 如何在Swift中搜索包含结构元素的数组?的相关信息,请在本站寻找。

本文标签: