mysql> select * from tb_user where phone >='17799990020';+----+--------+-------------+---------------------+------------+------+--------+--------+---------------------+| id | name | phone | email | profession | age | gender | status | createtime |+----+--------+-------------+---------------------+------------+------+--------+--------+---------------------+|21| 典韦 |17799990020| ycaunanjian@163.com | 城市规划 |52|1|2|2000-04-1200:00:00||22| 廉颇 |17799990021| lianpo321@126.com | 土木工程 |19|1|3|2002-07-1800:00:00||23| 后羿 |17799990022| altycj2000@139.com | 城市园林 |20|1|0|2002-03-1000:00:00||24| 姜子牙 |17799990023|37483844@qq.com | 工程造价 |29|1|4|2003-05-2600:00:00|+----+--------+-------------+---------------------+------------+------+--------+--------+---------------------+4 rows in set(0.00 sec)
mysql>
2.3、执行计划 phone >=‘17799990020’
mysql> explain select * from tb_user where phone >='17799990020';+----+-------------+---------+------------+-------+----------------+----------------+---------+------+------+----------+-----------------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+---------+------------+-------+----------------+----------------+---------+------+------+----------+-----------------------+|1| SIMPLE | tb_user |NULL| range | idx_user_phone | idx_user_phone |46|NULL|4|100.00| Using index condition |+----+-------------+---------+------------+-------+----------------+----------------+---------+------+------+----------+-----------------------+1 row in set,1warning(0.00 sec)
mysql>
此时走索引
2.4、执行计划 phone >=‘17799990000’
mysql> explain select * from tb_user where phone >='17799990000';+----+-------------+---------+------------+------+----------------+------+---------+------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+---------+------------+------+----------------+------+---------+------+------+----------+-------------+|1| SIMPLE | tb_user |NULL| ALL | idx_user_phone |NULL|NULL|NULL|24|100.00| Using where |+----+-------------+---------+------------+------+----------------+------+---------+------+------+----------+-------------+1 row in set,1warning(0.00 sec)
此时没有走索引,走的全表扫描
2.5、执行计划 phone >=‘17799990010’
mysql> explain select * from tb_user where phone >='17799990010';+----+-------------+---------+------------+------+----------------+------+---------+------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+---------+------------+------+----------------+------+---------+------+------+----------+-------------+|1| SIMPLE | tb_user |NULL| ALL | idx_user_phone |NULL|NULL|NULL|24|58.33| Using where |+----+-------------+---------+------------+------+----------------+------+---------+------+------+----------+-------------+1 row in set,1warning(0.00 sec)
mysql>
此时没有走索引,走的是全表扫描,因为表中的数据大部分满足phone >='17799990010'
2.6、执行计划 phone >=‘17799990013’
mysql> explain select * from tb_user where phone >='17799990013';+----+-------------+---------+------------+-------+----------------+----------------+---------+------+------+----------+-----------------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+---------+------------+-------+----------------+----------------+---------+------+------+----------+-----------------------+|1| SIMPLE | tb_user |NULL| range | idx_user_phone | idx_user_phone |46|NULL|11|100.00| Using index condition |+----+-------------+---------+------------+-------+----------------+----------------+---------+------+------+----------+-----------------------+1 row in set,1warning(0.00 sec)
mysql>
此时命中索引,因为表中的数据很少一部分满足phone >=‘17799990013’所以走索引更快
2.7、执行计划 profession is null
mysql> explain select * from tb_user where profession is null;+----+-------------+---------+------------+------+----------------------+----------------------+---------+-------+------+----------+-----------------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+---------+------------+------+----------------------+----------------------+---------+-------+------+----------+-----------------------+|1| SIMPLE | tb_user |NULL| ref | idx_user_pro_age_sta | idx_user_pro_age_sta |47|const|1|100.00| Using index condition |+----+-------------+---------+------------+------+----------------------+----------------------+---------+-------+------+----------+-----------------------+1 row in set,1warning(0.00 sec)
mysql>
此时命中索引,因为表中的数据都不是null,所以走索引更快
2.8、执行计划 profession is not null
mysql> explain select * from tb_user where profession is not null;+----+-------------+---------+------------+------+----------------------+------+---------+------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+---------+------------+------+----------------------+------+---------+------+------+----------+-------------+|1| SIMPLE | tb_user |NULL| ALL | idx_user_pro_age_sta |NULL|NULL|NULL|24|100.00| Using where |+----+-------------+---------+------------+------+----------------------+------+---------+------+------+----------+-------------+1 row in set,1warning(0.00 sec)
mysql>
mysql> explain select * from tb_user where profession is null;+----+-------------+---------+------------+------+----------------------+------+---------+------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+---------+------------+------+----------------------+------+---------+------+------+----------+-------------+|1| SIMPLE | tb_user |NULL| ALL | idx_user_pro_age_sta |NULL|NULL|NULL|24|100.00| Using where |+----+-------------+---------+------------+------+----------------------+------+---------+------+------+----------+-------------+1 row in set,1warning(0.00 sec)
mysql>
2.11、执行计划 profession is not null
mysql> explain select * from tb_user where profession is not null;+----+-------------+---------+------------+-------+----------------------+----------------------+---------+------+------+----------+-----------------------+| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |+----+-------------+---------+------------+-------+----------------------+----------------------+---------+------+------+----------+-----------------------+|1| SIMPLE | tb_user |NULL| range | idx_user_pro_age_sta | idx_user_pro_age_sta |47|NULL|1|100.00| Using index condition |+----+-------------+---------+------------+-------+----------------------+----------------------+---------+------+------+----------+-----------------------+1 row in set,1warning(0.00 sec)
mysql>
网址如下:
OpenJudge - 1012:Joseph 其中一个解法
只想到了一个快速找到下一位处决的人的方法,本质上还是遍历,暂时没想到更优的方法了
代码如下:
#include<cstdio>
int k;bool judge(int tt, int m, int r){if(tt k) …
LeetCode 是一个在线编程平台,它提供了大量的算法题目供用户练习。 TOP5题目通常指的是 LeetCode 网站上最受欢迎的前5道题目。 以下是 LeetCode TOP5 题目的列表以及它们常见的解题思路和代码示例。 题目1 两数之和
两数之和 - 1. Two Sum
Given an array of int…