一遍考研,一遍还是要刷刷题。感觉自己的时间安排的不是很好,还是要抓紧自己的日常时间,当然,也要练练刷题的手感。

1.第一题就两重循环找到索引就OK,因为是无序的,所以就不能用二分来查找,题目中每个数的下标是定死的,所以不能排序后再二分。真是太年轻,什么都想试试(4.5)

 public class Solution {
public int[] twoSum(int[] nums, int target) {
int[] arr = new int[2];
int cnt = 0;
for(int i = 0; i < nums.length; ++i){
for(int j = i+1; j < nums.length; ++j) {
if(nums[i] + nums[j] == target){
arr[cnt] = i;
cnt++;
arr[cnt] = j;
cnt++;
}
}
}
return arr;
}
}

2.第二题就是个简单的java单链表,将两个链表合成一个链表。需要简单考虑一下进位问题,调试还是调试了一会儿,链表又有段时间没用过了,想了半天。

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int t1, t2;
ListNode head;
t1 = (l1.val + l2.val) % 10;
ListNode l = new ListNode(t1);
head = l;
t2 = (l1.val + l2.val) / 10;
while(l1.next != null && l2.next != null){
l1 = l1.next;
l2 = l2.next;
t1 = (l1.val + l2.val + t2) % 10;
t2 = (l1.val + l2.val + t2) / 10;
ListNode ltmp = new ListNode(t1);
l.next = ltmp;
l = l.next;
}
while(l1.next!=null){
l1 = l1.next;
t1 = (l1.val + t2) % 10;
t2 = (l1.val + t2) / 10;
ListNode ltmp = new ListNode(t1);
l.next = ltmp;
l = l.next;
}
while(l2.next != null) {
l2 = l2.next;
t1 = (l2.val + t2) % 10;
t2 = (l2.val + t2) / 10;
ListNode ltmp = new ListNode(t1);
l.next = ltmp;
l = l.next;
}
if(t2 != 0){
ListNode ltmp = new ListNode(t2);
l.next = ltmp;
l = l.next;
}
return head;
}
}

我去,返回头看自己以前的代码,感觉昨天写的好搓啊

 public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode ret = new ListNode(0);
ListNode cur = ret; int sum = 0;
while(true) {
if(l1 != null) {
sum += l1.val;
l1 = l1.next;
}
if(l2 != null) {
sum += l2.val;
l2 = l2.next;
}
cur.val = sum % 10;
sum /= 10; if(l1 != null || l2 != null || sum != 0) {
//cur = (cur.next = new ListNode(0));
cur.next = new ListNode(0);
cur = cur.next;
} else {
break;
}
}
return ret;
}
}

3.第37题sudu,这个题目好吧,我猥琐的用了之前的代码,就dfs就可以

 package LeetCode;

 /**
* Created by lenovo on 2016-04-07.
*/ public class Solution {
static int[][] row = new int[9][9];
static int[][] col = new int[9][9];
static int[][] per = new int[9][9];
static int[][] map = new int[9][9];
static boolean isFind = false; public void solveSudoku(char[][] board) {
fill(row);
fill(col);
fill(per);
isFind = false;
int k;
for(int i = 0; i < 9; ++i)
for(int j = 0; j < 9; ++j){
if(board[i][j] != '.'){
k = board[i][j] - '0';
map[i][j] = k - 1;
if(k != 0){
row[i][k-1] = col[j][k-1] =
per[(i/3)*3+(j/3)][k-1] = 1;
}
}else{
map[i][j] = -1;
}
} dfs(0, 0, board);
}
static void fill(int[][] a){
for(int i = 0; i < 9; ++i){
for(int j = 0; j < 9; ++j){
a[i][j] = 0;
}
}
}
public static void dfs(int x, int y, char[][] board){
int u = x * 9 + y + 1;
if(x == 9){
isFind = true;
for(int i = 0; i < 9; ++i)
for(int j = 0; j < 9; ++j)
board[i][j] = (char)(map[i][j] + '0' + 1);
}
if(isFind) return;
if(map[x][y] != -1){
dfs(u/9, u%9, board);
return;
} for(int i = 0; i < 9 && !isFind; ++i){
int k = (x/3)*3 + y/3;
if(row[x][i] == 0 && col[y][i] == 0 && per[k][i] == 0){
row[x][i] = col[y][i] = per[k][i] = 1;
map[x][y] = i; dfs(u/9, u%9, board); row[x][i] = col[y][i] = per[k][i] = 0;
map[x][y] = -1;
}
}
} public static void main(String[] args){
char[][] board = { {'.','.','9','7','4','8','.','.','.'},
{'7','.','.','.','.','.','.','.','.'},
{'.','2','.','1','.','9','.','.','.'},
{'.','.','7','.','.','.','2','4','.'},
{'.','6','4','.','1','.','5','9','.'},
{'.','9','8','.','.','.','3','.','.'},
{'.','.','.','8','.','3','.','2','.'},
{'.','.','.','.','.','.','.','.','6'},
{'.','.','.','2','7','5','9','.','.'}}; Solution s = new Solution();
s.solveSudoku(board);
}
}

但是,这个题目我有疑惑,并不是算法的疑惑,而是java中static变量的疑惑。因为有过用java写面向过程的代码(好吧,是java的语法问题,关于static的,以前用一直都没有问题,然后今天就有问题了)然后好好找下,看看(4.7)

LeetCode刷刷记录的更多相关文章

  1. LeetCode解题记录(贪心算法)(二)

    1. 前言 由于后面还有很多题型要写,贪心算法目前可能就到此为止了,上一篇博客的地址为 LeetCode解题记录(贪心算法)(一) 下面正式开始我们的刷题之旅 2. 贪心 763. 划分字母区间(中等 ...

  2. Leetcode解题记录

    尽量抽空刷LeetCode,持续更新 刷题记录在github上面,https://github.com/Zering/LeetCode 2016-09-05 300. Longest Increasi ...

  3. mysql——leetcode问题记录

    问题: 表1: Person +-------------+---------+ | 列名 | 类型 | +-------------+---------+ | PersonId | int | | ...

  4. [leetcode] 题解记录 11-20

    博客园markdown太烂, 题解详情https://github.com/TangliziGit/leetcode/blob/master/solution/11-20.md Leetcode So ...

  5. [leetcode] 题解记录 1-10

    博客园markdown太烂, 题解详见https://github.com/TangliziGit/leetcode/blob/master/solution/1-10.md Leetcode Sol ...

  6. LeetCode解题记录(贪心算法)(一)

    1. 前言 目前得到一本不错的算法书籍,页数不多,挺符合我的需要,于是正好借这个机会来好好的系统的刷一下算法题,一来呢,是可以给部分同学提供解题思路,和一些自己的思考,二来呢,我也可以在需要复习的时候 ...

  7. [leetcode解题记录]Jump Game和Jump Game II

    Jump Game Given an array of non-negative integers, you are initially positioned at the first index o ...

  8. LeetCode解题记录(双指针专题)

    1. 算法解释 双指针主要用于遍历数组,两个指针指向不同的元素,从而协同完成任务.也可以延伸到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,则也称为滑动窗口(两个指针包围的区域 ...

  9. Leetcode Scratching Recorder

    Author: Zhong-Liang Xiang Start from: August 7th, 2017 这个题为连滚带爬的Leetcode填坑记录就这么开始了. 网上看了看, 一堆小伙从2014 ...

随机推荐

  1. NDK笔记(二)-在Android Studio中使用ndk-build

    前面一篇我们接触了CMake,这一篇写写关于ndk-build的使用过程.刚刚用到,想到哪儿写哪儿. 环境背景 Android开发IDE版本:AndroidStudio 2.2以上版本(目前已经升级到 ...

  2. linux 复 带进度条

    rsync命令 #rsync -av --progress /mnt/yidong2/full20100526.tar.gz /mnt/yidong1/ 可以实现本机带进度条提示拷贝,可以实现不同机器 ...

  3. C#进阶目录

    一.Sql语句的性能优化 二.Quartz.NET的介绍 三.Log4.Net的介绍 四.Topshelf的介绍 五.Git的使用 六.IEnumerable接口迭代原理 七.Lambada表达式的演 ...

  4. 理解Attribute

    注:本文转载自http://kb.cnblogs.com/page/87531/ Attribute与Property 的翻译区别 Attribute 一般译作“特性”,Property 仍然译为“属 ...

  5. 打不死的redis集群

    导读 最近遇到部分系统因为redis服务挂掉,导致部分服务不可用.所以希望搭建一个redis集群镜像,把原先散落各处的redis服务器统一管理起来,并且保障高可用和故障自动迁移. 最近遇到部分系统因为 ...

  6. 初识java泛型

    1 协变数组类型(covariant array type) 数组的协变性: if A IS-A B then A[] IS-A B[] 也就是说,java中的数组兼容,一个类型的数组兼容他的子类类型 ...

  7. 高性能MySQL(四):schema陷阱

    一.schema陷阱 二.缓存表和汇总表 三.范式和反范式

  8. ListView只更新某个item

    方案1:针对需要更新的item调用public View getView(int position, View convertView, ViewGroup parent)即可.如: public c ...

  9. 常见端口 HTTP代码

    端口号 系统保留了前0到1023端口作为常用的网络服务. 0-1023 公认端口 1024-49151 注册端口 49152-65535 动态或私有端口 1 TCPMUX 主要在SGI Irix机器 ...

  10. IIS性能提升

    1. 调整IIS 7应用程序池队列长度 由原来的默认1000改为65535. IIS Manager > ApplicationPools > Advanced Settings Queu ...