#1. 两数之和

#1. 两数之和

题目

给定一个数组nums,一个target,返回数组中两个相加等于target的数的索引,用List表示。

  • 每一个input都有一个解。

解答

哈希表解法

创建dict() 记录数字的val:index。循环List中的每一个元素nums[i],如果target - nums[i]已经出现在dict里,返回相应索引。

class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        records = dict()
        
        for idx, val in enumerate(nums):
            left = target - val
            if left in records:
                return [idx, records[left]]
            else:
                records[val] = idx

收获

  • 本题的精髓在于:两个相加等于target的数必定一前一后出现,所以对于每一个数,check之前子数组中是否存有余数,必定可以找到这两个相加得target的数。
  • enumerate方法用在数组上,可以得到索引,值。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这个任务我可以帮你完成。我们可以按照以下步骤进行设计: 1. 首先需要通过输入行数和列数来生成地图,我们可以使用二维字符数组来表示地图,其中字符“#”表示墙,字符“.”表示空地,字符“O”表示角色起始位置,字符“X”表示宝藏所在位置。 2. 在生成地图后,需要随机生成宝藏位置和角色起始位置。我们可以使用rand()函数生成两个不同的随机数作为宝藏和角色的坐标。 3. 接下来就是游戏的逻辑部分,需要循环读取玩家的输入,并根据输入来移动角色。如果角色移动到宝藏所在位置,则游戏结束,输出恭喜信息。 4. 在移动角色时,需要先判断移动后的位置是否越界,如果越界则不移动。如果不越界,则需要判断移动后的位置是否为墙,如果为墙则不移动。如果不是墙,则可以移动角色到该位置。 5. 在移动角色后,需要更新地图的显示,将原来的角色位置和宝藏位置改为“.”,将移动后的角色位置改为“O”,如果移动的位置是宝藏位置,则将该位置改为“$”。 下面是一个简单的代码实现,仅供参考: ```c++ #include <iostream> #include <cstdlib> #include <ctime> using namespace std; const int MAX_ROW = 10; const int MAX_COL = 10; void printMap(char map[MAX_ROW][MAX_COL], int row, int col) { for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { cout << map[i][j]; } cout << endl; } } int main() { srand(time(NULL)); int row, col; char map[MAX_ROW][MAX_COL]; cout << "请输入地图的行数和列数:"; cin >> row >> col; // 生成地图 for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (i == 0 || i == row - 1 || j == 0 || j == col - 1) { map[i][j] = '#'; } else { map[i][j] = '.'; } } } // 随机生成宝藏位置和角色起始位置 int treasureRow, treasureCol, playerRow, playerCol; do { treasureRow = rand() % (row - 2) + 1; treasureCol = rand() % (col - 2) + 1; playerRow = rand() % (row - 2) + 1; playerCol = rand() % (col - 2) + 1; } while (treasureRow == playerRow && treasureCol == playerCol); map[treasureRow][treasureCol] = 'X'; map[playerRow][playerCol] = 'O'; // 开始游戏 char direction; while (true) { printMap(map, row, col); cout << "请输入移动方向(w/a/s/d):"; cin >> direction; int newRow = playerRow, newCol = playerCol; switch (direction) { case 'w': newRow--; break; case 'a': newCol--; break; case 's': newRow++; break; case 'd': newCol++; break; default: continue; } if (newRow < 1 || newRow >= row - 1 || newCol < 1 || newCol >= col - 1) { continue; } else if (map[newRow][newCol] == '#') { continue; } else { map[playerRow][playerCol] = '.'; if (newRow == treasureRow && newCol == treasureCol) { map[newRow][newCol] = '$'; printMap(map, row, col); cout << "游戏结束,恭喜你完成游戏。" << endl; break; } else { map[newRow][newCol] = 'O'; playerRow = newRow; playerCol = newCol; } } } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值