62,621
社区成员
发帖
与我相关
我的任务
分享SELECT MAX(id) FROM t WHERE id >= 1300000 AND id < 1400000;SELECT MAX(id) FROM t WHERE id>=1300000 and id<1400000;
public class TestGetMax {
public static void main(String[] args) {
int[] arr = {1300220,1300221,1300224,1300700,1300400,1300200,1400001,1400023,1400035,1400021,1400043,1400075,1500023,1600012};
System.out.println(getBeginMax(arr,13,5));
}
/*
* begin:开始的数字
* s:除开头数字的位数
*/
public static int getBeginMax(int[] arr,int begin,int s){
int[] nums = arr;
int temp = 0;
int j = 0;
int ss = (int)Math.pow(10, s);
for(int i=0;i<nums.length;i++){
if(nums[i]/100000==13){
int n = nums[i]%ss;
if(n>j){
j = n;
temp = nums[i];
}
}
}
return temp;
}
}
mysql> select * from t;
+---------+
| id |
+---------+
| 1300220 |
| 1300221 |
| 1300224 |
| 1300700 |
| 1300400 |
| 1300200 |
| 1400001 |
| 1400023 |
| 1400035 |
| 1400021 |
| 1400043 |
| 1400075 |
| 1500032 |
| 1500023 |
| 1600012 |
+---------+
15 rows in set (0.00 sec)
mysql> select max(id) as mid from t
-> where floor(id/100000)=13;
+---------+
| mid |
+---------+
| 1300700 |
+---------+
1 row in set (0.00 sec)
int getMax(int[] ids) {
int max = 0;
for (int i = 0; i < ids.length; i++) {
if (ids[i] < 1300000 && ids[i] >= 1400000) {
continue;
}
if (ids[i] > max)
{
max = ids[i];
}
}
return max;
}
如果你那传过来 是数组的话,我给你写个 伪代码 这个效率 还可以
int getMax(int[] ids) {
int max = 0;
for (int i = 0; i < ids.length; i++) {
if (ids[i] < 1300000 && ids[i] >= 1400000) {
continue;
}
max = ids[i];
}
return max;
}
如果有等他也好办,
你 先做个判断 把 大于1300000 小于1400000先 存到你的 数组中
然后 对这个 做个 Arrays.sort排序下, 最后1个就是你要的了