对于如何在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数组的公共元素列表数据)
- Algs4-1.4.12找出两个有序数组的公共元素-方法1
- iOS |如何在swift中获取运行时图像的宽度/高度
- ios – Swift:从元组的数组中获取一个元素数组
- ios – 如何在Swift中搜索包含结构元素的数组?
如何在Swift中获取2数组的公共元素列表?(如何在swift中获取2数组的公共元素列表数据)
我有两个数组:
fruitsArray = ["apple", "mango", "blueberry", "orange"]vegArray = ["tomato", "potato", "mango", "blueberry"]
我如何获得这两个数组中的常见项目列表
ouptput = ["mango", "blueberry"]
我无法使用,if contains(array, string)
因为我想比较2个数组。
答案1
小编典典您还可以结合使用filter
和contains
:
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)
Set
与Array
仅计算共同元素相比
我们考虑以下代码片段:
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)
我用Int
short和long String
s(10到100Character
s)(全部随机生成)做了一些(人工)基准测试。我总是用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)
。然而从转换Array
到Set
和背部是这解释了增加大数据非常昂贵的`critical
elements`更大的数据类型。
结论
对于Array
具有约100个元素的小s,该Array
方法很好,但对于较大的s,则应使用该Set
方法。
如果您想多次使用此“常见元素”运算,则建议 仅* 在可能的情况下使用Set
s (元素的类型必须为)。 *Hashable
结束语
从Array
到Set
的转换比较昂贵,而从Set
到的转换Array
则非常便宜。
filter
与with一起使用比在以下.filter(array1.contains)
情况下性能更快.filter{array1.contains($0) }
:
- 最后一个创建一个新的闭包( 仅一次 ),而第一个仅传递一个函数指针
- 对于最后一个封闭件的调用创建花费空间和时间的附加堆栈帧( 多次 :
O(N)
)
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中获取运行时图像的宽度/高度
当我使用imageview.frame.size.width时,它会按预期返回整个屏幕宽度.当我使用image.size.width时,它返回固定的图像宽度(实际图像宽度).但我的目的是获取UI中显示的运行时图像宽度.
如何从源头找到它.
编辑1
当前的肖像画面,中间有标签
当前景观屏幕中间有标签.标签宽度与imageview相同,但不是当前显示的图像宽度.
解决方法
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:从元组的数组中获取一个元素数组
var answers: [(number: Int,good: Bool)]
我想从它得到一个数字的成员数组.就像我做了一样的事情:
answers["number"] // -> Should give [Int] of all values named "number"
我没有找到任何类似的东西,也许这是不可能的,但这将是悲伤:(
解决方法
answers.map { $0.number }
ios – 如何在Swift中搜索包含结构元素的数组?
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中搜索包含结构元素的数组?的相关信息,请在本站寻找。
本文标签: