LeetCode66/169/79 Plus One/Majority Element /Word Search

本文解析了LeetCode上的三个经典算法题目:数字加一、求众数和单词搜索。通过详细的代码实现和算法思路说明,帮助读者理解这些基础但重要的算法问题。

一: Plus One

题目:

Given a non-negative number represented as an array of digits, plus one to the number.

The digits are stored such that the most significant digit is at the head of the list.

链接:https://leetcode.com/problems/plus-one/

分析:就是简单的加法运算,注意进位即可。

class Solution {
public:
    vector<int> plusOne(vector<int> &digits) {
        vector<int> vec;
        int up = 1;
        for(int i = digits.size()-1; i >= 0 ; i--){
            int value = digits[i] + up;
            vec.insert(vec.begin(), value%10);
            if(value < 10) up = 0;
        }
        if(up == 1) vec.insert(vec.begin(), 1);
        return vec;
        
    }
};
二:Majority Element 

题目:Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.

You may assume that the array is non-empty and the majority element always exist in the array.

链接:https://leetcode.com/problems/majority-element/

分析:就是统计元素的词频,可以使用hash_map  在O(N)时间 map为<value, 词频>的形式,没有出现过则为1,出现了则+1;当有出现次数超过一半时返回。

class Solution {
public:
    int majorityElement(vector<int> &num) {
        map<int, int> hmap;
        for(int i = 0; i < num.size(); i++){
            if(!hmap.count(num[i]))hmap[num[i]] = 1;
            else hmap[num[i]] += 1;
            if(hmap[num[i]] >= (num.size()+1)/2) return num[i];
        }
    }
};

三:Word Search 

题目:

Given a 2D board and a word, find if the word exists in the grid.

The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.

For example,
Given board =

[
  ["ABCE"],
  ["SFCS"],
  ["ADEE"]
]
word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.

链接:https://leetcode.com/problems/word-search/

分析:这题就是dfs的思想,需要回溯。

class Solution {
public:
    bool dfs(int xi, int yi, string &word, int index, vector<vector<char> > &board, const int &m, const int &n, int **visited){
        visited[xi][yi] = 1;        // 该结点已经访问过了
        if(index + 1 < word.size()){
            if(xi-1 >= 0 && visited[xi-1][yi]==0 && board[xi-1][yi] == word[index+1]){
                if(dfs(xi-1, yi, word, index+1, board, m, n, visited))return true;   //深度遍历
				visited[xi-1][yi] = 0;      // 这条路行不通 设为未访问 以不影响下面的遍历
            }
            if(xi+1 <m && visited[xi+1][yi]==0 && board[xi+1][yi] == word[index+1]){
                if(dfs(xi+1, yi, word, index+1, board, m, n, visited))return true;
				visited[xi+1][yi] = 0;
            }
            if(yi-1 >= 0 && visited[xi][yi-1]==0 && board[xi][yi-1] == word[index+1]){
                if(dfs(xi, yi-1, word, index+1, board, m, n,visited)) return true;
				visited[xi][yi-1] = 0;
            }
            if(yi+1 < n && visited[xi][yi+1]==0 && board[xi][yi+1] == word[index+1]){
                if(dfs(xi, yi+1, word, index+1, board, m, n,visited)) return true;
				visited[xi][yi+1] = 0;
            }
            return false;
        }else return true;
    }
    
	void initVisited(int ** visited, const int &m, const int &n){
		for(int i = 0; i < m; i++)
			memset(visited[i], 0, sizeof(int)*n);
	}
    bool exist(vector<vector<char> > &board, string word) {
        int m = board.size();
        int n = board[0].size();
        int **visited = new int*[m];
        for(int i = 0; i < m; i++)
            visited[i] = new int[n];
        
        for(int i = 0; i < m; i++){   // 找到其实的i和j
            for(int j = 0; j < n; j++){
                if(word[0] == board[i][j]){
                    initVisited(visited, m, n);
                    if(dfs(i, j, word, 0, board, m, n,visited)) return true;
                }
            }
        }
        for(int i = 0; i < m; i++)
            delete []visited[i];
        delete []visited;
        return false;
    }
};



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值