Skip to content

Instantly share code, notes, and snippets.

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){
/*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){
/*
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;
/*
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;
/*
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;
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;
@XinboChen
XinboChen / cc1.8.java
Created September 14, 2014 21:24
Assume you have a method isSubstring which checks if one word is a substring of another. Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring (e.g.,"waterbottle"is a rotation of"erbottlewat").
/*
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;
@XinboChen
XinboChen / CC1.7.java
Created September 14, 2014 16:01
Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0.
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;
/*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{
/*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);