GVKun编程网logo

[Swift]LeetCode611. 有效三角形的个数 | Valid Triangle Number(三角形有效值)

6

在本文中,您将会了解到关于[Swift]LeetCode611.有效三角形的个数|ValidTriangleNumber的新资讯,同时我们还将为您解释三角形有效值的相关在本文中,我们将带你探索[Swi

在本文中,您将会了解到关于[Swift]LeetCode611. 有效三角形的个数 | Valid Triangle Number的新资讯,同时我们还将为您解释三角形有效值的相关在本文中,我们将带你探索[Swift]LeetCode611. 有效三角形的个数 | Valid Triangle Number的奥秘,分析三角形有效值的特点,并给出一些关于65. Valid Number、Android开发 Error:The number of method references in a .dex file cannot exceed 64K.Android开发 Error:The number of method references in a .dex file cannot exceed 64K、Angular 12 - 不可分配给类型 '[number, number]、Angular 2 typescript d3类型问题:'[number,number]’类型中不存在属性’x’的实用技巧。

本文目录一览:

[Swift]LeetCode611. 有效三角形的个数 | Valid Triangle Number(三角形有效值)

[Swift]LeetCode611. 有效三角形的个数 | Valid Triangle Number(三角形有效值)

Given an array consists of non-negative integers,your task is to count the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle.

Example 1:

Input: [2,2,3,4]
Output: 3
Explanation:
Valid combinations are: 
2,4 (using the first 2)
2,4 (using the second 2)
2,3 

Note:

  1. The length of the given array won‘t exceed 1000.
  2. The integers in the given array are in the range of [0,1000].

给定一个包含非负整数的数组,你的任务是统计其中可以组成三角形三条边的三元组个数。

示例 1:

输入: [2,4]
输出: 3
解释:
有效的组合是: 
2,4 (使用第一个 2)
2,4 (使用第二个 2)
2,3

注意:

  1. 数组长度不超过1000。
  2. 数组里整数的范围为 [0,1000]。
Runtime: 36 ms
Memory Usage: 19.4 MB
 1 class Solution {
 2     func triangleNumber(_ nums: [Int]) -> Int {
 3         var nums = nums
 4         var res:Int = 0
 5         var n:Int  = nums.count
 6         nums.sort()
 7         for i in stride(from:n - 1,through:2,by:-1)
 8         {
 9             var left:Int = 0
10             var right:Int = i - 1
11             while (left < right)
12             {
13                 if nums[left] + nums[right] > nums[i]
14                 {
15                     res += (right - left)
16                     right -= 1
17                 }
18                 else
19                 {
20                     left += 1
21                 }
22             }
23         }
24         return res
25     }
26 }

76ms

 1 class Solution {
 2     func triangleNumber(_ nums: [Int]) -> Int {
 3         var result = 0
 4         guard nums.count >= 3 else {
 5             return 0
 6         }
 7         var nums = nums.sorted()
 8         for i in 0...nums.count - 2 {
 9             let longest = nums[i]
10             var fast = i + 2
11             var slow = i + 1
12             while fast < nums.count {
13                 while slow < fast && nums[i] + nums[slow] <= nums[fast] {
14                     slow += 1
15                 }
16                 result += fast - slow
17                 fast += 1
18             }
19         }
20         return result
21     }
22 }

124ms

 1 class Solution {
 2     func triangleNumber(_ nums: [Int]) -> Int {
 3         guard nums.count >= 3 else { return 0 }
 4         let sortednums = nums.sorted {
 5             return $0 < $1
 6         }
 7         var count = 0
 8         let last = sortednums.count - 1
 9         print(sortednums)
10         for i in 0..<last {
11             if sortednums[i] == 0 {
12                 continue
13             }
14             var k = i + 1
15             for j in i+1...last {
16                 if sortednums[j] == 0 {
17                     continue
18                 }
19                 let sum = sortednums[i] + sortednums[j]
20                 while k < sortednums.count && sortednums[k] < sum {
21                     k += 1
22                 }
23                 count += (k - j - 1)
24             }
25         }
26         return count
27     }
28 }

128ms

 1 class Solution {
 2     func triangleNumber(_ nums: [Int]) -> Int {
 3         let n = nums.sorted()
 4         var ans = 0
 5         for i in 0..<n.count {
 6             var k = i + 1
 7             for j in i + 1..<n.count {
 8                 while k + 1 < n.count && n[k + 1] < n[i] + n[j] {
 9                     k += 1
10                 }
11                 if n[k] < n[i] + n[j] {
12                     ans += max(0,k - j)
13                 }
14             }
15         }
16         return ans
17     }
18 }

232ms

 1 class Solution {
 2     // return the last index of a biggest number <= target
 3     func binarySearch(_ nums: [Int],_ target: Int,_ start: Int,_ end: Int) -> Int {
 4         var i = start,j = end
 5         while i < j {
 6             let m = (i + j) / 2
 7             if nums[m] > target { j = m }
 8             else { i = m + 1 }
 9         }
10         return i-1
11     }
12     
13    
14     func triangleNumber(_ nums: [Int]) -> Int {
15         guard nums.count > 2 else { return 0 }
16         let nums = nums.sorted()
17         var res = 0
18         for i in 0..<(nums.count - 2) {
19             for j in i+1..<(nums.count - 1) {
20                 res += binarySearch(nums,nums[i] + nums[j] - 1,j+1,nums.count) - j
21             }
22         }
23         return res
24     }
25 }

248ms

 1 class Solution {
 2     func triangleNumber(_ nums: [Int]) -> Int {
 3         if nums.count < 3 { return 0 }
 4         var res = 0
 5         let nums = nums.sorted()
 6         for i in 1..<nums.count - 1 {
 7             var k = nums.count - 1
 8             for j in (0..<i).reversed() {
 9                 let s = nums[j] + nums[i]
10                 while k > i && nums[k] >= s { k -= 1 }
11                 res += k - i
12             }
13         }
14         return res
15     }
16 }

1180ms、18948kb

 1 class Solution {
 2     func triangleNumber(_ nums: [Int]) -> Int {
 3     if nums.count < 3 {
 4       return 0
 5     }
 6     var count = 0
 7     let nums_sorted = nums.sorted()
 8     let maxnum = nums_sorted.last!
 9     for i in 0..<nums_sorted.count {
10      
11       for j in i+1..<nums_sorted.count {
12         let side1 = nums_sorted[i]
13         let side2 = nums_sorted[j]
14         var numOfValidThridNums = 0
15         for k in j+1..<nums_sorted.count {    
16           if nums_sorted[k] < side1 + side2 {
17             numOfValidThridNums = numOfValidThridNums + 1
18           } else {
19             break;
20           }
21         }
22         count = count + numOfValidThridNums
23       }
24     }
25     return count
26   }
27 }

1584ms 

 1 class Solution {
 2     func triangleNumber(_ nums: [Int]) -> Int {
 3         
 4         var n = 0
 5         let nums = nums.sorted()
 6         for i in 0..<nums.count {
 7             for j in i + 1..<nums.count {
 8                 for k in j + 1..<nums.count {
 9                     if nums[k] < nums[i] + nums[j] {
10                         n += 1
11                     } else {
12                         break
13                     }
14                 }
15             }
16         }
17         return n
18     }
19 }

2332ms

 1 class Solution {
 2     func triangleNumber(_ nums: [Int]) -> Int {
 3         let ct = nums.count
 4         if ct < 3 {
 5             return 0
 6         }
 7         var nums = nums.sorted { $0 > $1 }
 8         var ans = 0
 9         for i in 0..<ct-2 {
10             for j in (i+1)..<ct-1 {
11                 for k in (j+1)..<ct {
12                     if vailidTriangle(nums[i],nums[j],nums[k]) {
13                         ans += 1
14                     }else{
15                         break
16                     }
17                 }
18             }
19         }
20         return ans
21     }
22 
23     func vailidTriangle(_ a: Int,_ b: Int,_ c: Int) -> Bool {
24         return (b+c>a)
25     }
26 }

65. Valid Number

65. Valid Number

Validate if a given string is numeric.

Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true

判断字符串是否代表了有效数字。

这道题有点坑,情况比较多……

 1 class Solution {
 2 public:
 3     bool isNumber(string s) {
 4         int i = 0;
 5         int len = s.length() - 1;
 6         bool flag_p = false;
 7         bool flag_e = false;
 8         bool flag_n = false;
 9         bool flag_f = false;
10         //跳过首尾的空格
11         while (i < len && s[i] == '' '')
12             ++i;
13         while (len > i && s[len] == '' '')
14             --len;
15         //必须以数字或小数点结尾
16         if (len >= 0 && !(s[len] >= ''0'' && s[len] <= ''9'' || s[len] == ''.''))
17             return false;
18         while (i <= len) {
19              if (s[i] >= ''0'' && s[i] <= ''9'') {
20                  ++i;
21                  flag_n = true;
22              }   
23             //小数点只能出现一次,并且不能作为指数
24              else if (s[i] == ''.'') {
25                  if (!flag_p && !flag_e) {
26                      flag_p = true;
27                      ++i;
28                  }
29                  else
30                      return false;
31             }
32             //e只能出现一次,且之前必须有数字
33             else if (s[i] == ''e'') {
34                 if (!flag_e && flag_n) {
35                      flag_e = true;
36                      ++i;
37                  }
38                  else
39                      return false;
40             }
41             //正负号既可以作为底数的符号,也可以作为指数的。
42             //底数符号:只能第一个出现。前面不能有其他符号或数字
43             //指数符号:必须紧跟e之后
44             else if (s[i] == ''+'' || s[i] == ''-'') {
45                 if ((!flag_n && !flag_f && !flag_p) || (flag_e && s[i-1] == ''e'' )) {
46                     flag_f = true;
47                     ++i;
48                 }
49                 else
50                     return false;
51             }
52             //不允许出现其他字符
53             else
54                 return false;
55         }
56         //必须要有数字
57         return flag_n;
58     }
59 };

 

Android开发 Error:The number of method references in a .dex file cannot exceed 64K.Android开发 Error:The number of method references in a .dex file cannot exceed 64K

Android开发 Error:The number of method references in a .dex file cannot exceed 64K.Android开发 Error:The number of method references in a .dex file cannot exceed 64K

前言

错误起因:

在Android系统中,一个App的所有代码都在一个Dex文件里面。

Dex是一个类似Jar的存储了多有Java编译字节码的归档文件。

因为Android系统使用Dalvik虚拟机,所以需要把使用Java Compiler编译之后的class文件转换成Dalvik能够执行的class文件。

这里需要强调的是,Dex和Jar一样是一个归档文件,里面仍然是Java代码对应的字节码文件。当Android系统启动一个应用的时候,有一步是对Dex进行优化,这个过程有一个专门的工具来处理,叫DexOpt。DexOpt的执行过程是在第一次加载Dex文件的时候执行的。

这个过程会生成一个ODEX文件,即Optimised Dex。执行ODex的效率会比直接执行Dex文件的效率要高很多。 但是在早期的Android系统中,DexOpt的Linearalloc存在着限制: Android 2.2和2.3的缓冲区只有5MB,Android 4.x提高到了8MB或16MB。当方法数量过

多导致超出缓冲区大小时,会造成dexopt崩溃,导致无法安装.

另外由于DEX文件格式限制,一个DEX文件中method个数采用使用原生类型short来索引文件中的方法,也就是4个字节共计最多表达65536个method,field/class的个数也均有此限制。对于DEX文件,则是将工程所需全部class文件合并且压缩到一个DEX文件期间,也就

是Android打包的DEX过程中, 单个DEX文件可被引用的方法总数被限制为65536(自己开发以及所引用的Android Framework和第三方类库的代码).

解决办法

可以引入: compile 'com.android.support:multidex:1.+' 修改Application extends MultiDexApplication; 最后在build.gradle defaultConfig中加入:

defaultConfig {
multiDexEnabled true}

以为到这里就是解决方案? 其实这样做事不好的,为什么呢? 引用这种方式的原理在于:生成的Apk中将包含多个dex文件。

而且一次性加载这么多方法及其影响性能,两种方式才好解决:1.插件化开发 2.分包。

Angular 12 - 不可分配给类型 '[number, number]

Angular 12 - 不可分配给类型 '[number, number]

如何解决Angular 12 - 不可分配给类型 ''[number, number]?

我正在 Angular 12 中试用 ngx-charts 库,但我一直遇到这个问题:

类型 ''any[]'' 不能分配给类型 ''[number,number]''。

目标需要 2 个元素,但源可能更少。

我不知道错误是什么意思。

代码如下:

HTML:

<ngx-charts-linear-gauge 
  [view]="view" 
  [scheme]="colorScheme" 
  [value]="value" 
  [prevIoUsValue]="prevIoUsValue" 
  (select)="onSelect($event)" 
  [units]="units"
>
</ngx-charts-linear-gauge>

ts:

import { Component } from ''@angular/core'';

@Component({
  selector: ''app-root'',templateUrl: ''./app.component.html'',styleUrls: [''./app.component.scss'']
})
export class AppComponent {

  single: any[];
  view: any[] = [400,400];
  colorScheme = {
    domain: [''#aae3f5'']
  };
  value: number = 43;
  prevIoUsValue: number = 70;
  units: string = ''counts'';

  onSelect(event) {
    console.log(event);
  }

}

关于错误意味着什么的任何想法,如何解决这个问题?

ts 配置代码是:

{
  "compileOnSave": false,"compilerOptions": {
    "baseUrl": "./","outDir": "./dist/out-tsc","forceConsistentCasingInFileNames": true,"strict": false,"noImplicitReturns": true,"noFallthroughCasesInSwitch": true,"sourceMap": true,"declaration": false,"downlevelIteration": true,"experimentalDecorators": true,"moduleResolution": "node","importHelpers": true,"target": "es2017","module": "ES2020","lib": [
      "es2018","dom"
    ]
  },"angularCompilerOptions": {
    "enableI18nLegacyMessageIdFormat": false,"strictInjectionParameters": true,"strictInputAccessModifiers": true,"strictTemplates": true
  }
}

解决方法

我想问题在于您的 view 属性类型。根据鳕鱼,它必须是 number[] 类型:doc-ref
注意:文档中的类型不够准确。 view 不是定义为数字数组 (number[]),而是定义为 2 个数字的元组 ([number,number]) - source-code ref

但是您将其定义为 any[]
根据您的打字稿编译器设置,您可能会收到错误或警告。

当您需要完全正确的类型时,请使用:

// use as const so that typescript will infer the type [number,number] instead of number[]
view = [400,400] as const;

显式类型为:

view: [number,number] = [400,400];

Angular 2 typescript d3类型问题:'[number,number]’类型中不存在属性’x’

Angular 2 typescript d3类型问题:'[number,number]’类型中不存在属性’x’

我正在用d3生成折线图.它工作,但打字稿代码抱怨在一个不存在的属性

Property ‘x’ does not exist on type ‘[number,number]’

enter image description here

看着错误.看起来预期的数据点是一个带有两个数字的数组.

但我正在传递一个对象. D3应该支持我认为.

有谁知道如何摆脱这个错误而不改变我的数据?

解决方法

这是解决方案.我需要使用泛型:

enter image description here

enter image description here

我们今天的关于[Swift]LeetCode611. 有效三角形的个数 | Valid Triangle Number三角形有效值的分享已经告一段落,感谢您的关注,如果您想了解更多关于65. Valid Number、Android开发 Error:The number of method references in a .dex file cannot exceed 64K.Android开发 Error:The number of method references in a .dex file cannot exceed 64K、Angular 12 - 不可分配给类型 '[number, number]、Angular 2 typescript d3类型问题:'[number,number]’类型中不存在属性’x’的相关信息,请在本站查询。

本文标签: