LeetCode刷题——190322

Work-break

题目

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.
For example, given s =“leetcode”, dict =[“leet”, “code”].
Return true because"leetcode"can be segmented as"leet code".

思路

动态规划
状态转移方程:f(i)=f(j)&&f(j+1,i);0<=j<i; f(i)表示从(0,i)是否可以分词;

重点判断边界。S.subStrng(j,i)//包括j但是不包括i;所以i的取值应该到length

代码



  public boolean wordBreak(String s, Set<String> dict) {
     if(s==null||s.length()==0||dict==null||dict.size()==0)
        return false;
     boolean[] flag = new boolean[s.length()+1];
     flag[0]=true;
     for(int i=1;i<=s.length();++i) {
        for(int j=i-1;j>=0;--j) {
            if(flag[j]&&dict.contains(s.substring(j, i))){
                flag[i]=true;
                break;
            }
            flag[i]=false;            
        }
     }
     return flag[s.length()];
    }




Copy-list-with-random-pointer

题目:

链表的深拷贝

思路:

深拷贝:创建一个新的对象和数组,将原对象的各项属性的**“值”(数组的所有元素)拷贝过来,是“值”而不是“引用”
浅拷贝:将原对象或原数组的引用直接赋给新对象,新数组,新对象/数组只是原对象的一个
引用**
首先忽略random域,进行正常next域的复制(遍历一遍链表);
再次遍历链表,对random域进行赋值(两边链表顺序不可以颠倒)

代码:

   public RandomListNode copyRandomList(RandomListNode head) {
	   if(head == null) return null;
           if(head.next==null&&head.random==null) return new RandomListNode(head.label);          
           RandomListNode res = new RandomListNode(head.label);
           RandomListNode cur = head.next;
           RandomListNode newNode = res;
           Map<RandomListNode,RandomListNode> map = new HashMap<>();
           map.put(newNode,head);//自己做一直错的原因是,没有把首节点放入map中;cur从head.next开始
           while(cur!=null){
               RandomListNode temp =new RandomListNode(cur.label);//temp复制了cur的值
               map.put(temp,cur);//在map中存入cur
               newNode.next = temp;
               cur = cur.next;
               newNode = newNode.next;
           }
           newNode = res;
           while(newNode!=null){
               newNode.random = map.get(newNode).random; 
               newNode = newNode.next;           
          }
        return res;
        }




Single-number

题目

给定一个数组,除了一个元素以外,其他每个数都出现了两次,找到那个出现一次的数。
注意:您的算法时间复杂度应该为线性。你能在不使用额外内存的情况下实现它吗?

思路

使用异或(相同异或为0,不同异或为1,1和0异或为1,0和0异或为0),所以最后剩下的

代码

public int singleNumber(int[] A) {
    int res = A[0];
    for(int i =1;i<A.length;++i)
        res = res^A[i];
    return res;
}



Singtle-number-ii

题目:

给定一个数组,除了一个元素,其余都出现了三次
注意:您的算法的时间复杂度为线性。你能在不使用额外内存的情况下实现吗?

思路:

1.使用Map 2.使用三元组的异或
Map(复杂度近似为n)

代码:

(该方法不满足O(1))

   public int singleNumber(int[] A) {
     Map<Integer,Integer> map = new HashMap<>();
     for(int i=0;i<A.length;++i) {
        if(map.containsKey(A[i])) 
            map.put(A[i], map.get(A[i])+1);
        else
            map.put(A[i], 1);
     }
     
     for(Integer key: map.keySet()) {
        if(map.get(key)!=3) 
            return key;
     }
        return 0;
}

三元组的异或

借由位运算

http://blog.jobbole.com/78358/

https://www.nowcoder.com/profile/2343147/codeBookDetail?submissionId=20452002

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值