Coding——小练习

 

 三、LRU实现方式

思路:hashMap + 双向链表(因为双向链表,算法复杂度低)

代码如下:

 1 public class Node<K, V> {
 2     Node<K, V> prev;
 3     Node<K, V> next;
 4     K k;
 5     V v;
 6 
 7     public Node(K k, V v) {
 8         this.k = k;
 9         this.v = v;
10     }
11 }
View Code
 1 public class LRUCache<K, V> {
 2 
 3     Node<K, V> head;
 4     Node<K, V> tail;
 5     HashMap<K, Node<K, V>> map;
 6     int capacity;
 7 
 8     public LRUCache(int capacity) {
 9         map = new HashMap<K, Node<K, V>>();
10         this.capacity = capacity;
11     }
12 
13     public V get(K key) {
14         Node<K, V> node = map.get(key);
15         if (node == null) {
16             return null;
17         }
18         V value = node.v;
19         // move node to tail
20         removeNode(node);
21         offerNode(node);
22         return value;
23     }
24 
25     public void put(K key, V value) {
26         if (map.containsKey(key)) {
27             Node<K, V> node = map.get(key);
28             node.v = value;
29 
30             // move node to tail
31             removeNode(node);
32             offerNode(node);
33         } else {
34 
35             // add to tail
36             Node<K, V> node = new Node<K, V>(key, value);
37             offerNode(node);
38             map.put(key, node);
39 
40             if (map.size() > capacity) {
41                 map.remove(head.k);
42                 removeNode(head);
43             }
44         }
45     }
46 
47     private void removeNode(Node<K, V> node) {
48         if (node.prev != null) {
49             node.prev.next = node.next;
50         } else {
51             head = node.next;
52         }
53 
54         if (node.next != null) {
55             node.next.prev = node.prev;
56         } else {
57             tail = node.prev;
58         }
59     }
60 
61     /*
62      * move node to tail
63      */
64     private void offerNode(Node<K, V> node) {
65         if (tail != null) {
66             tail.next = node;
67         }
68         node.prev = tail;
69         node.next = null;
70         tail = node;
71 
72         if (head == null) {
73             head = tail;
74         }
75     }
76 
77 }
View Code

 

一、字符串反转

input:“abcde”

output:"edcba"

解决方案:

从后往前,一个个放入到新的char 数组

 1 public String forReverse(String original) {
 2         char[] temp = original.toCharArray();
 3         StringBuffer sb = new StringBuffer();
 4         int tempLenth = temp.length;
 5         for (int i = tempLenth - 1; i >= 0; i--) {
 6             sb.append(temp[i]);
 7         }
 8         return sb.toString();
 9 
10     }

两端同时交换(从两边到中间)

 1         String input = "Hello world"; 
 2         char[] temparray = input.toCharArray(); 
 3         int left, right=0; 
 4         right = temparray.length-1; 
 5   
 6         for (left=0; left < right ; left++ ,right--) 
 7         { 
 8             // Swap values of left and right 
 9             char temp = temparray[left]; 
10             temparray[left] = temparray[right]; 
11             temparray[right]=temp; 
12         } 
13   
14         for (char c : temparray) 
15             System.out.print(c); 

两端同时交换(从中间到两端)

 1 public String forReverse2(String original) {
 2         char[] value = original.toCharArray();
 3         int count = value.length;
 4         int n = count - 1;
 5         for (int j = (n - 1) >> 1; j >= 0; j--) {
 6             int k = n - j;
 7             char cj = value[j];
 8             char ck = value[k];
 9             value[j] = ck;
10             value[k] = cj;
11 
12         }
13         return String.copyValueOf(value);
14     }

 

二、字母排序

题目:给定一个字符串(里面全是大写字母,从A到Z,可以重复),如“CAEEFDK”。让你从新进行排序。

要求:

①必须以辅音字母开头(元音字母为:A、E、I、O、U,其余的字母全是辅音字母)

②2个辅音字母不可以连续放在一起

③2个元音字母不可以连续放在一起

求,给定一个字符串后,对它进行重排,那么可以有多少种组合?

例子:

给定字符串“AAA”,组合数为0

给定字符串“ABEK”,组合数为4

解题算法如下(我自己想到的算法,正确与否,有兴趣的朋友一起探讨):

class Solution {
    public int solution(String S) {
        int sum = 1;
        // null check
        if (S == null || S.length() == 0) {
            return 0;
        }

        // Divide into two groups
        StringBuilder vowel = new StringBuilder();
        StringBuilder consonant = new StringBuilder();
        char[] original = S.toCharArray();
        for (char temp : original) {
            if (temp == 'A' || temp == 'E' || temp == 'I' || temp == 'O' || temp == 'U') {
                vowel.append(temp);
            } else {
                consonant.append(temp);
            }
        }

        // All vowels
        String vowelS = vowel.toString();
        String consonantS = consonant.toString();
        if (consonantS.length() == 0) {
            return 0;
        }

        // vowelS length
        int countVowel = vowelS.length();
        // consonantS length
        int countconsonant = consonantS.length();
        if ((countconsonant - countVowel) != 1 && countVowel != countconsonant) {
            return 0;
        }

        int countSamll = countVowel < countconsonant ? countVowel : countconsonant;

        for (int i = 0; i < countSamll; i++, countconsonant--, countVowel--) {
            sum = sum * countconsonant * countVowel;
        }
        return sum;
    }
}
View Code

解题思路,我就不说了,代码里写了,不明白的地方,大家相互探讨!如有不正之处,望指点。

 

补充中。。。2019/06/10更新

转载于:https://www.cnblogs.com/lihao007/p/10865017.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值