当前位置:网站首页>开发中使用的语言技巧
开发中使用的语言技巧
2022-06-12 13:26:00 【Li.CQ】
- result 的值变化范围是 0 - valueRange / 2, valueRange / 2 - 0, 0 - valueRange / 2, valueRange / 2 - 0;
static int rote = 0;
int valueRange = 20;
int maxValue = valueRange / 2;
int a = rote % valueRange;
int b = rote / maxValue;
int c = b % 2;
int result = (valueRange * c + a * (pow(-1, c)));
rote++;
- result 的值变化范围是 0 - valueRange, 0 - valueRange, 0 - valueRange, 0 - valueRange;
static int rote = 0;
int valueRange = 20;
int result = (rote % valueRange );
rote++;
- scorllView的子view的布局
NSArray <UIView *> * viewsArray;
UIScrollView *scrollView;
__block UIView *lastView = scrollView;
[viewsArray enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[obj mas_makeConstraints:^(MASConstraintMaker *make) {
MASViewAttribute * mas_ori = (idx == 0) ? lastView.mas_left : lastView.mas_right;
make.left.equalTo(mas_ori).offset(5);
make.top.bottom.equalTo(scrollView);
if (idx == viewsArray.count - 1) make.right.equalTo(scrollView);
}];
lastView = obj;
}];
// - 返回 大于 wid 的 最小的16的倍数
#define aw_stride(wid) ((wid % 16 != 0) ? ((wid) + 16 - (wid) % 16): (wid))
空判断
// - 判断v1是否等于v2前需要判断v1和v2是否为空.
private boolean valEquals(v1, v2){
return v1 == null ? v2 == null : v1.equals(v2);
}
// - 判断当前的节点是不是左子节点.
boolean left = parent.left == null || node.isLeftChild();
字符串计算hash值
- jack由四个字符组成
- j * n^3 + a * n^2 + c * n^1 + j * n^1; 即: [(j * n + a) * n +c] * n + k j, a, c, k 都有自己的ascaii码, 可以计算出一个整数值
- 在JDK中, n = 31;
- 31是一个奇素数, 31 * i = (( i << 5 ) - i ); 这样 再次进行替换, 就可以提高计算速度.
- JDK中计算hash值
- 可以使用 1 << 4 表示2^4.
String string = "jack";
int len = string.length();
int hashCode = 0;
for (int i = 0; i< len; i++){
char c = string.cahrAt(i);
hashCode = hashCode * 31 + c;
// - 等价于: hashCode = (hashCode << 5) - hashCode + c;
}
a++和 ++a
a++: 先用后加 是先使用a的值,然后再对a做加1处理;
int a = 0;
for (int i = 0; i < 10000; i++){
[array addObject:@(i)];
}
array[a++] = @"abc";
// - 这里结果 array[0] == @"abc", a = 1
++a: 先加后用 是先对a作加1处理,然后再使用a的值。
int a = 0;
for (int i = 0; i < 10000; i++){
[array addObject:@(i)];
}
array[++a] = @"abc";
// - 这里结果 array[1] == @"abc", a = 1
使用表达式做为下标
// - 此时不但改变了a的值, 并且取到了数组的下标为 4 的元素.
int a = 0;
[self.settingModelArr objectAtIndex:(a = 4)];
使用表达式做为判断条件
// - 此时不但改变了a的值, 并且取到了数组的下标为 4 的元素.
while ((node = node.parent) != null) {}
左右交替的情况(先从后到前遍历, 如果不满足条件, 就从前到后遍历, 如果又不满足条件了, 又从后到前遍历, 然后如果又不满足条件在从前到后遍历, 一直循环…)
while (begin < end) {
while (begin < end) {
if (cmp(pivot, array[end]) < 0) { // 右边元素 > 轴点元素
end--;
} else { // 右边元素 <= 轴点元素
array[begin++] = array[end];
break;
}
}
while (begin < end) {
if (cmp(pivot, array[begin]) > 0) { // 左边元素 < 轴点元素
begin++;
} else { // 左边元素 >= 轴点元素
array[end--] = array[begin];
break;
}
}
}
跳出for循环的方式
// - 1. 这个方式退出for 循环 , 很方便
for (NSUInteger i = 0 ; i < 10; i++) {
for (NSUInteger j = 0 ; j < 10; j++) {
if (j == 3 && i == 2) {
goto exitLoop;
}
NSLog(@"i : %lu, j : %lu", (unsigned long)i, j);
}
}
exitLoop:
NSLog(@"exitLoop");
// - 2. 这个方式退出for 循环 , 很方便
bool flag = true;
for (int i = 0; i < 5 && flag == true; i++){
for (int j = 0; j < 5 && flag == true; j++) {
if (i == 3 && j == 5)
flag = false;
}
}
1 和 0 之间的相互转换
// - 1. a = 0, result = 1, a = 1, result = 0;
int result = 1 - a;
// - 2. iOS特性
int result = ! a;
// -
记录a的变化
__block NSInteger row = -1;
// - 先找第一个未完成的任务, 如果没有找到就找第一个待完成或去完成的任务, 如果都没有选中第一个宝箱
[self.userTaskModel.list enumerateObjectsUsingBlock:^(QIEUserRewardTaskItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj.status == QIERewardGetStatusWaitingGet) {
row = idx;
*stop = YES;
}
if (((obj.status == QIERewardGetStatusWaitingDone) || (obj.status == QIERewardGetStatusGoToDone)) && (row == -1)) {
row = idx;
}
}];
// - 记录result的方式
1. int result = MAX(0, row);
2. int result = row >0 ? row : row++;
3. int result = 0; if(result > 0) result = row;
4. 先定义 __block NSInteger row = self.userTaskModel.list.count, 然后在结尾位置 int result = row % self.userTaskModel.list.count;
5. int result = (abs(row) + row) / 2;
// - 选中指定的view
NSIndexPath *idx = [NSIndexPath indexPathForRow:result inSection:0];
[self collectionView:self.boxCollectionView didSelectItemAtIndexPath:idx];
对2取余的本质
// - 对2取余本质就是看这个数组的二进制结果的最低位是0还是1
/*
6 % 2 = 0b160 & 0b001 = 0;
5 % 2 = 0b101 & 0b001 = 1;
4 % 2 = 0b100 & 0b001 = 0;
3 % 2 = 0b011 & 0b001 = 1;
2 % 2 = 0b010 & 0b001 = 0;
1 % 2 = 0b001 & 0b001 = 1;
*/
int a = 100;
a % 2 等价于 a & 1;
两次for循环, 记录上次值得变化
NSArray *arr1, *arr2, *dp;
for (int i = 1; i <= arr1.count; i++) {
int cur = 0;
for (int j = 1; j <= arr2.count; j++) {
int leftTop = cur;
cur = dp[j];
if (arr1[i - 1] == arr2[j - 1]) {
dp[j] = leftTop + 1;
} else {
dp[j] = MAX(dp[j], dp[j - 1]);
}
}
}
使用do while 实现 goto的功能
-(NSString *)test{
NSInteger errorCode = 0;
do {
if (0) {
errorCode = OKWTransactionErrorPwdEmpty;
break;
}
if (0) {
errorCode = OKWTransactionErrorTxDataEmpty;
break;
}
if (0) {
errorCode = OKWTransactionErrorDecodePwdEmpty;
break;
}
if (1) {
return @"成功"
} while (0);
return @"失败"
}
边栏推荐
猜你喜欢

用PyTorch进行语义分割

Hardware composition of embedded system - introduction of embedded development board based on ARM

How to balance multiple losses in deep learning?

Openstack network

torch_geometric mini batch 的那些事

多源BFS问题 模板(附题)

There was an error installing mysql. Follow the link below to CMD

Implementing tensorflow deep learning framework similarflow with numpy

How to solve the problem of data table query error when SQLite writes the registration function?

Experience and learning path of introductory deep learning and machine learning
随机推荐
Successfully rated Tencent t3-2, 10000 word parsing
1004: character triangle
Application of short circuit expression (||) in C language
verilog-mode的简要介绍
2061: [example 1.2] trapezoidal area
1001:Hello,World
【云原生 | Kubernetes篇】Kubernetes 网络策略(NetworkPolicy)
[EDA] chip layout design: VLSI layout design using electric
Chaotic engineering practice of distributed kV storage in station B
The problem of Joseph in Informatics
torch_ geometric message passing network
移动应用出海的“新大陆”
Semantic segmentation with pytorch
import torch_geometric 的Data 查看
import torch_ Data view of geometric
在 Debian 10 上独立安装MySQL数据库
2067: [example 2.5] circle
C#DBHelper_ FactoryDB_ GetConn
import torch_geometric 第一个图网络例子
Stm32f1 and stm32cubeide programming examples - device driver -eeprom-at24c256 driver