• 每日练习-9


    目录

    1、井字棋

    2、密码强度等级

    3、二维数组中的查找

    4.调整数组奇数偶数

    5.旋转数组中的最小元素

    6、替换空格


    1、井字棋

    解析:井字棋有四种情况表示当前玩家获胜,行全为1, 列全为1,主对角全为1,  副对角全为1。遍历board的每一行,每一列,每一条对角线,检查是否有连成一排的的棋子,如果有,返回True。如果遍历完没有找到连成一排的棋子,返回False。

    代码如下:

    1. class Board {
    2. public:
    3. bool checkWon(vectorint> > board) {
    4. int sum = 0;
    5. int row = board.size();
    6. for (int i = 0; i < row; i++) {
    7. //先看每一行
    8. sum = 0;
    9. for (int j = 0; j < row; j++) {
    10. sum += board[i][j];
    11. if (sum == row) {
    12. return true;
    13. }
    14. }
    15. }
    16. for (int j = 0; j < row; j++) {
    17. //再看每一列
    18. sum = 0;
    19. for (int i = 0; i < row; i++) {
    20. sum += board[j][i];
    21. if (sum == row) {
    22. return true;
    23. }
    24. }
    25. }
    26. //检查主对角线的和是是否等于row
    27. sum = 0;
    28. for (int i = 0; i < row; i++) {
    29. sum += board[i][i];
    30. }
    31. if (sum == row)
    32. return true;
    33. //检查副对角线的和是是否等于row
    34. sum = 0;
    35. for (int i = 0; i < row; i++) {
    36. sum += board[i][row - i - 1];
    37. }
    38. if (sum == row)
    39. return true;
    40. return false;
    41. }
    42. // write code here
    43. };

    2、密码强度等级

    解析:这一道题思路很简单,由于不同种类的字符得分不同,同可以对每一个维度进行单独判断,即对于长度,字母,数字,符号单独判断,最后把所有的单项值根据题目要求相加,输出对应的安全级别。

    代码如下:

    1. #include
    2. #include
    3. using namespace std;
    4. int score_count(string& str) {
    5. int digit = 0, symbol = 0;
    6. int lower = 0, upper = 0, charc = 0;
    7. int size = 0, sum = 0;
    8. for (auto ch : str) {
    9. if (ch >= 'a' && ch <= 'z') {
    10. lower++;
    11. charc++;
    12. } else if (ch >= 'A' && ch <= 'Z') {
    13. upper++;
    14. charc++;
    15. } else if (ch >= '0' && ch <= '9') {
    16. digit++;
    17. } else if ((ch >= 0x21 && ch <= 0x2F) ||
    18. (ch >= 0x3A && ch <= 0x40) ||
    19. (ch >= 0x5B && ch <= 0x60) ||
    20. (ch >= 0x7B && ch <= 0x7E)) {
    21. symbol++;
    22. }
    23. }
    24. size = str.size();
    25. if (size <= 4)
    26. sum += 5;
    27. else if (size <= 7)
    28. sum += 10;
    29. else
    30. sum += 25;
    31. if (lower > 0 && upper > 0)
    32. sum += 20;
    33. else if (lower == charc || upper == charc)
    34. sum += 10;
    35. if (digit == 1)
    36. sum += 10;
    37. else if (digit > 1)
    38. sum += 20;
    39. if (symbol == 1)
    40. sum += 10;
    41. else if (symbol > 1)
    42. sum += 25;
    43. if (lower > 0 && upper > 0 && digit > 0 && symbol > 0)
    44. sum += 5;
    45. else if ((lower > 0 || upper > 0) && digit > 0 && symbol > 0)
    46. sum += 3;
    47. else if ((lower > 0 || upper > 0) && digit > 0 && symbol == 0)
    48. sum += 2;
    49. return sum;
    50. }
    51. int main() {
    52. string str;
    53. while (cin >> str) {
    54. int score = score_count(str);
    55. if (score >= 90) {
    56. cout << "VERY_SECURE" << endl;
    57. } else if (score >= 80) {
    58. cout << "SECURE" << endl;
    59. } else if (score >= 70) {
    60. cout << "VERY_STRONG" << endl;
    61. } else if (score >= 60) {
    62. cout << "STRONG" << endl;
    63. } else if (score >= 50) {
    64. cout << "AVERAGE" << endl;
    65. } else if (score >= 25) {
    66. cout << "WEAK" << endl;
    67. } else {
    68. cout << "VERY_WEAK" << endl;
    69. }
    70. }
    71. return 0;
    72. }

    3、二维数组中的查找

    解析:这是一个经典的算法问题,一种常见的方法是从二维数组的右上角或左下角开始,根据目标整数和当前元素的大小关系,逐步缩小查找范围,直到找到目标整数或者超出数组边界。这种方法的时间复杂度是O(m+n),其中m和n分别是二维数组的行数和列数。

    代码如下:

    1. class Solution {
    2. public:
    3. bool Find(int target, vectorint> >& array)
    4. {
    5. int row = array.size();
    6. int col = array[0].size();
    7. int i = 0;
    8. int j = col - 1;
    9. while (i < row && j > -1)
    10. {
    11. if (array[i][j] == target)
    12. {
    13. return true;
    14. } else if (array[i][j] > target)
    15. {
    16. j--;
    17. } else if (array[i][j] < target)
    18. {
    19. i++;
    20. }
    21. }
    22. return false;
    23. }
    24. };

    4.调整数组奇数偶数

    解析:这是一个数组的奇偶数分割问题,可以用双指针的方法来实现。双指针的方法是用两个变量分别指向数组的头部和尾部,然后同时向中间移动,每次移动时判断当前指向的元素是奇数还是偶数,如果是奇数就保持不变,如果是偶数就和另一个指针指向的元素交换位置,直到两个指针相遇或者交错为止。

    代码如下:

    1. class Solution {
    2. public:
    3. vector<int> reOrderArrayTwo(vector<int>& array)
    4. {
    5. int left=0;
    6. int right=array.size()-1;
    7. int term=0;
    8. while(left
    9. {
    10. while(array[left]%2==1)
    11. {
    12. left++;
    13. }
    14. while(array[right]%2==0)
    15. {
    16. right--;
    17. }
    18. if(left
    19. {
    20. term=array[left];
    21. array[left]=array[right];
    22. array[right]=term;
    23. }
    24. }
    25. return array;
    26. }
    27. };

    5.旋转数组中的最小元素

    解析:旋转数组中的最小元素是一个常见的算法问题,可以用二分法来解决。二分法的思路是用两个指针分别指向数组的首尾,然后根据中间元素和右指针指向的元素的大小关系,缩小查找范围,直到找到最小元素或者两个指针相遇。

    代码如下:

    1. #include
    2. #include
    3. using namespace std;
    4. int main() {
    5. vector<int> arr;
    6. int n;
    7. while(cin>>n)
    8. {
    9. arr.push_back(n);
    10. }
    11. int left=0;
    12. int right=arr.size()-1;
    13. while(left
    14. {
    15. int mid=left+(right-left)/2;
    16. if(arr[mid]>arr[right])
    17. {
    18. left=mid+1;
    19. }
    20. else if(arr[mid]
    21. {
    22. right=mid;
    23. }
    24. else right=right-1;
    25. }
    26. cout<
    27. return 0;
    28. }

    6、替换空格

    解析:

    首先,我们需要遍历字符串s,找出其中有多少个空格,这样我们就可以计算出替换后的字符串长度。然后,我们需要从后往前修改字符串s,使用两个指针i和j,分别指向原字符串和新字符串的末尾。接下来,我们需要判断s[i]是否是空格,如果不是,就直接复制到s[j],然后i和j都向前移动一位;如果是,就在s[j]处依次填入’0’,‘2’和’%',然后i向前移动一位,j向前移动三位。最后,我们需要重复上述步骤,直到i和j相等为止,这样就完成了空格的替换。

    代码如下:

    1. class Solution {
    2. public:
    3. /**
    4. * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
    5. *
    6. *
    7. * @param s string字符串
    8. * @return string字符串
    9. */
    10. string replaceSpace(string s)
    11. {
    12. int count = 0;
    13. for (char c : s) {
    14. if (c == ' ') count++;
    15. }
    16. // 如果没有空格,直接返回
    17. if (count == 0) return s;
    18. // 计算替换后的字符串长度
    19. int len = s.length();
    20. int newLen = len + count * 2;
    21. // 从后往前修改字符串s,使用两个指针i和j
    22. int i = len - 1;
    23. int j = newLen - 1;
    24. // 扩充字符串s的容量
    25. s.resize(newLen);
    26. // 当i和j不相等时,循环执行
    27. while (i != j)
    28. {
    29. // 如果s[i]不是空格,就复制到s[j],然后i和j都向前移动一位
    30. if (s[i] != ' ')
    31. {
    32. s[j] = s[i];
    33. i--;
    34. j--;
    35. } else
    36. {
    37. // 如果s[i]是空格,就在s[j]处依次填入'0','2'和'%',然后i向前移动一位,j向前移动三位
    38. s[j] = '0';
    39. s[j - 1] = '2';
    40. s[j - 2] = '%';
    41. i--;
    42. j -= 3;
    43. }
    44. }
    45. return s;
    46. }
    47. };
  • 相关阅读:
    【JAVA】给线程的interrupt()方法使用举个栗子
    加法器—笔记
    docker安装mysql-简单无坑
    2022-9-7合并k个已排序的链表---困难
    行业洞察 | 如何更好的实现与虚拟人的互动体验?
    [仅需一步][org.eclipse.jgit]Java代码操作git命令的步骤...再也不用手写git命令了
    Java随笔-继承
    输入的查询SQL语句,是如何执行的?
    泛型+IO流基础知识+java->符号 lambda表达式
    自动驾驶和辅助驾驶系统的概念性架构(一)
  • 原文地址:https://blog.csdn.net/weixin_65592314/article/details/133279860