Skip to content

Instantly share code, notes, and snippets.

@Ray1988
Ray1988 / gist:5172896
Created March 15, 2013 20:30
sort string array base on anagram(hashtable implement)
public void anagramSort(String[] s){
if(s==null) return;
Hashtable<String, LinkedList<String>> hTable=new Hashtable<String, LinkedList<String>>()
for(String temp: s){
String key=sortChar(s);
if(!hTable.contains(key)){
hTable.put(key, new Linkedlist<String>());
}
LinkedList temp=hTable.get(key)
@Ray1988
Ray1988 / gist:5174855
Created March 16, 2013 03:42
11.3 Given a sorted array of n integers that has been rotated an unknown number of times, write code to find an element in the array. You may assume that the array was originally sorted in increasing order.
public int findNuminRotatedArray(int[] arr,int l, int r, int findNum){
int mid=(l+r)/2;
if(arr[mid]==finNum) return mid;
if(l>r){
return -1
}
if(arr[l]<arr[mid]){
public int binarySearch(String[] s, int start, int end, String target){
mid=(stat+edn);
if(s[mid].equals(target)) return mid;
if(s[mid]!=null){
if(target>s[mid]) return binarySearch(s, mid+1, end, target);
else return binarySearch(s, start, mid, target);
}
@Ray1988
Ray1988 / Swap Nodes in Pairs.java
Last active December 22, 2015 17:59
Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.
/**
* Definition for singly-linked list.
@Ray1988
Ray1988 / Reverse Nodes in k-Group.java
Last active December 22, 2015 18:18
Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed.
Only constant memory is allowed.
For example,
Given this linked list: 1->2->3->4->5
@Ray1988
Ray1988 / Divide Two Integers.java
Last active December 22, 2015 20:19
Divide Two Integers
Divide two integers without using multiplication, division and mod operator.
"Time complexity: [(log(dividend/divisor)](not sure)"
public class Solution {
public int divide(int dividend, int divisor) {
if (divisor==0){
// throw new InvalidInputException("Divisor can not be 0");
@Ray1988
Ray1988 / Search for a Range.java
Created September 12, 2013 00:56
Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1].
For example,
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].
@Ray1988
Ray1988 / Next Permutation.java
Last active December 22, 2015 22:29
Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically
next greater permutation of numbers.If such arrangement is not possible, it must
rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place, do not allocate extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1
@Ray1988
Ray1988 / Search Insert Position.java
Last active December 22, 2015 22:38
Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not,
return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Here are few examples.
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0
@Ray1988
Ray1988 / CountAndSay.java
Last active December 22, 2015 22:39
Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...
1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.
Given an integer n, generate the nth sequence.
Note: The sequence of integers will be represented as a string.