class Solution {
private List < List < Integer > > pathList = new LinkedList < > ( ) ;
private List < Integer > path = new LinkedList < > ( ) ;
public List < List < Integer > > permute ( int [ ] nums) {
backtrack ( nums) ;
return pathList;
}
public void backtrack ( int [ ] nums) {
if ( path. size ( ) == nums. length) {
pathList. add ( new LinkedList < > ( path) ) ;
return ;
}
for ( int i = 0 ; i < nums. length; i++ ) {
if ( path. contains ( nums[ i] ) ) continue ;
path. add ( nums[ i] ) ;
backtrack ( nums) ;
path. remove ( path. size ( ) - 1 ) ;
}
}
}
class Solution {
public void rotate ( int [ ] [ ] matrix) {
int n = matrix. length;
for ( int i = 0 ; i < n; i++ ) {
for ( int j = i; j < n; j++ ) {
int tmp = matrix[ i] [ j] ;
matrix[ i] [ j] = matrix[ j] [ i] ;
matrix[ j] [ i] = tmp;
}
}
for ( int [ ] row : matrix) {
reverse ( row) ;
}
}
public void reverse ( int [ ] row) {
int i = 0 , j = row. length - 1 ;
while ( i < j) {
int tmp = row[ i] ;
row[ i] = row[ j] ;
row[ j] = tmp;
i++ ;
j-- ;
}
}
}
class Solution {
public List < List < String > > groupAnagrams ( String [ ] strs) {
Map < String , List < String > > map = new HashMap < > ( ) ;
for ( String str : strs) {
String code = encode ( str) ;
map. putIfAbsent ( code, new LinkedList < > ( ) ) ;
map. get ( code) . add ( str) ;
}
return new ArrayList < > ( map. values ( ) ) ;
}
private String encode ( String s) {
char [ ] code = new char [ 26 ] ;
for ( char c : s. toCharArray ( ) ) {
int delta = c - 'a' ;
code[ delta] ++ ;
}
return new String ( code) ;
}
}
public int maxSubArray1 ( int [ ] nums) {
int n = nums. length;
int [ ] dp = new int [ n] ;
dp[ 0 ] = nums[ 0 ] ;
int res = dp[ 0 ] ;
for ( int i = 1 ; i < n; i++ ) {
dp[ i] = Math . max ( nums[ i] , dp[ i - 1 ] + nums[ i] ) ;
res = Math . max ( dp[ i] , res) ;
}
return res;
}
public int maxSubArray2 ( int [ ] nums) {
int n = nums. length;
int pre = nums[ 0 ] ;
int res = pre;
for ( int i = 1 ; i < n; i++ ) {
pre = Math . max ( nums[ i] , pre + nums[ i] ) ;
res = Math . max ( pre, res) ;
}
return res;
}
class Solution {
public boolean canJump ( int [ ] nums) {
int n = nums. length;
int fathest = 0 ;
for ( int i = 0 ; i < n - 1 ; i++ ) {
fathest = Math . max ( i + nums[ i] , fathest) ;
if ( fathest <= i) {
return false ;
}
}
return fathest >= n - 1 ;
}
}
class Solution {
public int [ ] [ ] merge ( int [ ] [ ] intervals) {
int n = intervals. length;
Arrays . sort ( intervals, Comparator . comparingInt ( x -> x[ 0 ] ) ) ;
LinkedList < int [ ] > res = new LinkedList < > ( ) ;
res. add ( intervals[ 0 ] ) ;
for ( int i = 1 ; i < n; i++ ) {
int [ ] curr = intervals[ i] ;
int [ ] last = res. getLast ( ) ;
if ( curr[ 0 ] <= last[ 1 ] ) {
last[ 1 ] = Math . max ( last[ 1 ] , curr[ 1 ] ) ;
} else {
res. add ( curr) ;
}
}
return res. toArray ( new int [ 0 ] [ 0 ] ) ;
}
}
public int uniquePaths ( int m, int n) {
int [ ] [ ] dp = new int [ m] [ n] ;
for ( int i = 0 ; i < m; i++ ) {
dp[ i] [ 0 ] = 1 ;
}
for ( int i = 0 ; i < n; i++ ) {
dp[ 0 ] [ i] = 1 ;
}
for ( int i = 1 ; i < m; i++ ) {
for ( int j = 1 ; j < n; j++ ) {
dp[ i] [ j] = dp[ i - 1 ] [ j] + dp[ i] [ j - 1 ] ;
}
}
return dp[ m - 1 ] [ n - 1 ] ;
}
public int minPathSum ( int [ ] [ ] grid) {
int m = grid. length;
int n = grid[ 0 ] . length;
int [ ] [ ] dp = new int [ m] [ n] ;
dp[ 0 ] [ 0 ] = grid[ 0 ] [ 0 ] ;
for ( int i = 1 ; i < m; i++ ) {
dp[ i] [ 0 ] = dp[ i - 1 ] [ 0 ] + grid[ i] [ 0 ] ;
}
for ( int i = 1 ; i < n; i++ ) {
dp[ 0 ] [ i] = dp[ 0 ] [ i - 1 ] + grid[ 0 ] [ i] ;
}
for ( int i = 1 ; i < m; i++ ) {
for ( int j = 1 ; j < n; j++ ) {
dp[ i] [ j] = Math . min ( dp[ i - 1 ] [ j] , dp[ i] [ j - 1 ] ) + grid[ i] [ j] ;
}
}
return dp[ m - 1 ] [ n - 1 ] ;
}
public int climbStairs ( int n) {
if ( n < 2 ) return n;
int [ ] dp = new int [ n] ;
dp[ 0 ] = 1 ;
dp[ 1 ] = 2 ;
for ( int i = 2 ; i < n; i++ ) {
dp[ i] = dp[ i - 1 ] + dp[ i - 2 ] ;
}
return dp[ n - 1 ] ;
}
暂时不写