赞
踩
- class Solution {
- public int[] productExceptSelf(int[] nums) {
- int[] dpleft = new int[nums.length];
- int[] dpright = new int[nums.length];
- int[] answer = new int[nums.length];
- dpleft[0] = 1;
- dpright[nums.length-1] = 1;
- for (int i=1;i<nums.length;i++){
- dpleft[i] = dpleft[i-1]*nums[i-1];
- }
- for (int i=nums.length-2;i>=0;i--){
- dpright[i] = dpright[i+1]*nums[i+1];
- }
- for (int i=0;i<nums.length;i++){
- answer[i] = dpleft[i] * dpright[i];
- }
- return answer;
- }
- }
- class Solution {
- public int firstMissingPositive(int[] nums) {
- //最小正整数只能在【1,nums.length+1】中间出现
- //将数组当成map看待
-
- // 给每一个负数元素打上标记:只要是不影响后面的标记就行,可以大于等于nums.length+1
- for (int i=0;i<nums.length;i++){
- if (nums[i]<=0){
- nums[i] = nums.length+1;
- }
- }
- // 给数组中在【1,nums.length】区间的元素打上标记
- // 每个数组(i)下标+1,代表正整数(i+1)
- // 如果有i+1的值的正整数,则下标i的数组元素打为负数
- for (int i=0;i<nums.length;i++){
- int num = Math.abs(nums[i]);
- if (num<=nums.length){
- nums[num-1] = -Math.abs(nums[num-1]);
- }
- }
- // 找到第一个不为负数的元素,记住下标i,则i+1即未不存在的正整数
- for (int i=0;i<nums.length;i++){
- if (nums[i]>0){
- return i+1;
- }
- }
- // 如果【1,nums.length】都具有,即下标【0,nums.length-1】元素都打了标记
- // 则返回之后的下一个正整数
- return nums.length+1;
- }
- }
- class Solution {
- public int firstMissingPositive(int[] nums) {
- // 置换元素,匹配正整数和元素下标
-
- // 找到在区间【1,nums.length】的元素,将该元素与下标相对应的元素交换
- for (int i=0;i<nums.length;i++){
- while(nums[i]>0 && nums[i]<=nums.length && nums[nums[i]-1]!=nums[i]){
- int temp = nums[nums[i]-1];
- nums[nums[i]-1] = nums[i];
- nums[i] = temp;
- }
- }
- // 找到置换之后的数组中,元素与下标不符合的第一个下标,即为最小正整数-1
- for (int i=0;i<nums.length;i++){
- if (nums[i]!= i+1){
- return i+1;
- }
- }
- return nums.length+1;
- }
- }
- class Solution {
- public void setZeroes(int[][] matrix) {
- boolean[] flagrow = new boolean[matrix.length];
- boolean[] flagcol = new boolean[matrix[0].length];
- for (int i=0;i<matrix.length;i++){
- for (int j=0;j<matrix[0].length;j++){
- if (matrix[i][j]==0){
- flagrow[i] = true;
- flagcol[j] = true;
- }
- }
- }
- for (int i=0;i<matrix.length;i++){
- for (int j=0;j<matrix[0].length;j++){
- if (flagrow[i] || flagcol[j]){
- matrix[i][j] = 0;
- }
- }
- }
- }
- }
- class Solution {
- public List<Integer> spiralOrder(int[][] matrix) {
- int m = matrix.length;
- int n = matrix[0].length;
- List<Integer> result = new ArrayList<>();
- int up = 0, down = m-1;
- int left = 0, right = n-1;
- while(true){
- //left--right
- if (left>right) break;
- for (int i=left;i<=right;i++){
- result.add(matrix[up][i]);
- }
- up++;
- //up--down
- if (up>down) break;
- for (int i=up;i<=down;i++){
- result.add(matrix[i][right]);
- }
- right--;
- //right--left
- if(right<left) break;
- for (int i=right;i>=left;i--){
- result.add(matrix[down][i]);
- }
- down--;
- //down--up
- if(down<up) break;
- for (int i=down;i>=up;i--){
- result.add(matrix[i][left]);
- }
- left++;
- }
- return result;
- }
- }
- class Solution {
- public void rotate(int[][] matrix) {
- int n=matrix.length;
- for (int i=0;i<n/2;i++){
- for (int j=0;j<n;j++){
- int temp = matrix[i][j];
- matrix[i][j] = matrix[n-1-i][j];
- matrix[n-1-i][j] = temp;
- }
- }
- for (int i=0;i<n;i++){
- for (int j=0;j<i;j++){
- int temp = matrix[i][j];
- matrix[i][j] = matrix[j][i];
- matrix[j][i] = temp;
- }
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。