class Solution {
public String longestCommonPrefix ( String [ ] strs) {
if ( strs. length == 0 )
return "" ;
String ans = strs[ 0 ] ;
for ( int i = 1 ; i< strs. length; i++ ) {
int j= 0 ;
for ( ; j< ans. length ( ) && j < strs[ i] . length ( ) ; j++ ) {
if ( ans. charAt ( j) != strs[ i] . charAt ( j) )
break ;
}
ans = ans. substring ( 0 , j) ;
if ( ans. equals ( "" ) )
return ans;
}
return ans;
}
}
public static boolean isValid ( String s) {
if ( s. isEmpty ( ) ) {
return true ;
}
Stack < Character > stack = new Stack < Character > ( ) ;
for ( char c: s. toCharArray ( ) ) {
if ( c == '(' ) {
stack. push ( ')' ) ;
}
else if ( c == '[' ) {
stack. push ( ']' ) ;
}
else if ( c == '{' ) {
stack. push ( '}' ) ;
}
else if ( stack. empty ( ) || c!= stack. pop ( ) ) {
return false ;
}
}
if ( stack. empty ( ) ) {
return true ;
}
return false ;
}
}
class Solution {
public ListNode mergeTwoLists ( ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode ( 0 ) ;
ListNode cur = dummyHead;
while ( l1 != null && l2 != null ) {
if ( l1. val < l2. val) {
cur. next = l1;
cur = cur. next;
l1 = l1. next;
} else {
cur. next = l2;
cur = cur. next;
l2 = l2. next;
}
}
if ( l1 == null ) {
cur. next = l2;
}
if ( l2 == null ) {
cur. next = l1;
}
return dummyHead. next;
}
class Solution {
public int removeDuplicates ( int [ ] nums) {
int len = nums. length;
int j= 0 ;
for ( int i= 1 ; i< nums. length; i++ ) {
if ( nums[ i- 1 ] == nums[ i] ) {
len-- ;
}
else {
nums[ ++ j] = nums[ i] ;
}
}
return len;
}
}
public int removeDuplicates ( int [ ] nums) {
if ( nums == null || nums. length == 0 ) return 0 ;
int p = 0 ;
int q = 1 ;
while ( q < nums. length) {
if ( nums[ p] != nums[ q] ) {
nums[ p + 1 ] = nums[ q] ;
p++ ;
}
q++ ;
}
return p + 1 ;
}
class Solution {
public int removeElement ( int [ ] nums, int val) {
int j= - 1 ;
for ( int i= 0 ; i< nums. length; i++ ) {
if ( nums[ i] != val) {
nums[ ++ j] = nums[ i] ;
}
}
return j+ 1 ;
}
}
public int searchInsert ( int [ ] nums, int target) {
for ( int i = 0 ; i < nums. length; i++ ) {
if ( nums[ i] >= target) {
return i;
}
}
return nums. length;
}
class Solution {
public int maxSubArray ( int [ ] nums) {
int ans = nums[ 0 ] ;
int sum = 0 ;
for ( int num: nums) {
if ( sum > 0 ) {
sum += num;
}
else {
sum = num;
}
ans = Math . max ( ans, sum) ;
}
return ans;
}
}
class Solution {
public int lengthOfLastWord ( String s) {
char [ ] ch = s. toCharArray ( ) ;
int temp = 0 ;
for ( int i= 0 ; i< ch. length; i++ ) {
if ( ch[ i] == ' ' ) {
temp = 0 ;
}
temp++ ;
}
return temp- 1 ;
}
}
class Solution {
public int singleNumber ( int [ ] nums) {
int ans= 0 ;
for ( int i= 0 ; i< nums. length; i++ ) {
ans^= nums[ i] ;
}
return ans;
}
}
class Solution {
public int climbStairs ( int n) {
int [ ] dp = new int [ n + 1 ] ;
dp[ 0 ] = 1 ;
dp[ 1 ] = 1 ;
for ( int i = 2 ; i <= n; i++ ) {
dp[ i] = dp[ i - 1 ] + dp[ i - 2 ] ;
}
return dp[ n] ;
}
}
class Solution {
public ListNode deleteDuplicates ( ListNode head) {
ListNode temp = head;
while ( temp != null && temp. next != null ) {
if ( temp. val == temp. next. val) {
temp. next = temp. next. next;
}
else {
temp = temp. next;
}
}
return head;
}
}
public static ListNode reverseListIterative ( ListNode head) {
ListNode prev = null ;
ListNode curr = head;
while ( curr != null ) {
ListNode nextTemp = curr. next;
curr. next = prev;
prev = curr;
curr = nextTemp;
}
return prev;
}
class Solution {
public void merge ( int [ ] nums1, int m, int [ ] nums2, int n) {
int p1 = 0 , p2 = 0 ;
int [ ] sorted = new int [ m + n] ;
int cur;
while ( p1 < m || p2 < n) {
if ( p1 == m) {
cur = nums2[ p2++ ] ;
} else if ( p2 == n) {
cur = nums1[ p1++ ] ;
} else if ( nums1[ p1] < nums2[ p2] ) {
cur = nums1[ p1++ ] ;
} else {
cur = nums2[ p2++ ] ;
}
sorted[ p1 + p2 - 1 ] = cur;
}
for ( int i = 0 ; i != m + n; ++ i) {
nums1[ i] = sorted[ i] ;
}
}
}
class Solution {
public int lengthOfLongestSubstring ( String s) {
HashMap < Character , Integer > map = new HashMap < > ( ) ;
int maxLen = 0 ;
int left = 0 ;
for ( int i= 0 ; i< s. length ( ) ; i++ ) {
if ( map. containsKey ( s. charAt ( i) ) ) {
left = Math . max ( left, map. get ( s. charAt ( i) ) + 1 ) ;
}
map. put ( s. charAt ( i) , i) ;
maxLen = Math . max ( maxLen, i- left+ 1 ) ;
}
}
}