This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package us.xinbo.www; | |
/** | |
* Question: give you an integer, you have to duplicate a digit of this integer, return the biggest new number | |
* For example: input: 123, output: 1233 | |
* input: 132, output: 1332 | |
* input: -132, output: -1132 | |
* input: -321, output: -3211 | |
*/ | |
class Solution { | |
public static int findMax(int num){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the Ts digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list. | |
EXAMPLE | |
Input:(7-> 1 -> 6) + (5 -> 9 -> 2).Thatis,617 + 295. | |
Output: 2 -> 1 -> 9.That is, 912. | |
FOLLOW UP | |
Suppose the digits are stored in forward order. Repeat the above problem. EXAMPLE | |
Input:(6 -> 1 -> 7) + (2 -> 9 -> 5).Thatis,617 + 295. | |
Output: 9 -> 1 -> 2.That is, 912. | |
*/ | |
LinkedListNode addLists(LinkedListNode l1, LinkedListNode l2, int carry){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
2.4 Write code to partition a linked list around a value x, | |
such that all nodes less than x come before all nodes greater than or equal to x. | |
*/ | |
class partition{ | |
public linkNode partition(linkNode node, int x){ | |
linkNode beforeStart = NULL; | |
linkNode beforeEnd = NULL; | |
linkNode afterStart = NULL; | |
linkNode afterEnd = NULL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
2.3 Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node. | |
* EXAMPLE | |
* Input: the node c from the linked list a->b->c->d->e | |
* Result: nothing is returned, but the new linked list looks like a->b->d->e | |
*/ | |
class deleteMiddle{ | |
public void deleteMiddle(Node node){ | |
if(node==NULL || node.next == NULL) | |
return; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
2.2 Implement an algorithm to find the kth to last element of a singly linked list. | |
*/ | |
public class findElement{ | |
public int findElement(LinkNode* head,int k){ | |
if(head == NULL) | |
return; | |
LinkNode* curr = head; | |
LinkNode* kStep = head; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class deleteDup{ | |
public void deleteDup(LinkNode* head){ | |
if(head == NULL) | |
return; | |
LinkNode* curr = head; | |
while(curr!=NULL){ | |
LinkNode runner = curr; | |
while(runner.next!=NULL){ | |
if(runner.next.value == curr.value){ | |
runner.next = runner.next.next; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Date: 09/14/2014 | |
Time Complexity: | |
Space Complexity: | |
*/ | |
public class solution{ | |
public static boolean isSubString(String s1, String s2){ | |
if(s1.contain(s2)) | |
return true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int[][] setMatrixzero(int[][] matrix){ | |
int M = matrix.length; | |
int N = matrix[0].length; | |
boolean setRowZero[] = new boolean [M]; | |
boolean setColumnZero[] = new boolean[N]; | |
for(int i = 0; i < M; i++){ | |
for(int j=0; j<N; j++) | |
if(matrix[i][j] ==0){ | |
setRowZero[i]=0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*implement a method to perform basic string compression using the | |
counts of repeated characters. For example, the string aabcccccaaa would | |
become a2b1c5a3. If the "compressed" string would not become smaller than | |
the original string, your method should return the original string. | |
Time complexity: O(n) | |
Space complexity: O(n) | |
Date: 09/02/2014 | |
*/ | |
public class compressed{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*1.4 Write a method to replace all spaces in a string with '%20'. You may assume that the string has sufficient space at the end of the string to hold the additional characters, and that you are given the "true" length of the string. (Note: if implementing in Java, please use a character array so that you can perform this operation in place.) | |
EXAMPLE | |
Input: "Mr John Smith " | |
Output: "Mr%20John%20Smith" | |
Time complexity: O(n) | |
Space complexity: O(1) | |
Date: 09/02/2014*/ | |
public class replaceSpace{ | |
public void replace(char[] str){ | |
int len = strlen(str); |
NewerOlder