leetcode1

作者: 永远缺钱的程序员 | 来源:发表于2016-07-15 08:10 被阅读0次

按难度排序,每天刷点leetcode题,抄点解法,大部分解答是在leetcode的dicuss中找到的,没有一一引用,抱歉。
292 Nim Game
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones.
Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of stones in the heap.
For example, if there are 4 stones in the heap, then you will never win the game: no matter 1, 2, or 3 stones you remove, the last stone will always be removed by your friend.
Ans:当石子是4的整数倍时,无法赢。
371 Sum of Two Integers
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
Ans:

// Recursive
public int getSum(int a, int b) { return (b == 0) ? a : getSum(a ^ b, (a & b) << 1);}

258Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
For example:
Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
Follow up:Could you do it without any loop/recursion in O(1) runtime?
Ans:

class Solution {
public:
    int addDigits(int num) {
        return (num-1)%9 + 1;
    }
};

104Maximum Depth of Binary Tree&226Invert Binary Tree
Ans:二叉树的简单操作
283 Move Zeroes
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.For example,given nums = [0, 1, 0, 3, 12], after calling your function, nums
should be [1, 3, 12, 0, 0].
Note:
You must do this in-place without making a copy of the array.
Minimize the total number of operations.
Ans:

class Solution {
public: void moveZeroes(vector<int>& nums) 
{ int j = 0; // move all the nonzero elements advance for (int i = 0; i < nums.size(); i++) { if (nums[i] != 0) { nums[j++] = nums[i]; } } for (;j < nums.size(); j++) { nums[j] = 0; } }};

相关文章

  • LeetCode1

    用第一题熟悉一下环境 题目很简单,数组中找出两个数,使其和等于给出的target 思路 直接暴力求解,双重for循...

  • leetcode1

    按难度排序,每天刷点leetcode题,抄点解法,大部分解答是在leetcode的dicuss中找到的,没有一一引...

  • Leetcode1

    题目描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,...

  • leetcode1

    给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。 给定 nums = [2, 7, 11, 15], ...

  • LeetCode1: Single Number

    1、一个非空整数数组,除了一个元素只出现一次,其余元素都出现了两次。找出这个一单独出现的元素。note: 线性的时...

  • leetcode1 two sum

    问题:Given an array of integers, returnindicesof the two nu...

  • LeetCode1:Two Sum

    注:最精华的思想是:x = nums[i] dict[x] = i取出第i个数字(以0为开头),把它的值装载...

  • leetcode1 Two Sum

    题目 https://leetcode.com/problems/two-sum/ 思路 盲猜N^2的遍历会超时,...

  • Leetcode1:python实现

    给定一个整数数组 nums和一个整数目标值 target,请你在该数组中找出 和为目标值 的那两个整数,并返回它们...

  • Leetcode1——两数之和

    给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。你可以假设每个输入只对应一种答案,且同样的元素不能被重...

网友评论

      本文标题:leetcode1

      本文链接:https://www.haomeiwen.com/subject/rykyjttx.html