码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-04-05 21:59:23      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:math   dfs   回溯   array   

一: 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;
    }
};



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

标签:math   dfs   回溯   array   

原文地址:http://blog.csdn.net/lu597203933/article/details/44891745

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!