There is a special square room with mirrors on each of the four walls. Except for the southwest corner, there are receptors on each of the remaining corners, numbered 0, 1, and 2.
The square room has walls of length p and a laser ray from the southwest corner first meets the east wall at a distance q from the 0<sup>th</sup> receptor.
Given the two integers p and q, return the number of the receptor that the ray meets first.
The test cases are guaranteed so that the ray will meet a receptor eventually.
Given two integers dividend and divisor, divide two integers without using multiplication, division, and mod operator.
The integer division should truncate toward zero, which means losing its fractional part. For example, 8.345 would be truncated to 8, and -2.7335 would be truncated to -2.
Return *the quotient after dividing dividend by *divisor.
**Note: **Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: [−2<sup>31</sup>, 2<sup>31</sup><span> </span>− 1]. For this problem, if the quotient is strictly greater than2<sup>31</sup><span> </span>- 1, then return 2<sup>31</sup><span> </span>- 1, and if the quotient is strictly less than-2<sup>31</sup>, then return -2<sup>31</sup>.
You are given a 2D integer array stockPrices where stockPrices[i] = [day<sub>i</sub>, price<sub>i</sub>] indicates the price of the stock on day day<sub>i</sub> is price<sub>i</sub>. A line chart is created from the array by plotting the points on an XY plane with the X-axis representing the day and the Y-axis representing the price and connecting adjacent points. One such example is shown below:
Given an array of points where points[i] = [x<sub>i</sub>, y<sub>i</sub>] represents a point on the X-Y plane, return the maximum number of points that lie on the same straight line.
Given an integer array nums, design an algorithm to randomly shuffle the array. All permutations of the array should be equally likely as a result of the shuffling.
Implement the Solution class:
Solution(int[] nums) Initializes the object with the integer array nums.
int[] reset() Resets the array to its original configuration and returns it.
int[] shuffle() Returns a random shuffling of the array.
classSolution { int[] data; int[] shuffle; Random r; publicSolution(int[] nums) { data = nums; shuffle = newint[nums.length]; r = newRandom(); for(inti=0; i < data.length; i++){ shuffle[i] = nums[i]; } } publicint[] reset() { return data; } publicint[] shuffle() { for(inti= data.length-1 ; i >= 0; i--){ swap(i, r.nextInt(i+1)); } return shuffle; } privatevoidswap(int i, int j){ inttemp= shuffle[i]; shuffle[i] = shuffle[j]; shuffle[j] = temp; } }
/** * Your Solution object will be instantiated and called as such: * Solution obj = new Solution(nums); * int[] param_1 = obj.reset(); * int[] param_2 = obj.shuffle(); */
classSolution { int[] data; Random r; publicSolution(int[] nums) { data = nums; r = newRandom(); } publicint[] reset() { return data; } publicint[] shuffle() { ArrayList<Integer> bin = newArrayList<>(); for(inti=0; i < data.length; i++){ bin.add(data[i]); } int[] ret = newint[data.length]; for(inti=0 ; i < data.length; i++){ intrandom= r.nextInt(bin.size()); ret[i] = bin.get(random); bin.remove(random); } return ret; } }
/** * Your Solution object will be instantiated and called as such: * Solution obj = new Solution(nums); * int[] param_1 = obj.reset(); * int[] param_2 = obj.shuffle(); */
There are n friends that are playing a game. The friends are sitting in a circle and are numbered from 1 to n in clockwise order. More formally, moving clockwise from the ith friend brings you to the (i+1)th friend for 1 <= i < n, and moving clockwise from the nth friend brings you to the 1st friend.
The rules of the game are as follows:
1.Start at the 1st friend.
2.Count the next k friends in the clockwise direction including the friend you started at. The counting wraps around the circle and may count some friends more than once.
3.The last friend you counted leaves the circle and loses the game.
4.If there is still more than one friend in the circle, go back to step 2 starting from the friend immediately clockwise of the friend who just lost and repeat.
5.Else, the last friend in the circle wins the game. Given the number of friends, n, and an integer k, return the winner of the game.
An integer array is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.
For example, [1,3,5,7,9], [7,7,7,7], and [3,-1,-5,-9] are arithmetic sequences. Given an integer array nums, return the number of arithmetic subarrays of nums.
A subarray is a contiguous subsequence of the array.
You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
class Solution { public void rotate(int[][] matrix) { int m = matrix.length, n = matrix[0].length; int[][] visited = new int[m][n]; for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ if(visited[i][j] == 1) continue; int x = i, y = j; int value = matrix[i][j]; while(true){ int s = n-1-x, t = y; if(visited[t][s] == 1) break; int temp = matrix[t][s]; matrix[t][s] = value; value = temp; visited[t][s] = 1; x = t; y = s; } } } } }
Solution 2
此方法类似Solution 1,但是并非原地算法。
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { publicvoidrotate(int[][] matrix) { intm= matrix.length, n = matrix[0].length; int[][] mat = newint[m][n]; for(inti=0; i < m; i++){ for(intj=0; j < n; j++){ ints= n-1-i, t = j; mat[t][s] = matrix[i][j]; } } for(inti=0; i < m; i++){ for(intj=0; j < n; j++){ matrix[i][j] = mat[i][j]; } } } }