背景:c嵌入式工程师,碎片时间通过刷题学习仓颉自用记录贴,菜勿喷
1、怎么声明数组
var arr:Array<Int64>=[0,0]
声明格式:
var/let name : type<subType> = initVal
var变量 let字面量
type一堆,文档写的也很乱,subType用在Type = Array/Range时,subType可以是任何type
整型 | 浮点 | 布尔 | 字符 | 字符串 | 元组 | 数组 | 区间 | Unit | nothing |
---|---|---|---|---|---|---|---|---|---|
Int8、Int16、Int32、Int64 和 IntNative,编码长度为 8-bit、16-bit、32-bit、64-bit 和平台相关大小的有符号整数值的类型。 UInt8、UInt16、UInt32、UInt64 和 UIntNative,编码长度为 8-bit、16-bit、32-bit、64-bit 和平台相关大小的无符号整数值的类型。 | Float16、 Float32 和 Float64,编码长度为 16-bit、 32-bit 和 64-bit 的浮点数的类型。Float16、 Float32 和 Float64 分别对应 IEEE 754 中的半精度格式(即 binary16)、单精度格式(即 binary32)和双精度格式(即 binary64) | Bool | Rune | String | 元组(Tuple)可以将多个不同的类型组合在一起,成为一个新的类型。元组类型使用 (T1, T2, …, TN) 表示,其中 T1 到 TN 可以是任意类型,不同类型间使用逗号(,)连接。元组至少是二元,例如,(Int64, Float64) 表示一个二元组类型,(Int64, Float64, String) 表示一个三元组类型 | 仓颉使用 Array 来表示 Array 类型。T 表示 Array 的元素类型,T 可以是任意类型 | Range 表示。当 T 被实例化不同的类型时(要求此类型必须支持关系操作符,并且可以和 Int64 类型的值做加法),会得到不同的区间类型,如最常用的 Range 用于表示整数区间。每个区间类型的实例都会包含 start、end 和 step 三个值。其中,start 和 end 分别表示序列的起始值和终止值,step 表示序列中前后两个元素之间的差值(即步长);start 和 end 的类型相同(即 T 被实例化的类型),step 类型是 Int64,并且它的值不能等于 0。 | 对于那些只关心副作用而不关心值的表达式,它们的类型是 Unit。例如,print 函数、赋值表达式、复合赋值表达式、自增和自减表达式、循环表达式,它们的类型都是 Unit。Unit 类型只有一个值,也是它的字面量:()。除了赋值、判等和判不等外,Unit 类型不支持其他操作。 | Nothing 是一种特殊的类型,它不包含任何值,并且 Nothing 类型是所有类型的子类型。break、continue、return 和 throw 表达式的类型是 Nothing,程序执行到这些表达式时,它们之后的代码将不会被执行。其中 break、continue 只能在循环体中使用,return 只能在函数体中使用。 |
官方文档
https://developer.huawei.com/consumer/cn/doc/cangjie-guides-V5/2_3_u57fa_u7840_u6570_u636e_u7c7b_u578b-V5
2、for循环遍历数组
for(_ in left…right) // 从left到right-1循环,左闭右开
for(a in 0..10){
print(a)
}
标准输出
0123456789
3、怎么判空
题目给出
/**
* Definition for singly-linked list.
* public class ListNode {
* public var val: Int64
* public var next: ?ListNode
* public init() {
* val = 0
* next = None
* }
* public init(val: Int64) {
* this.val = val
* next = None
* }
* }
*/
class Solution {
func addTwoNumbers(l1: ?ListNode, l2: ?ListNode): ?ListNode {
}
}
第一下想到的是弄一个ListNode的指针,但是不知道仓颉怎么写if(p == null)这个表达式
最后结论:
可以用?
运算符
吐槽:cangjie很多option的类型,导致不能直接赋值,很蠢
/**
* Definition for singly-linked list.
* public class ListNode {
* public var val: Int64
* public var next: ?ListNode
* public init() {
* val = 0
* next = None
* }
* public init(val: Int64) {
* this.val = val
* next = None
* }
* }
*/
class Solution {
func addTwoNumbers(l1: ?ListNode, l2: ?ListNode): ?ListNode {
var p = l1
var q = l2
var needPlus1 = false
var nextNeedPlus1 = false
var res:ListNode = ListNode(0)
var tmp:ListNode = res
while(true){
var needBreak = true
var num:Int64 = 0
if(p?.val != None){
needBreak = false
num += (p??throw Exception("222")).val
p = (p??break).next
}
if(q?.val != None){
needBreak = false
num += (q??throw Exception("333")).val
q = (q??break).next
}
if(nextNeedPlus1){
needPlus1 = true
nextNeedPlus1 = false
}
if(needPlus1){
needPlus1 = false
num += 1
}
if(num >= 10){
nextNeedPlus1 = true
num -= 10
}
if(needBreak){
if(num != 0){
tmp.next = ListNode(1)
tmp = (tmp.next??throw Exception("444"))
}
break
}
tmp.next = ListNode(num)
tmp = (tmp.next??throw Exception("555"))
}
return res.next
}
}
4、string的分量
是uint8哦不是rune
5、sortBy用法
myCuts.sortBy(stable: true){ rht: Int64, lht: Int64 =>
if (rht < lht) {
return Ordering.LT
}
if (rht > lht) {
return Ordering.GT
}
return Ordering.EQ
}
6、数组嵌套初始化
用循环初始化
适合定长且需要O1访问
or
改用ArrayList
动态变化,适合不定数据
7. 链表
8. lru cache
9. 深浅拷贝问题 && 10. ArrayList 的 insert 位置问题 && 11. Rune(i)使用问题
12、 string不可以slice
RT
13、collection类型 remove问题
如果用固定index当作迭代器移除,每次移除后,整个collection类型回对齐,导致迭代器指向改变。
14. 类型极值 && 类型强转
Int64.Max
Int32.Min
Int64(Int32.max)
Rune(Int64(r'a')+1) // = r'b'
...