資料結構與演算法練習(Java實現)
阿新 • • 發佈:2019-01-22
package lintcode;
/**
*
* @ClassName: Solution
* @Description: TODO()
* @author A18ccms a18ccms_gmail_com
* @date 2017年8月14日 上午10:12:29
*
*/
public class Solution {
/**
* @param str: a string
* @return: a boolean
*/
public static void main(String[] args) {
//System.out.println(isUnique("abca"));
//System.out.println(aplusb(2, 4));
//System.out.println(strStr("hello,world","world"));
//System.out.println(isPalindrome(11));
//System.out.println(replaceSpace(new StringBuffer("we are happy")));
syso(checkDifferent("abca"));
}
public static void syso(Object object){
System.out.println(object);
}
/**
*
* @Title: strStr
* @Description: TODO(對於一個給定的 source 字串和一個 target 字串,
* 你應該在 source 字串中找出 target 字串出現的第一個位置(從0開始)。
* 如果不存在,則返回 -1。)
* @param @param source
* @param @param target
* @param @return 設定檔案
* @return int 返回型別
* @throws
*/
public static int strStr(String source, String target) {
// write your code here
if (source == null || target == null) {
return -1;
}
int i, j;
for (i = 0; i < source.length() - target.length() + 1; i++) {
for (j = 0; j < target.length(); j++) {
if (source.charAt(i + j) != target.charAt(j)) {
break;
}
}
if (j == target.length()) {
return i;
}
}
return -1;
}
/**
*
* @Title: removeElement
* @Description: TODO(給出一個數組 [0,4,4,0,0,2,4,4],和值 4
*返回 4 並且4個元素的新陣列為[0,0,0,2])
* @param @param A
* @param @param elem
* @param @return 設定檔案
* @return int 返回型別
* @throws
*/
public int removeElement(int[] A, int elem) {
// write your code here
int num=0;
for (int i = 0; i < A.length; i++) {
if(A[i]==elem){
num++;
}
return 1;
}
return 0;
}
/**
* @Title: isUnique
* @Description: TODO(//判斷字串是否重複)
* @param @param str
* @param @return 設定檔案
* @return boolean 返回型別
* @throws
*/
public static boolean isUnique(String str) {
// write your code here
if(str==null||str.isEmpty()){
return true;
}
char[] elements=str.toCharArray();
for(char e:elements){
if(str.indexOf(e)!=str.lastIndexOf(e)){
return false;
}
}
return true;
}
/**
* @Title: aplusb
* @Description: TODO(兩數相加)
* @param @param a
* @param @param b
* @param @return 設定檔案
* @return int 返回型別
* @throws
*/
public static int aplusb(int a, int b) {
// write your code here
if(a==0) return b;
if(b==0) return a;
int sum,i;
i=a^b;
sum=(a&b)<<1;
return aplusb(sum,i);
}
/**
* @Title: moveZeroes
* @Description: TODO(給一個數組 nums 寫一個函式將 0 移動到陣列的最後面,非零元素保持原陣列的順序)
* @param @param nums 設定檔案
* @return void 返回型別
* @throws
*/
public static void moveZeroes(int[] nums) {
// Write your code here
int one = 0;
int fast = 0;
int n = nums.length;
int x = 0;
for(int i=0;i<n;i++){
if(nums[i]!=x) { // 不為0 的向前移動
nums[one] = nums[i];
one++;
}
}
for(int i= one;i<n;i++) // 後面的就是0
nums[i] = x;
}
public int sum(int a,int b){
return a+b;
}
/**
*
* @ClassName: ListNode
* @Description: TODO( 將兩個排序連結串列合併為一個新的排序連結串列)
* @author A18ccms a18ccms_gmail_com
* @date 2017年8月14日 上午10:15:50
*
*/
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
public ListNode Merge(ListNode list1,ListNode list2) {
if(list1==null)return list2; //判斷到某個連結串列為空就返回另一個連結串列。如果兩個連結串列都為空呢?沒關係,這時候隨便返回哪個連結串列,不也是空的嗎?
if(list2==null)return list1;
ListNode list0=null;//定義一個連結串列作為返回值
if(list1.val<list2.val){//判斷此時的值,如果list1比較小,就先把list1賦值給list0,反之亦然
list0=list1;
list0.next=Merge(list1.next, list2);//做遞迴,求連結串列的下一跳的值
}
else{
list0=list2;
list0.next=Merge(list1, list2.next);
}
return list0;
}
}
/**
*
* @Title: isPalindrome
* @Description: TODO(迴文數判斷)
* @param @param x
* @param @return 設定檔案
* @return boolean 返回型別
* @throws
*/
public static boolean isPalindrome(int x) {
String str=x+"";
if(str.length()<=1){
return true;
}else{
if(new StringBuffer(str).reverse().toString().equals(str)){
return true;
}
return false;
}
}
/*
* 請實現一個函式,將一個字串中的空格替換成“%20”。例如,當字串為We Are Happy.則經過替換之後的字串為We%20Are%20Happy。
*/
public static String replaceSpace(StringBuffer str) {
//從後往前,先確定字串的長度和替換後的長度
int len=str.length();
int count=0;
for(int i=0;i<str.length();i++){
if(str.charAt(i)==' ')
count++;
}
int newLen=2*count+len;
int index=newLen-1;
char []ptr=new char[newLen];
while(len>0){
if(str.charAt(len-1)!=' '){
ptr[index--]=str.charAt(len-1);
}else{
ptr[index--]='0';
ptr[index--]='2';
ptr[index--]='%';
}
--len;
}
return new String(ptr);
}
/*
* 在一個長度為n的數組裡的所有數字都在0到n-1的範圍內。 陣列中某些數字是重複的,但不知道有幾個數字是重複的。
* 也不知道每個數字重複幾次。請找出陣列中任意一個重複的數字。 例如,如果輸入長度為7的陣列{2,3,1,0,2,5,3},
* 那麼對應的輸出是第一個重複的數字2
*/
public boolean duplicate(int numbers[],int length,int [] duplication) {
for(int i = 0; i < length; i++){
//由於數字都是0到length -1範圍內的,將以遍歷到的數字為下標的元素加上length
//當第二 次找到該下標元素,發現它大於length-1時,說明,該下標數字重複了。
//當遍歷到一個元素,發現它大於length -1時,讓它前去length,就為原來的元素
int index = numbers[i];//當遍歷到一個元素,發現它大於length -1時,讓它前去length,就為原來的元素
if(index > length - 1)
index -= length;
if(numbers[index] > length - 1){//當第二 次找到該下標元素,發現它大於length-1時,說明,該下標數字重複了。
// *duplication = index;
return true;
}
numbers[index] += length;//由於數字都是0到length -1範圍內的,將以遍歷到的數字為下標的元素加上length
}
return false;
}
/*
* 給定一個二叉樹,找到其最大深度。最大深度是從根節點到最遠葉節點的最長路徑中的節點數
*/
/* public int maxDepth(TreeNode root){
if (root == null)
return 0;
int leftDepth = maxDepth(root.left);
int rightDepth = maxDepth(root.right);
return leftDepth > rightDepth ? (leftDepth+1): (rightDepth+1);
}*/
/*
* 請實現一個演算法,確定一個字串的所有字元是否全都不同。這裡我們要求不允許使用額外的儲存結構。
*/
public static boolean checkDifferent(String iniString) {
// write code here
String str=iniString;
char c;
for(int i=0;i<str.length();i++){
c=str.charAt(i);
for(int j=i+1;j<str.length();j++){
if(str.charAt(j)==c){
return false;
}else {
continue;
}
}
}
return true;
}
}
/**
*
* @ClassName: Solution
* @Description: TODO()
* @author A18ccms a18ccms_gmail_com
* @date 2017年8月14日 上午10:12:29
*
*/
public class Solution {
/**
* @param str: a string
* @return: a boolean
*/
public static void main(String[] args) {
//System.out.println(isUnique("abca"));
//System.out.println(aplusb(2, 4));
//System.out.println(strStr("hello,world","world"));
//System.out.println(isPalindrome(11));
//System.out.println(replaceSpace(new StringBuffer("we are happy")));
syso(checkDifferent("abca"));
}
public static void syso(Object object){
System.out.println(object);
}
/**
*
* @Title: strStr
* @Description: TODO(對於一個給定的 source 字串和一個 target 字串,
* 你應該在 source 字串中找出 target 字串出現的第一個位置(從0開始)。
* 如果不存在,則返回 -1。)
* @param @param source
* @param @param target
* @param @return 設定檔案
* @return int 返回型別
* @throws
*/
public static int strStr(String source, String target) {
// write your code here
if (source == null || target == null) {
return -1;
}
int i, j;
for (i = 0; i < source.length() - target.length() + 1; i++) {
for (j = 0; j < target.length(); j++) {
if (source.charAt(i + j) != target.charAt(j)) {
break;
}
}
if (j == target.length()) {
return i;
}
}
return -1;
}
/**
*
* @Title: removeElement
* @Description: TODO(給出一個數組 [0,4,4,0,0,2,4,4],和值 4
*返回 4 並且4個元素的新陣列為[0,0,0,2])
* @param @param A
* @param @param elem
* @param @return 設定檔案
* @return int 返回型別
* @throws
*/
public int removeElement(int[] A, int elem) {
// write your code here
int num=0;
for (int i = 0; i < A.length; i++) {
if(A[i]==elem){
num++;
}
return 1;
}
return 0;
}
/**
* @Title: isUnique
* @Description: TODO(//判斷字串是否重複)
* @param @param str
* @param @return 設定檔案
* @return boolean 返回型別
* @throws
*/
public static boolean isUnique(String str) {
// write your code here
if(str==null||str.isEmpty()){
return true;
}
char[] elements=str.toCharArray();
for(char e:elements){
if(str.indexOf(e)!=str.lastIndexOf(e)){
return false;
}
}
return true;
}
/**
* @Title: aplusb
* @Description: TODO(兩數相加)
* @param @param a
* @param @param b
* @param @return 設定檔案
* @return int 返回型別
* @throws
*/
public static int aplusb(int a, int b) {
// write your code here
if(a==0) return b;
if(b==0) return a;
int sum,i;
i=a^b;
sum=(a&b)<<1;
return aplusb(sum,i);
}
/**
* @Title: moveZeroes
* @Description: TODO(給一個數組 nums 寫一個函式將 0 移動到陣列的最後面,非零元素保持原陣列的順序)
* @param @param nums 設定檔案
* @return void 返回型別
* @throws
*/
public static void moveZeroes(int[] nums) {
// Write your code here
int one = 0;
int fast = 0;
int n = nums.length;
int x = 0;
for(int i=0;i<n;i++){
if(nums[i]!=x) { // 不為0 的向前移動
nums[one] = nums[i];
one++;
}
}
for(int i= one;i<n;i++) // 後面的就是0
nums[i] = x;
}
public int sum(int a,int b){
return a+b;
}
/**
*
* @ClassName: ListNode
* @Description: TODO( 將兩個排序連結串列合併為一個新的排序連結串列)
* @author A18ccms a18ccms_gmail_com
* @date 2017年8月14日 上午10:15:50
*
*/
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
public ListNode Merge(ListNode list1,ListNode list2) {
if(list1==null)return list2; //判斷到某個連結串列為空就返回另一個連結串列。如果兩個連結串列都為空呢?沒關係,這時候隨便返回哪個連結串列,不也是空的嗎?
if(list2==null)return list1;
ListNode list0=null;//定義一個連結串列作為返回值
if(list1.val<list2.val){//判斷此時的值,如果list1比較小,就先把list1賦值給list0,反之亦然
list0=list1;
list0.next=Merge(list1.next, list2);//做遞迴,求連結串列的下一跳的值
}
else{
list0=list2;
list0.next=Merge(list1, list2.next);
}
return list0;
}
}
/**
*
* @Title: isPalindrome
* @Description: TODO(迴文數判斷)
* @param @param x
* @param @return 設定檔案
* @return boolean 返回型別
* @throws
*/
public static boolean isPalindrome(int x) {
String str=x+"";
if(str.length()<=1){
return true;
}else{
if(new StringBuffer(str).reverse().toString().equals(str)){
return true;
}
return false;
}
}
/*
* 請實現一個函式,將一個字串中的空格替換成“%20”。例如,當字串為We Are Happy.則經過替換之後的字串為We%20Are%20Happy。
*/
public static String replaceSpace(StringBuffer str) {
//從後往前,先確定字串的長度和替換後的長度
int len=str.length();
int count=0;
for(int i=0;i<str.length();i++){
if(str.charAt(i)==' ')
count++;
}
int newLen=2*count+len;
int index=newLen-1;
char []ptr=new char[newLen];
while(len>0){
if(str.charAt(len-1)!=' '){
ptr[index--]=str.charAt(len-1);
}else{
ptr[index--]='0';
ptr[index--]='2';
ptr[index--]='%';
}
--len;
}
return new String(ptr);
}
/*
* 在一個長度為n的數組裡的所有數字都在0到n-1的範圍內。 陣列中某些數字是重複的,但不知道有幾個數字是重複的。
* 也不知道每個數字重複幾次。請找出陣列中任意一個重複的數字。 例如,如果輸入長度為7的陣列{2,3,1,0,2,5,3},
* 那麼對應的輸出是第一個重複的數字2
*/
public boolean duplicate(int numbers[],int length,int [] duplication) {
for(int i = 0; i < length; i++){
//由於數字都是0到length -1範圍內的,將以遍歷到的數字為下標的元素加上length
//當第二 次找到該下標元素,發現它大於length-1時,說明,該下標數字重複了。
//當遍歷到一個元素,發現它大於length -1時,讓它前去length,就為原來的元素
int index = numbers[i];//當遍歷到一個元素,發現它大於length -1時,讓它前去length,就為原來的元素
if(index > length - 1)
index -= length;
if(numbers[index] > length - 1){//當第二 次找到該下標元素,發現它大於length-1時,說明,該下標數字重複了。
// *duplication = index;
return true;
}
numbers[index] += length;//由於數字都是0到length -1範圍內的,將以遍歷到的數字為下標的元素加上length
}
return false;
}
/*
* 給定一個二叉樹,找到其最大深度。最大深度是從根節點到最遠葉節點的最長路徑中的節點數
*/
/* public int maxDepth(TreeNode root){
if (root == null)
return 0;
int leftDepth = maxDepth(root.left);
int rightDepth = maxDepth(root.right);
return leftDepth > rightDepth ? (leftDepth+1): (rightDepth+1);
}*/
/*
* 請實現一個演算法,確定一個字串的所有字元是否全都不同。這裡我們要求不允許使用額外的儲存結構。
*/
public static boolean checkDifferent(String iniString) {
// write code here
String str=iniString;
char c;
for(int i=0;i<str.length();i++){
c=str.charAt(i);
for(int j=i+1;j<str.length();j++){
if(str.charAt(j)==c){
return false;
}else {
continue;
}
}
}
return true;
}
}