leetcode [#268] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (11)
目录1. 题目2. 解决方案3. 注意事项
题目Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array.
For example:
Given nums = [0, 1, 3] return 2.
NoteYour algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
解决方案123456789101112131415161718192021public class Solution {    public int missingNumber(int[] nums) {        int N = nums.length;        if(N == 0) return 0;        int result = 0; ...阅读全文

leetcode [#283] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (11)
目录1. 题目2. 解决方案3. 注意事项
题目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.
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.解决方案12345678910111213141516public class Solution {    public void moveZeroes(int[] nums) {        int N = 0;        for(int k = 0;...阅读全文

leetcode [#278] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (20)
目录1. 题目2. 解决方案3. 注意事项
题目You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
Suppose you have n versions [1, 2, …, n] and you want to find out the first bad one, which causes all the following ones to be bad.
You are given an API bool isBadVersion(version) which will return wheth...阅读全文

leetcode [#326] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (16)
目录1. 题目2. 解决方案3. 注意事项
题目Given an integer, write a function to determine if it is a power of three.
Follow up:Could you do it without using any loop / recursion?
解决方案12345678910111213public class Solution {    public boolean isPowerOfThree(int n) {        if(n == 0) return false;        if(n == 1) return true;        boolean result = false;        String str = Integer.toString(n);        int len = str.length();        if(n == Math.pow(3, (2 * len - 1)) || n == Math.pow(3, (2 * len))){         ...阅读全文

leetcode [#292] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (18)
目录1. 题目2. 解决方案3. 注意事项
题目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 he...阅读全文