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

一: 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;
    }
};
时间: 2024-10-24 17:11:59

LeetCode66/169/79 Plus One/Majority Element /Word Search的相关文章

Majority Element(ARRAY-BINARY SEARCH)

QUESTION 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. FIRST TRY 每找出两个不同的e

169. Majority Element &amp;&amp; 229. Majority Element II

169. 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. Hide T

169. Majority Element(C++)

169. 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. 题目大意:

LeetCode Javascript实现 169. Majority Element

169. Majority Element /** * @param {number[]} nums * @return {number} */ var majorityElement = function(nums) { var hash = {}; var y=-1,z; //注意这里的方括号,利用变量访问对象属性时要用方括号 for(var i=0;i<=nums.length-1;i++){ if(hash[nums[i]]){ hash[nums[i]]++; }else{ hash[

leetCode 169. Majority Element 数组

169. 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. 思路1: 使用m

169. Majority Element - LeetCode

Question 169. Majority Element Solution 思路:构造一个map存储每个数字出现的次数,然后遍历map返回出现次数大于数组一半的数字. 还有一种思路是:对这个数组排序,次数超过n/2的元素必然在中间. Java实现: public int majorityElement(int[] nums) { Map<Integer, Integer> countMap = new HashMap<>(); for (int num : nums) { In

Leetcode#169. Majority Element(求众数)

题目描述 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ? n/2 ? 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [3,2,3] 输出: 3 示例 2: 输入: [2,2,1,1,1,2,2] 输出: 2 思路 思路一: 利用哈希表的映射,储存数组中的数字以及它们出现的次数,当众数出现时,返回这个数字. 思路二: 因为众数是出现次数大于n/2的数字,所以排序之后中间的那个数字一定是众数.即nums[n/2]为众数.但是在计算比

[Lintcode]46. Majority Element/[Leetcode]169. Majority Element

46. Majority Element/[169. Majority Element(https://leetcode.com/problems/majority-element/) 本题难度: Easy Topic: Greedy Description Given an array of integers, the majority number is the number that occurs more than half of the size of the array. Find

刷题169. Majority Element

一.题目说明 题目169. Majority Element,给定n个数的数组,返回出现次数超过半数的元素. 二.我的解答 这个题目用一个map,遍历一遍数组,计数每个元素出现的次数. class Solution{ public: int majorityElement(vector<int>& nums){ unordered_map<int,int> ump; int maxNum = 0; int maxCount = 0; for(int i=0;i<num